Excel VBA - 索引和匹配 VBA 错误

标签 excel vba

我对 VBA 非常陌生,但我在使用工作表函数时遇到问题。我不确定我是否正确使用它,因为我收到运行时错误“1004”:“_Global”的方法“范围”失败。下面是我的VBA希望大家能帮助我。谢谢!

Sub MergeAllWorkbooks2()

    Dim FolderPath As String
    Dim X As Long
    Dim i As Long
    Dim FileName As String
    Dim WorkBk As Workbook
    Dim SourceRange As Range
    Dim DestRange As Range


    'path of directory
    FolderPath = "C:\Users\XXXX\Desktop\New folder\XXXXX\"

    ' Setting starting points
    X = 3

    ' Call Dir the first time, pointing it to all Excel files in the folder path.
    FileName = Dir(FolderPath & "*.xl*")

    ' Loop until Dir returns an empty string.
    Do Until FileName = ""
        ' Open a workbook in the folder
        Set WorkBk = Workbooks.Open(FolderPath & FileName)

        'Set the source range
        Set SourceRange = WorkBk.Worksheets(1).Range("C2:C7")

        ' Set the destination range
        Workbooks("Summary.xlsm").Worksheets("Sheet1").Activate
        Set DestRange = Workbooks("Summary.xlsm").Worksheets("Sheet1").Range(Cells(4, X), Cells(9, X))

        ' Copy over the values from the source to the destination.
        DestRange.Value = SourceRange.Value

            'Get hourly from each file
            For i = 12 To 8762
                Workbooks("Summary.xlsm").Worksheets("Sheet1").Range(Cells(12, X), Range(Cells(12, X).End(xlDown))) = _
                    Application.WorksheetFunction.Index(WorkBk.Worksheets(2).Range("B3:B8762"), Application.Match(Workbooks("Summary.xlsm").Worksheets("Sheet1").Range(Cells(i, X)), WorkBk.Worksheets(2).Range("A3:A8763")), 0)
            Next i

        ' Increase NColumn so that we know where to copy data next.
        X = X + DestRange.Columns.Count

        ' Close the source workbook without saving changes.
        WorkBk.Close savechanges:=False

        ' Use Dir to get the next file name.
        FileName = Dir()
    Loop

'    Range("C4:C9", Range("C4:C9").End(xlToRight)).Sort key1:=Range("b7"), key2:=Range("b8"), key3:=Range("b9"), _
'     order1:=xlAscending, Orientation:=xlLeftToRight

endTime = Now()
totTimeSec = Round(((endTime - startTime) * (24 * CLng(3600))), 1)
MsgBox (totTimeSec & " seconds")

End Sub

最佳答案

我还没有检查所有的代码问题,但主要的问题是:

  1. 您没有正确限定您的 Range 和 Cells 调用 工作表对象;
  2. 您不能使用Range(cells(x, y)),除非 Cells(x, y) 处的单元格值是范围的地址。
  3. 索引/匹配部分中的括号不正确。

试试这个:

Sub MergeAllWorkbooks2()

    Dim FolderPath            As String
    Dim X                     As Long
    Dim i                     As Long
    Dim FileName              As String
    Dim WorkBk                As Workbook
    Dim SourceRange           As Range
    Dim DestRange             As Range
    Dim wsDest                As Worksheet


    'path of directory
    FolderPath = "C:\Users\XXXX\Desktop\New folder\XXXXX\"

    ' Setting starting points
    X = 3

    ' Call Dir the first time, pointing it to all Excel files in the folder path.
    FileName = Dir(FolderPath & "*.xl*")

    ' Loop until Dir returns an empty string.
    Do Until FileName = ""
        ' Open a workbook in the folder
        Set WorkBk = Workbooks.Open(FolderPath & FileName)

        'Set the source range
        Set SourceRange = WorkBk.Worksheets(1).Range("C2:C7")

        ' Set the destination range
        Set wsDest = Workbooks("Summary.xlsm").Worksheets("Sheet1")

        Set DestRange = wsDest.Range(wsDest.Cells(4, X), wsDest.Cells(9, X))

        ' Copy over the values from the source to the destination.
        DestRange.Value = SourceRange.Value

        'Get hourly from each file
        For i = 12 To 8762
            wsDest.Range(wsDest.Cells(12, X), wsDest.Cells(12, X).End(xlDown)) = _
            Application.WorksheetFunction.Index(WorkBk.Worksheets(2).Range("B3:B8762"), Application.Match(wsDest.Cells(i, X), WorkBk.Worksheets(2).Range("A3:A8763"), 0), 1)
        Next i

        ' Increase NColumn so that we know where to copy data next.
        X = X + DestRange.Columns.Count

        ' Close the source workbook without saving changes.
        WorkBk.Close savechanges:=False

        ' Use Dir to get the next file name.
        FileName = Dir()
    Loop

    '    Range("C4:C9", Range("C4:C9").End(xlToRight)).Sort key1:=Range("b7"), key2:=Range("b8"), key3:=Range("b9"), _
         '     order1:=xlAscending, Orientation:=xlLeftToRight

    endTime = Now()
    totTimeSec = Round(((endTime - startTime) * (24 * CLng(3600))), 1)
    MsgBox (totTimeSec & " seconds")

End Sub

关于Excel VBA - 索引和匹配 VBA 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27775911/

相关文章:

ms-access - 循环 Access 表单上的文本框并更改数据

java - java中如何防止方法被覆盖

excel - 如何将数字四舍五入到最接近的 0.5?

html - 使用 VBA 将 HTML 表格转换为 Excel

Excel 电子表格到 COBOL 程序

excel - 如何在 Excel 中创建状态对话框

python - 有没有办法在pywin32中解码数字COM错误代码

c# - 与人机接口(interface)设备 (HID) 通信的 VBA 代码

c# - 编程附加组件,访问 HRMS 后端

excel - 验证单元格并获得成绩