0

Line 22 is throwing the error (Set wbPath2)

This code is supposed to loop through each worksheet in my workbook and, as it loops, open another workbook related to the current loop iteration, then sum a column, then put that SUM in my original workbook. I'm getting and object error 91. I've been scratching my head for a while. Anyone know why this error message appears?

Private Sub PopulateData_Click()
Application.ScreenUpdating = False

Dim ws As Worksheet
Dim lastDay As Long

lastDay = Day(WorksheetFunction.EoMonth(ComboBox1.Value & Year(Date), 0))
monthNumber = Month(DateValue("01-" & ComboBox1.Value & "-1900")) 
Root = "C:\myDirectory\" & Year(Date) & "\" & 
        monthNumber & ". " & ComboBox1.Value & " " & Year(Date) & "\"

'TOTAL CARS PER WEEK
Dim wbPath2 As Object
sourceFile = monthNumber & ". " & ComboBox1.Value & " " & Year(Date)
sourceSheet = "\[" & ws.Name & " " & monthNumber & "." & lastDay & "." & 
               Format(Now(), "yy") & ".csv]"


For Each ws In ThisWorkbook.Sheets
    If (ws.Name <> "Master") And (ws.Name <> "Combined") Then
        Set wbPath2 = Workbooks.Open(Root & ws.Name & " " & monthNumber & 
                      "." & lastDay & "." & Format(Now(), "yy") & ".csv")
        With ws
            .Cells(Application.WorksheetFunction.Match("Total cars per 
            week", Range("A:A"), 0), 18).Formula = "=SUM('" & Root & 
            sourceFile & sourceSheet & ws.Name & " " & monthNumber & "." & 
            lastDay & "." & Format(Now(), "yy") & "'!$H:$H)"
        End With

        wbPath2.Close
        MsgBox wbPath2
    End If
Next

Application.ScreenUpdating = True
End Sub
10
  • 2
    what line of code is the error happening on? Commented Jun 30, 2017 at 19:25
  • 3
    I would have expected it to occur on the line before that, where you are using ws.Name without having set ws to anything. Commented Jun 30, 2017 at 19:33
  • 3
    Note that the Sheets collection contains Worksheet objects, but also Chart objects, which will make your code throw another type of error if your workbook contains any. If you want to iterate worksheets, iterate the Worksheets collection. Commented Jun 30, 2017 at 20:00
  • 2
    You also have unqualified Range calls inside that With block - these are implicitly referring to whatever worksheet is currently active, and that's yet another bug / runtime error 1004 waiting to be thrown. Commented Jun 30, 2017 at 20:01
  • 2
    Also, Option Explicit is your friend :D Commented Jun 30, 2017 at 20:20

1 Answer 1

3

I had to Set the ws object to resolve run time 91 error. Look in the comments section for Mat's Mug's additional bug fixes.

Private Sub PopulateData_Click()

    Application.ScreenUpdating = False

    Dim ws As Worksheet
    Set ws = ThisWorkbook.Activesheet

    '...
Sign up to request clarification or add additional context in comments.

1 Comment

I still can't see how that would have fixed an error at the Set wbPath2 statement, which is where the question says the code crashed, because by the time it gets to that line ws is already set by the For Each ws In ThisWorkbook.Sheets line. I still think your code must be crashing earlier, at the line using ws.Name before the loop starts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.