Sameer.k21
Member
Hi All,
here I am again with new issue.
I have a code which needs some correction/advice.
I have multiple workbooks & in them some have 3 sheets, some 4 and some 7 sheets. The code loops through only through the 1st sheet of each book and copies even the headers.
headers are same for all the sheets and workbooks, the data changes. I am trying a code to run, loop through the sheets in each WB, copy data from A2 till the last colum and row. Go to new created wb and dump everything in sheet1 ONLY.
here I am again with new issue.
I have a code which needs some correction/advice.
I have multiple workbooks & in them some have 3 sheets, some 4 and some 7 sheets. The code loops through only through the 1st sheet of each book and copies even the headers.
Code:
Sub LoopThroughFiles()
Dim intChoice As Integer
Dim strPath, fname As String
Dim MyObj As Object, MySource As Object, file As Variant
file = Dir("C:\Desktop\My Documents")
Range("A2").Select
Workbooks.Add
ChDir "C:\Desktop"
ActiveWorkbook.SaveAs filename:="C:\Desktop\Consolidated.xlsx"
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
While (file <> "")
Workbooks("Consolidated.xlsx").Activate
Workbooks.Open filename:=file
ActiveWorkbook.Activate
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Workbooks("Consolidated.xlsx").Activate
Range("A2").Select
If Range("A2").Value = "" Then
ActiveSheet.Paste
Else
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
End If
Workbooks.Open filename:=file
ActiveWorkbook.Close
file = Dir
Wend
End Sub
headers are same for all the sheets and workbooks, the data changes. I am trying a code to run, loop through the sheets in each WB, copy data from A2 till the last colum and row. Go to new created wb and dump everything in sheet1 ONLY.