VBA: Combine data from Multiple Workbooks Into One File and Consolidate into one Tab

unknownymous

Board Regular
Joined
Sep 19, 2017
Messages
249
Office Version
  1. 2016
Platform
  1. Windows
Hello Gurus,

Good day!

Could you possibly help me tweak below code? I have multiple workbooks and need to combine into one.

I have below code that can do it. However, I am getting an error message whenever there is same tab name that exists on different workbook. Note that the tab name most of the time maximize the number of letters applicable for tab names.

Example Tab Name:
Workbook 1 - ABDC0123456789_Subject123456781
Workbook 2 - ABDC0123456789_Subject123456781

Is it possible to shorten the tab name until 25 letters only so it will look like: First tab is ABDC0123456789_Subject123 then next is ABDC0123456789_Subject123 (2) and so on.

= = = = = = = = = = = =

Sub ConsolidateFiles()

Dim f, fpath As String
fpath = GetFolder & "\"
Application.ScreenUpdating = False
f = Dir(fpath)

Do While Len(f) > 0
Select Case Right(f, Len(f) - InStrRev(f, "."))
Case "xls", "xlsx", "csv"
OpenFile (fpath & f)
End Select
f = Dir
Loop

Sheets("Main").Activate

End Sub

Function GetFolder() As String
Dim f As String
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = ThisWorkbook.Path
If .Show <> -1 Then GoTo There
f = .SelectedItems(1)
End With
There:
GetFolder = f
End Function

Private Sub OpenFile(filepath)
Dim sh, wb As Workbook, wbMe As Workbook
Set wbMe = ActiveWorkbook
Set wb = Workbooks.Open(filepath)
For Each sh In wb.Sheets
sh.Copy After:=wbMe.Sheets(wbMe.Sheets.Count)
Next sh
wb.Close False
End Sub

= = = = = = = = = = = =

Any thoughts will be much appreciated. :)
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Anything is possible.

The sheet name should clearly indicate what data is contained in the sheet.

Is there a meaning to all of the characters and their relative positions in the ABDC0123456789_Subject123 string?

Work out a convention and then determine how to convert the sheet name from what it is now to what it needs to be in the future.
Once you have done this you can develop a piece of code to apply the conversion routine. I can help in this respect.

It is not a good idea to use the suffix (2) for example. It gives off the message that the sheets contain duplicate data which they may not.
Excel does this but it does not know what else to do.
 
Upvote 0
I
Anything is possible.

The sheet name should clearly indicate what data is contained in the sheet.

Is there a meaning to all of the characters and their relative positions in the ABDC0123456789_Subject123 string?

Work out a convention and then determine how to convert the sheet name from what it is now to what it needs to be in the future.
Once you have done this you can develop a piece of code to apply the conversion routine. I can help in this respect.

It is not a good idea to use the suffix (2) for example. It gives off the message that the sheets contain duplicate data which they may not.
Excel does this but it does not know what else to do.
I see. Do you have any recommendation? The workbook name and enclosed data is different from one another but the tab name is same for some workbook. I always get an error saying "the name is already taken" so it won't push through. :(
 
Upvote 0
If the sheet name is the same as an existing sheet name in the target workbook then Excel should use the (x) suffix to maintain the unique sheet name rule.

So... Copy the sheet and then it becomes the active sheet in the new workbook.

Just rename the active sheet as ni the sample VBA code below.

It's the sheet naming convention which is the key to this rather than the mechanics.

VBA Code:
Workbooks("CopyFromWorkbook.xlsm").Sheets("Data 3").Copy After:=Workbooks("CopyToWorkbook.xlsm").Sheets(Workbooks("CopyToWorkbook.xlsm").Sheets.Count)

    ActiveSheet.Name = "NewWorksheetName"
 
Upvote 0
Hello Herakles. Appreciate your help on this but a small favor, can you help me integrate the additional code on the existing one that I have as stated above.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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