PDA

View Full Version : Solved: Need help to understand this code



leal72
06-22-2012, 09:16 AM
Public Sub MovePreviousDayDailyLog()
Dim IsWorkbookOpen As Boolean
Dim i As Integer

For i = 1 To Workbooks.Count
If Workbooks(i).Name = "Daily Logs.xlsx" Then
IsWorkbookOpen = True
Exit For
End If
Next
If IsWorkbookOpen = False Then
Workbooks.Open (ThisWorkbook.Path & "\Daily Logs.xlsx")
End If
IsWorkbookOpen = False
Workbooks("Main Log").Sheets(2).Move after:=Workbooks("Daily Logs.xlsx").Sheets(1)
Workbooks("Daily Logs").Close savechanges:=True
End Sub

I open the Main Log.xlsm file and get a "script out of range" error and the debug takes me to the line in bold print. The macro runs as soon as the Main Log book is opened. I need to fix the error but not having a luck so far.

Kenneth Hobs
06-22-2012, 09:37 AM
Maybe you should use LCase and the xlsm file extension in your Workbooks? The need for the file extension can vary depending on the user's Windows setting.

Public Sub MovePreviousDayDailyLog()
Dim IsWorkbookOpen As Boolean
Dim i As Integer

For i = 1 To Workbooks.Count
If LCase(Workbooks(i).Name) = "daily logs.xlsx" Then
IsWorkbookOpen = True
Exit For
End If
Next
If IsWorkbookOpen = False Then
Workbooks.Open (ThisWorkbook.Path & "\Daily Logs.xlsx")
End If
IsWorkbookOpen = False
Workbooks("Main Log.xlsm").Sheets(2).Move after:=Workbooks("Daily Logs.xlsx").Sheets(1)
Workbooks("Daily Logs.xlsx").Close savechanges:=True
End Sub

leal72
06-22-2012, 09:58 AM
Thank you. That worked.