• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Copy paste all excel files data in 1 excel sheet

Abhijeet

Active Member
Hi

Please give me macro 1 folder all excel files copy paste data in 1 excel sheet.I have this macro but every files and range to define so i want what ever files those copy paste in 1 workbook and give summery for this which files data copy paste in this workbook
 

Attachments

  • Consolidation Macro Chandoo.xlsm
    20.5 KB · Views: 9
Hi,

Try the below macro

Note: In a new file add the below macro and save the file in the same folder where the other excel files are there to consolidate


Code:
Dim ToBook As String
Dim ToSheet As Worksheet
Dim NumColumns As Integer
Dim ToRow As Long
Dim FromBook As String
Dim FromSheet As Worksheet
Dim FromRow As Long
Dim LastRow As Long
 
Sub FILES_FROM_FOLDER()
 
    Dim msg As String
    Dim title As String
    Dim style As VbMsgBoxStyle
    Dim response As VbMsgBoxResult
   
    msg = "Do you want to Consolidate Excel Macro?"
 
    style = VbMsgBoxStyle.vbYesNo
    title = "Message"
   
    response = MsgBox(msg, style, title)
    If response = vbYes Then
 
Application.Calculation = xlCalculationManual
ChDrive ActiveWorkbook.Path
ChDir ActiveWorkbook.Path
ToBook = ActiveWorkbook.Name
 
Set ToSheet = ActiveSheet
NumColumns = ToSheet.Range("a1").End(xlToRight).Column
ToRow = ToSheet.Range("b65536").End(xlUp).Row
 
If ToRow <> 1 Then
ToSheet.Range(ToSheet.Cells(2, 1), _
ToSheet.Cells(ToRow, NumColumns)).ClearContents
End If
ToRow = 2
 
FromBook = Dir("*.xlsx")
While FromBook <> ""
If FromBook <> ToBook Then
Application.StatusBar = FromBook
Transfer_data
End If
FromBook = Dir
Wend
 
MsgBox ("Done.")
Application.StatusBar = False
Application.Calculation = xlCalculationAutomatic
 
End If
End Sub
 
Private Sub Transfer_data()
Workbooks.Open Filename:=FromBook
For Each FromSheet In Workbooks(FromBook).Worksheets
LastRow = FromSheet.Range("b65536").End(xlUp).Row
 
FromSheet.Range(FromSheet.Cells(1, 1), _
FromSheet.Cells(LastRow, NumColumns)).Copy _
Destination:=ToSheet.Range("A" & ToRow)
 
 
ToRow = ToSheet.Range("b65536").End(xlUp).Row + 1
Next
Workbooks(FromBook).Close savechanges:=False
 
End Sub
 
Back
Top