Excel VBA - 无法找到导致“运行时错误91”的错误

时间:2017-12-14 12:34:16

标签: excel vba excel-vba

我有以下代码导致

  

运行时错误91 - 未设置对象变量

我已将每个工作簿,工作表和变量调暗,但它不会删除错误。

    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

请注意,第一个工作簿和工作表在代码中进一步变暗,但在同一个模块中。

感谢任何帮助。

编辑:

这是模块的顶部,包括设置实际工作簿和工作表:

'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")

1 个答案:

答案 0 :(得分:0)

For r = 2 to 8循环之前插入此代码,代码将停在故障对象上。

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