Consulting

Results 1 to 3 of 3

Thread: Solved: Need help to understand this code

  1. #1
    VBAX Regular
    Joined
    Jan 2009
    Posts
    89
    Location

    Solved: Need help to understand this code

    [vba]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[/vba]

    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.

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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.

    [vba]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[/vba]

  3. #3
    VBAX Regular
    Joined
    Jan 2009
    Posts
    89
    Location
    Thank you. That worked.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •