unknownymous
Board Regular
- Joined
- Sep 19, 2017
- Messages
- 249
- Office Version
- 2016
- Platform
- 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.
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.