Help VBA code for Consolidating multiple workbooks with sheet names into a single workbook

vrajesh544

New Member
Joined
Sep 20, 2021
Messages
2
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
I have a multiple workbook data and
need a single workbook (Master file) and also
need workbook names as well. (index workbook name use to separate column in a consolidated data)
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Welcome to MrExcel Message board.
Try Link give by Mark858, if Not satisfied, answer these questions and if possible upload one example sheet with structure of your workbooks with XL2BB Add-in here.

1. how many worksheet at each workbook should be extracted? & what is names?
2. Data at each sheet start form same row & column or Not? if yes, what is first row & column?
3. how many rows consider as heading and at all worksheets is same?
4. you want result at one sheet or each workbook at separate sheet?
 
Upvote 0
Welcome to MrExcel Message board.
Try Link give by Mark858, if Not satisfied, answer these questions and if possible upload one example sheet with structure of your workbooks with XL2BB Add-in here.

1. how many worksheet at each workbook should be extracted? & what is names?
2. Data at each sheet start form same row & column or Not? if yes, what is first row & column?
3. how many rows consider as heading and at all worksheets is same?
4. you want result at one sheet or each workbook at separate sheet?
1. Only one sheet. & (Sheet name as sheet name) example each workbook name as sheet name.
2.same row & last column. Yes, first row is Date & column (w)
3. Only one row is header & at all worksheets is same.
4. Result get all in single workbook.
 
Upvote 0
Thanks for reply but tell exactly:
1. first row number is 1 or ... ?
2. I think you want all result on one sheet?
3. Columns arrangement at all file is same or different (e.g if Same then Title row at all file is Date, name, ID, .... Respectively)
 
Last edited:
Upvote 0
if answer of all question is yes and have same arrangement columns and all excel files at one folder try this:
Note: when you see Select folder window only select folder that have excel file and don't open it
1. Change Red part if Sheet name isn't same with Workbook name, e.g if sheet name at all workbook is Sheet1 Change xStrAWBName to "Sheet1"
2. At Blue part you can change Destination Sheet name, if you want another name change it
Rich (BB code):
Sub ImportFiles3()
Dim xWS As Worksheet, xTWB As Workbook, DestSheet As Worksheet, Head As Range
Dim xStrAWBName As String, FolderName As String, sItem As String, Header As Long
Dim FolderPath As String, fldr As FileDialog, Lr As Long, LCD As Long, R As Long
Dim os As Long, LrS As Long, LCS As Long, FileName As String
On Error Resume Next
Set xTWB = ThisWorkbook
Set DestSheet = xTWB.ActiveSheet
  Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = Application.DefaultFilePath
        If .Show <> -1 Then GoTo NextCode
        sItem = .SelectedItems(1)
    End With
NextCode:
    FolderName = sItem
    Set fldr = Nothing
    FolderPath = FolderName & "\"
FileName = Dir(FolderPath & "*.xls*")
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
Do While FileName <> ""
Workbooks.Open FileName:=FolderPath & FileName, ReadOnly:=True
xStrAWBName = ActiveWorkbook.Name
For Each xWS In ActiveWorkbook.Sheets
If xWS.Name = xStrAWBName Then
Lr = DestSheet.Range("A" & Rows.Count).End(xlUp).Row
LrS = xWS.Range("A" & Rows.Count).End(xlUp).Row
LCS = xWS.Cells(1, Columns.Count).End(xlToLeft).Column + 1
LCD = DestSheet.Cells(1, Columns.Count).End(xlToLeft).Column + 1
If Lr = 1 Then
Range(DestSheet.Cells(1, 2), DestSheet.Cells(Lr, LCS)).Value = Range(xWS.Cells(1, 1), xWS.Cells(Lr, LCS - 1)).Value
DestSheet.Range("A1").Value = "FileName"
DestSheet.Range("A2:A" & Lr).Value = xStrAWBName
Else
Range(DestSheet.Cells(Lr + 1, 2), DestSheet.Cells(Lr + LrS - 1, LCS)).Value = Range(xWS.Cells(2, 1), xWS.Cells(Lr, LCS - 1)).Value
DestSheet.Range("A" & Lr + 1 & ":A" & Lr + LrS - 1).Value = xStrAWBName
End If
End If
Next xWS
Workbooks(xStrAWBName).Close
FileName = Dir()
Loop
DestSheet.Activate
DestSheet.Name = "Consolidate"
xTWB.Save
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,959
Messages
6,122,476
Members
449,087
Latest member
RExcelSearch

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top