2

I'm very new with VBA and I'm having a problem with the worksheet functions. I'm not sure if I'm using it right as I've been getting a run-time error '1004': Method 'Range' of '_Global' failed. Here's my VBA below hope you guys can help me out. Thanks!

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
2
  • Thanks @Rory! I think I understand the problems you pointed out in my code. However I'm still having an error with it. Is a 'Type Mismatch' Error. Commented Jan 5, 2015 at 9:36
  • No worries! I figured it out @Rory thanks for the help! Commented Jan 5, 2015 at 10:41

1 Answer 1

2

I haven't gone through all the code checking for issues, but the main ones were:

  1. You weren't properly qualifying your Range and Cells calls with a worksheet object;
  2. You can't use Range(cells(x, y)) unless the value of the cell at Cells(x, y) is the address of a range.
  3. Your parentheses weren't correct in the Index / Match part.

Try this one:

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
Sign up to request clarification or add additional context in comments.

Comments

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.