+ Reply to Thread
Results 1 to 4 of 4

Copying data from multiple files have the same structure

Hybrid View

  1. #1
    Forum Contributor tax112's Avatar
    Join Date
    02-28-2013
    Location
    Thailand
    MS-Off Ver
    Excel 2016 | 2019
    Posts
    438

    Copying data from multiple files have the same structure

    hello members
    I'm having difficulty in updating the data.
    Database my input includes a lot of the same file structure.
    Now I need to copy data from that file into a new file.
    Input: company1, Company 2, company3 ....
    Output: Company
    Can someone help me.
    I thank you in advance.
    Attached Files Attached Files

  2. #2
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Copying data from multiple files have the same structure

    Maybe :

    Private Function GetLastRow() As Long
      GetLastRow = Application.Max(Cells(Rows.Count, "C").End(xlUp).Row + 1, 4)
    End Function
    Sub Test()
      Dim wsMain As Worksheet, lastRow As Long, strFilePattern As String, strDir As String
      Application.ScreenUpdating = False
    
      strFilePattern = ThisWorkbook.Path & "\Company*.*"
    
      Set wsMain = ActiveSheet
      Rows("4:" & GetLastRow).Clear
      strDir = Dir(strFilePattern)
      Do While Len(strDir)
         If strDir <> ThisWorkbook.Name Then
            lastRow = GetLastRow
            With Workbooks.Open(ThisWorkbook.Path & "\" & strDir)
              With .Sheets("Sheet1")
                wsMain.Cells(lastRow, "A").Value = .Range("C2").Value
                wsMain.Cells(lastRow, "B").Value = .Range("F2").Value
                .Range("C4").CurrentRegion.Copy
                wsMain.Cells(lastRow, "C").PasteSpecial xlPasteValues
              End With
              Application.DisplayAlerts = False
                .Close
              Application.DisplayAlerts = True
            End With
         End If
         strDir = Dir
      Loop
    
      Application.ScreenUpdating = True
    End Sub

  3. #3
    Forum Contributor tax112's Avatar
    Join Date
    02-28-2013
    Location
    Thailand
    MS-Off Ver
    Excel 2016 | 2019
    Posts
    438

    Re: Copying data from multiple files have the same structure

    Quote Originally Posted by karedog View Post
    Maybe :

    Private Function GetLastRow() As Long
      GetLastRow = Application.Max(Cells(Rows.Count, "C").End(xlUp).Row + 1, 4)
    End Function
    Sub Test()
      Dim wsMain As Worksheet, lastRow As Long, strFilePattern As String, strDir As String
      Application.ScreenUpdating = False
    
      strFilePattern = ThisWorkbook.Path & "\Company*.*"
    
      Set wsMain = ActiveSheet
      Rows("4:" & GetLastRow).Clear
      strDir = Dir(strFilePattern)
      Do While Len(strDir)
         If strDir <> ThisWorkbook.Name Then
            lastRow = GetLastRow
            With Workbooks.Open(ThisWorkbook.Path & "\" & strDir)
              With .Sheets("Sheet1")
                wsMain.Cells(lastRow, "A").Value = .Range("C2").Value
                wsMain.Cells(lastRow, "B").Value = .Range("F2").Value
                .Range("C4").CurrentRegion.Copy
                wsMain.Cells(lastRow, "C").PasteSpecial xlPasteValues
              End With
              Application.DisplayAlerts = False
                .Close
              Application.DisplayAlerts = True
            End With
         End If
         strDir = Dir
      Loop
    
      Application.ScreenUpdating = True
    End Sub
    Perfect.
    Thank Karedog

  4. #4
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Copying data from multiple files have the same structure

    You are welcome, glad I can help.


    Regards

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Copying data from one excel file and pasting into multiple excel files
    By tonybeo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-26-2014, 06:23 PM
  2. Replies: 11
    Last Post: 02-27-2014, 12:27 PM
  3. Copying data from multiple files into new one file.
    By Algus in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-28-2013, 05:45 AM
  4. Copying data from multiple files in a folder to a master file
    By kjshep in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-22-2012, 12:31 PM
  5. Copying data from multiple files (with mult. sheets) into one file and create a graph
    By Macedonia in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 04-30-2010, 07:37 AM
  6. Copying Data from multiple excel files
    By rugbyfitz in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-10-2008, 04:34 AM
  7. Macro copying info/data in multiple excel files into one summary file.
    By Jskasango in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 04-06-2006, 03:30 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1