The hospitality industry work I'm doing is based on a report that is converted from a .pdf file and saved with a filename of the report date - so today's report is 012814.xlsx, tomorrow will be 012914.xlsx, etc.
I'm working on a macro that will be run from another workbook, pulling data from this report. Since the filename changes on a daily basis, I'm trying to get the macro to check to see if the proper workbook is open, and if so, switch to it. If it's not open, it will prompt the user to open it.
I've tried two different approaches, but neither have worked as I expected. This version is based on help I received here that checks for a specific named workbook:
I also tried this method:
Yet, when I have open one of these reports that has this type of filename, it's still prompting me to open the file, so the bFlag condition is never being set to True. I'm at a loss as to why?
I'm working on a macro that will be run from another workbook, pulling data from this report. Since the filename changes on a daily basis, I'm trying to get the macro to check to see if the proper workbook is open, and if so, switch to it. If it's not open, it will prompt the user to open it.
I've tried two different approaches, but neither have worked as I expected. This version is based on help I received here that checks for a specific named workbook:
Code:
Sub FindHandFWorkbookTest()
Dim wb As Workbook
Dim bFlag As Boolean
Dim sName As String
For Each wb In Application.Workbooks
If wb.Name Like "######" Then
bFlag = True
sName = wb.Name
Exit For
End If
Next wb
If bFlag = True Then
Workbooks(sName).Activate
Else
MsgBox ("Your History and Forecast spreadsheet is not currently open, please open it now")
FileToOpen = Application.GetOpenFilename _
(Title:="Please select your history and forecast report", _
FileFilter:="Excel Files *.xlsx (*.xlsx),")
If FileToOpen = False Then
MsgBox "No file specified.", vbExclamation, "Duh!!!"
Exit Sub
Else
Workbooks.Open Filename:=FileToOpen
End If
End If
End Sub
I also tried this method:
Code:
Sub FindHandFWorkbookTest()
Dim wb As Workbook
Dim bFlag As Boolean
Dim sName As String
For Each wb In Application.Workbooks
If Len(wb.Name) = 6 And IsNumeric(wb.Name) Then
bFlag = True
sName = wb.Name
Exit For
End If
Next wb
If bFlag = True Then
Workbooks(sName).Activate
Else
MsgBox ("Your History and Forecast spreadsheet is not currently open, please open it now")
FileToOpen = Application.GetOpenFilename _
(Title:="Please select your history and forecast report", _
FileFilter:="Excel Files *.xlsx (*.xlsx),")
If FileToOpen = False Then
MsgBox "No file specified.", vbExclamation, "Duh!!!"
Exit Sub
Else
Workbooks.Open Filename:=FileToOpen
End If
End If
End Sub
Yet, when I have open one of these reports that has this type of filename, it's still prompting me to open the file, so the bFlag condition is never being set to True. I'm at a loss as to why?