Question : Date variable from another workbook, set as variable - VBA

Experts,

I am trying to fetch some date data from another worksheet (done within VBA code only) and store these as variables to use in automation of header (date information) creation. Ultimately, I want to set the different date variables once (in one file) and then have the 25 other files that need header updates to all be linked back to that one, with the code (which I will change from sub to sub at print time) executed upon printing so that the correct dates come out without having to change them all by hand. Then the next month, a new date file is created, variables set, each file at print time will update the header, etc.

In my first question the subject, we seem to be stuck in getting the data over, without having to bring it as string placed into a cell on the worksheet and then set a variable as the value of that cell.

Can this be done without the use of a “cell/worksheet” as a “scratch pad”?

Also, my If Then Else, on lines 19 to 23, which is suppose to check if the file exists (ie, I have changed it from ending in MM_YYYY to the correct date, as found in variable FolderDate) seems to always go to the Else (when hitting F8 in VBE) no matter if the file DOES or DOES NOT exist on my system. What I am doing wrong here?

Please see the other question here:
http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_26353195.html

The most up-to-date code in use is listed below, along with the suggestion for bringing the data in as a string and then using VBA to set that string as a value in the variable.

Thank you for your time and help!
Kevin
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
Sub UpdateHeaderDataVariable()

Application.ScreenUpdating = False
Dim WS As Worksheet
Dim InDate1 As String
Dim InDate2 As String
Dim InDate3 As String
Dim Date1 As Date
Dim Date2 As Date
Dim Date3 As Date
Dim FolderDate As String

FolderDate = ActiveWorkbook.Path
FolderDate = Mid(FolderDate, InStrRev(ActiveWorkbook.Path, "\") - 7, 7)
MsgBox FolderDate

' Check if Monthly_Report_Date_-_MM_YYYY.xls exists with MM-YYYY converted to FolderDate Variable
' If it exists, then go on with Sub, if it does not exist, show message box and then exit
    If Dir("C:\Temp Downloads\VBA\" & FolderDate & "\[Monthly_Report_Date_-_" & FolderDate & ".xls") <> "" Then
        MsgBox "File exists" ' For Testing Only, Real sub will not have message box
            Else
        MsgBox "File - Monthly_Report_Date_-_" & FolderDate & ".xls" & vbNewLine & "In Directory - C:\Temp Downloads\VBA\" & FolderDate & vbNewLine & "DOES NOT EXIST"
    End If

InDate1 = "='C:\Temp Downloads\VBA\ " & FolderDate & " \[Monthly_Report_Date_-_" & FolderDate & ".xls]Sheet1'!$A$1"
Date1 = InDate1

'InDate1 = (cd ..) File (Monthly_Report_Date_-_??-???.xls, Sheet1, Cell A1)

'Date1 = CDate(InDate1)

'Set WS = Sheets("Sheet1")
'    WS.PageSetup.CenterHeader = ""
'    WS.PageSetup.CenterHeader = "Internal Report (Variable Here)"
     ' Format the Header here to be 16 point, Bold, Garmond
    
End Sub
1:
2:
3:
Range("A1").Formula = "='H:\VBA\ " & FolderDate & " \[Monthly_Report_Date_-_" & FolderDate & ".xls]Sheet1'!$A$1"

InDate1 = Range("A1").Value

Answer : Date variable from another workbook, set as variable - VBA

then I would also add a Main routine that calls the various parts

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
Dim dtIN1 As Date, dtIn2 As Date, dtIn3 As Date
Dim sDateFilename
Const datefile = "Monthly_Report_Date_-_" 'MM-YYYY.XLS
Dim wbReport As Workbook   'the pointer to the workbook being used as report target


Sub Main()

    Set wbReport = Application.ActiveWorkbook
    RetrieveDateValues
    UpdateHeaderDataVariable
    
End Sub
Random Solutions  
 
programming4us programming4us