Page 2 of 2 FirstFirst 12
Results 11 to 17 of 17

Thread: VBA Code to Open Workbook and copy data

  1. #11
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi

    Replace the following lines

    Code:
    UniqueHeaders Application.Index(d, 1, 0)
    For r = 2 To UBound(d, 1) 'skips header
    with

    Code:
    UniqueHeaders Application.Index(d, 2, 0)'second row holds the header
    For r = 3 To UBound(d, 1) 'skips header
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  2. #12
    Senior Member
    Join Date
    Apr 2012
    Posts
    193
    Rep Power
    14
    Hi

    Thanks for the reply & your help

    When activating the macro, it now comes up with run time error 9 "subscript out of range" and the folowing code is highlighted

    k(n, j) = d(r, c)

    It would be appreciated if you would amend your code and advise accordingly

  3. #13
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi,

    When it errors, click on debug and move the cursor over n and j and find the value. If the current value is greater than 50000 and 100 of n and j respectively, then replace the statement

    redim k(1 to 50000,1 to 100)

    with

    redim k(1 to 100000,1 to 200)

    or whatever the maximum possible rows or columns of output data.
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  4. #14
    Senior Member
    Join Date
    Apr 2012
    Posts
    193
    Rep Power
    14
    Hi

    Thanks for the reply. I clicked on debug and hovered over N and it gives me N= 1 , J = 0

    It would be appreciated if you would test your code on the attached files & let me know
    Attached Files Attached Files

  5. #15
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi,

    OK. use this.

    Code:
    Dim dic             As Object
    Dim Counter         As Long
    Sub Extraxt_Data()
        
        Dim r           As Long
        Dim c           As Long
        Dim n           As Long
        Dim j           As Long
        Dim Fldr        As String
        Dim Fname       As String
        Dim wbkActive   As Workbook
        Dim wbkSource   As Workbook
        Dim Dest        As Range
        Dim d, k()
        
        '// User settings
        Const SourceFileType        As String = "*"
        Const DestinationSheet      As String = "Sheet1"
        Const DestStartCell         As String = "A1"
        Const HeaderRow             As Long = 2
        '// End
        
        Application.ScreenUpdating = False
        Counter = 0
        With Application.FileDialog(4)
            .Title = "Select source file folder"
            .AllowMultiSelect = False
            If .Show = -1 Then
                Fldr = .SelectedItems(1)
            Else
                GoTo Xit
            End If
        End With
        
        
        Set dic = CreateObject("scripting.dictionary")
            dic.comparemode = 1
        Set wbkActive = ThisWorkbook
        ReDim k(1 To 10000, 1 To 200)
        Set Dest = wbkActive.Worksheets(DestinationSheet).Range(DestStartCell)
        Fname = Dir(Fldr & "\*." & SourceFileType)
        Do While Len(Fname)
            If wbkActive.Name <> Fname Then
                Set wbkSource = Workbooks.Open(Fldr & "\" & Fname)
                With wbkSource.Worksheets(1)
                    d = .Range("a1").CurrentRegion.Value2
                    UniqueHeaders Application.Index(d, HeaderRow, 0)
                    For r = HeaderRow + 1 To UBound(d, 1)
                        If Len(d(r, 1)) Then
                            n = n + 1
                            For c = 1 To UBound(d, 2)
                                If Len(Trim$(d(HeaderRow, c))) Then
                                    j = dic.Item(Trim$(d(HeaderRow, c)))
                                    k(n, j) = d(r, c)
                                End If
                            Next
                        End If
                    Next
                    Erase d
                End With
                wbkSource.Close 0
                Set wbkSource = Nothing
            End If
            Fname = Dir()
        Loop
        
        If n Then
            Dest.Resize(, dic.Count) = dic.keys
            Dest.Offset(1).Resize(n, dic.Count) = k
            MsgBox "Done"
        End If
    Xit:
        Application.ScreenUpdating = True
        
    End Sub
    Private Sub UniqueHeaders(ByRef DataHeader)
        
        Dim i   As Long
        Dim j   As Long
        
        With Application
            j = .ScreenUpdating
            .ScreenUpdating = False
        End With
        
        For i = LBound(DataHeader) To UBound(DataHeader)
            If Len(Trim$(DataHeader(i))) Then
                If Not dic.exists(Trim$(DataHeader(i))) Then
                    Counter = Counter + 1
                    dic.Add Trim$(DataHeader(i)), Counter
                End If
            End If
        Next
        
        Application.ScreenUpdating = j
        
    End Sub
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  6. #16
    Senior Member
    Join Date
    Apr 2012
    Posts
    193
    Rep Power
    14
    Hi Admin

    You are a star. The code works perfectly. Thanks for all the time and effort in sorting out the problem

  7. #17
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Thanks for the feedback.
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

Similar Threads

  1. Replies: 2
    Last Post: 05-28-2013, 05:32 PM
  2. Replies: 17
    Last Post: 05-22-2013, 11:58 PM
  3. Replies: 7
    Last Post: 05-17-2013, 10:38 PM
  4. Replies: 0
    Last Post: 04-20-2013, 10:07 AM
  5. VBA code to copy data from source workbook
    By Howardc in forum Excel Help
    Replies: 1
    Last Post: 07-30-2012, 09:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •