尝试在单元格引用中指定循环变量时,为什么会收到错误1004?

时间:2019-02-05 17:00:16

标签: excel vba

我被困住了,主要是因为我仍然在VBA上崭露头角。因此,我非常感谢您可以借给我的任何帮助。我在这里浏览了许多其他Error 1004帖子,但它们要么是针对不同的问题,要么是我很无知以至于不知道如何处理其中的建议。

我的问题是:我有两本工作簿,一本包含原始数据,另一本中将原始数据合并到相关统计信息中。我试图通过统计书中的两个条件对原始工作簿(6620)中X:X中的数据求和,然后更新统计书中相应单元格中的值。

我遇到了运行时错误1004:在指示的点出现了应用程序定义的错误或对象定义的错误。我不知道该如何超越。

非常感谢您的帮助!

据我所知:

Option Explicit

Sub ImportFTEs()

Application.ScreenUpdating = False
Application.EnableEvents = False

    Dim ws As Integer

    Workbooks.Open Filename:=ActiveWorkbook.Path & "\6620\FY19*.xlsb"
        For ws = 1 To Worksheets.Count
            Sheets(ws).Name = "Sheet1"
        Next ws

        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
        Dim l As Integer
        Dim totalFTE As Long
        Dim lastRow As Integer
            lastRow = Cells(Rows.Count, "D").End(xlUp).Row
        Dim wb As Worksheet
            Set wb = Workbooks.Open(ActiveWorkbook.Path & "\FY19*.xlsb").Sheets("Sheet1")
        Dim wc As Worksheet
            Set wc = ThisWorkbook.Sheets("B")
        Dim sum1R As Range
            Set sum1R = wb.Range("X:X")
        Dim arg2R As Range
            Set arg2R = wb.Range("D:D")
        Dim arg2C As Range
            Set arg2C = wc.Cells(7, i)
                                 >>> Error 1004
        Dim arg3R As Range
            Set arg3R = wb.Range("S:S")
        Dim arg3C As Range
            Set arg3C = wc.Cells(j, 6)
                                 >>> Error 1004
            For k = 8 To 18
                For l = 7 To 18
                    For i = 7 To 18
                        For j = 8 To 18
                            wc.Cells(k, l).value = Application.WorksheetFunction.SumIfs(sum1R, arg2R, arg2C, arg3R, arg3C)
                        Next j
                    Next i
                Next l
            Next k
    ActiveWorkbook.Close savechanges:=True

Application.ScreenUpdating = True
Application.EnableEvents = True

End Sub

1 个答案:

答案 0 :(得分:2)

应该是这样-您需要Set行,其中分别在它们各自的循环中包含ij -这样,值实际上就会增加(并且初始化时不为0):

Option Explicit
Sub ImportFTEs()

    Application.ScreenUpdating = False
    Application.EnableEvents = False

    Workbooks.Open Filename:=ActiveWorkbook.Path & "\6620\FY19*.xlsb"

    Dim i As Long, j As Long, k As Long, l As Long
    Dim totalFTE As Long

    Dim wb As Worksheet
    Set wb = Workbooks.Open(ActiveWorkbook.Path & "\FY19*.xlsb").Sheets("Sheet1")

    Dim wc As Worksheet
    Set wc = ThisWorkbook.Sheets("B")

    Dim sum1R As Range
    Set sum1R = wb.Range("X:X")

    Dim arg2R As Range
    Set arg2R = wb.Range("D:D")

    Dim arg3R As Range
    Set arg3R = wb.Range("S:S")

    Dim arg2C As Range
    Dim arg3C As Range

    For k = 8 To 18
        For l = 7 To 18
            For i = 7 To 18

                Set arg2C = wc.Cells(7, i)

                For j = 8 To 18

                    Set arg3C = wc.Cells(j, 6)

                    wc.Cells(k, l).Value = Application.WorksheetFunction.SumIfs(sum1R, arg2R, arg2C, arg3R, arg3C)

                Next j
            Next i
        Next l
    Next k

    ActiveWorkbook.Close True

    Application.ScreenUpdating = True
    Application.EnableEvents = True

End Sub