简体   繁体   中英

Excel VBA - Unable to find error causing 'runtime error 91'

I have the below code which is causing a

runtime error 91 - Object Variable not set

I have dim'd each workbook, worksheet, and variable, but it does not remove the error.

    Dim r As Long, c As Long
    Dim snowq As Workbook, CAQual As Workbook, qsum As Worksheet, CAqsum As Worksheet
    For r = 2 To 8
        For c = 2 To 4
            combinedReports.Worksheets("combinedQualities").Cells(r, c).Value = _
                snowq.Worksheets("qsum").Cells(r, c).Value + CAQual.Worksheets("CAqsum").Cells(r, c).Value
        Next
    Next

NOTE that the first workbook and worksheet are dim'd further up in the code but within the same module.

Any assistance is appreciated.

EDIT:

This is the top of the module which includes setting the actual workbook and worksheet(s):

'Create new workbook
Dim combinedReports As Workbook, combinedCsats As Worksheet, combinedQualities As Worksheet, combinedTickets As Worksheet
Set combinedReports = Workbooks.Add
Sheets("Sheet1").name = "Combined CSAT's"
Set combinedCsats = combinedReports.Sheets("Combined CSAT's")
Sheets.Add After:=ActiveSheet
Sheets("Sheet2").name = "Combined Qualities"
Set combinedQualities = combinedReports.Sheets("Combined Qualities")
Sheets.Add After:=ActiveSheet
Sheets("Sheet3").name = "Combined Tickets"
Set combinedTickets = combinedReports.Sheets("Combined Tickets")

Insert this code before the For r = 2 to 8 loop the code will stop on the faulty object.

Debug.Print combinedReports.Name
Debug.Print combinedReports.Worksheets("combinedQualities").Name
Debug.Print snowq.Name
Debug.Print snowq.Worksheets("qsum").Name
Debug.Print CAQual.Name
Debug.Print CAQual.Worksheets("CAqsum").Name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM