Originally Posted by
cliffwms
...i have some code above it that selects the correct wb/filename for the corresponding date
any idea what I need to do to fix it.....and still be able to use a "revolving" file name...
A fairly blind stab, but given that your current code provides the correct filename, check to see if the wb exists (is open) first, and if not, set a reference to it on opening...
Option Explicit
Sub Calling()
Dim RetVal As Variant
If GetInfo(ThisWorkbook.Path, "Book2.xls", RetVal) Then
MsgBox RetVal
Else
MsgBox "Failed"
End If
End Sub
Function GetInfo(Path As String, WBName As String, RetVal) As Boolean
Dim wb As Workbook
Dim bolClosed As Boolean
'//attempt to set a reference //
On Error Resume Next
Set wb = Workbooks(WBName)
On Error GoTo 0
'// If is Nothing, then not open //
If wb Is Nothing Then
'// Tack in trailing seperator //
If Not Right(Path, 1) = "\" Then Path = Path & "\"
'// attempt open //
On Error Resume Next
Set wb = Workbooks.Open(Path & WBName)
On Error GoTo 0
'// if not Nothing, then opened. Else bailout //
If Not wb Is Nothing Then
GetInfo = True
Else
Exit Function
End If
'// If wb was Nothing originally, flag, so we can //
'// close. //
bolClosed = True
End If
'// Do something here. I just retuned a val. //
RetVal = wb.Sheets(1).Range("A1").Value
'// If closed initially, re-close //
If bolClosed Then wb.Close False
End Function
Hope that helps,
Mark