anshul.malhotra
Member
Hi All,
I am looking for help as I am not a pro in VBA but have googled many code and fix my task, however this it is not helping me;
I want to merge 184 excel workbook, where each workbook has 3-5 worksheet, with Workbook name stamp in last column and Sheet tab name in next column following workbook name. But the below code is working on only one worksheet not all and also I am not able to add worksheet name from where the data has been copied from.
below is the code I am using
Thanks in advance
Anshul
I am looking for help as I am not a pro in VBA but have googled many code and fix my task, however this it is not helping me;
I want to merge 184 excel workbook, where each workbook has 3-5 worksheet, with Workbook name stamp in last column and Sheet tab name in next column following workbook name. But the below code is working on only one worksheet not all and also I am not able to add worksheet name from where the data has been copied from.
below is the code I am using
Code:
Sub Merge_with_Col_added()
Dim fn, ws As Worksheet, e, flg As Boolean, LastR As Range, wsName As String
fn = Application.GetOpenFilename("Excel(*.xls*),*.xls*", MultiSelect:=True)
If Not IsArray(fn) Then Exit Sub
Set ws = ActiveWorkbook.Sheets.Add
ws.Name = "Combined"
Set ws = ActiveWorkbook.Sheets("Combined")
Application.ScreenUpdating = False
ws.Cells.Clear
For Each e In fn
With Workbooks.Open(e)
With .Sheets(1)
wsName = .Name
If Not flg Then
.Rows(1).Copy ws.Cells(1)
ws.Columns(1).Insert
ws.Cells(1).Value = "Sheet name"
flg = True
End If
Set LastR = ws.Cells(Rows.Count, 2).End(xlUp)(2)
With .Range("a1").CurrentRegion
With .Resize(.Rows.Count - 1).Offset(1)
.Copy LastR
LastR(, 0).Resize(.Rows.Count).Value = CreateObject("Scripting.FileSystemObject").GetBasename(e)
End With
End With
End With
.Close False
End With
Next
ws.Range("a1").CurrentRegion.Columns.AutoFit
Application.ScreenUpdating = True
Set ws = Nothing
End Sub
Thanks in advance
Anshul
Last edited by a moderator: