OK, something about that pesky underscore doesn't want to evaluate inside the TEXT function, so I cheated. I used another character (¢) and then used a substitute function to change it INTO an underscore. Problem solved.
Sub OpenByDate()
'Opens a sheet based on date search
Dim TheString As String, TheDate As Date, ThePath As String
TheDate = Date
ThePath = "C:\holdings\" & WorksheetFunction.Text(TheDate, "YYYY¢MM¢DD") & "\"
ThePath = WorksheetFunction.Substitute(ThePath, "¢", "_")
TheString = "portfolio " & WorksheetFunction.Text(TheDate, "MM.DD.YYYY") & ".xls"
On Error Resume Next
Do
Workbooks.Open ThePath & TheString
If Err <> 0 Then
TheDate = TheDate - 1
ThePath = "C:\holdings\" & WorksheetFunction.Text(TheDate, "YYYY¢MM¢DD") & "\"
ThePath = WorksheetFunction.Substitute(ThePath, "¢", "_")
TheString = "portfolio " & WorksheetFunction.Text(TheDate, "MM.DD.YYYY") & ".xls"
End If
Loop Until Err = 0
End Sub
Bookmarks