Hello,
I tried to do what I have to do to fulfill what the subject specified but not quite satisfied with what I got.
I have about 5 workbooks for each model. I was asked to copy all the tabs in each book and place them in a single workbook. However, we have 10 to 20% of the tabs have the same tab name before they are put together, which is not allowed to coexist in one workbook.
I set up an utility procedure to list all the unique tab name and the duplicated tab name as well, which helps me rename the tab name in the process and make them all unique. Unfortunately life is not that easy. Because those input workbooks are kept updating, i.e., the rename part of the process works now but may not work next week or the week after.
My question is if we can set up a WHILE/WEND loop to keep checking and renaming the tabs 'on the fly' or dynamically, instead of checking the dups first with the utility macro and then renaming the tabs with IF/THEN or SELECT statement.
thanks in advance.
The following is the code I used to test it. Even it's not working, hope it can help as a reference.
Sub CombineMultiBooksMultiTabs()
'Application.ScreenUpdating = False
Dim strPath As String
Dim oFSO As Object
Dim oFile As Object
Dim oFolder As Object
Dim obook As Object
strPath = SourceFolder
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(strPath)
For Each oFile In oFolder.Files
If oFile.Name Like "*.xlsx" Then
Set obook = Workbooks.Open(oFile)
For Each st In obook.Worksheets
If Len(st.Name) > 26 Then st.Name = Mid(st.Name, 1, 26)
Set nusheet = ThisWorkbook.Worksheets.Add
nusheet.Name = st.Name
x = 1
While st.Name = nusheet.Name
st.UsedRange.Copy ThisWorkbook.Worksheets(nusheet.Name).Cells(1, 1)
If Err = 0 Then
nusheet.Name = st.Name & "_" & x
Else: nusheet.Name = st.Name & "_" & x * 2
End If
' MsgBox st.Name & "*" & nusheet.Name
x = x + 1
Wend
Next
End If
obook.Close SaveChanges:=False
Next
Set oFSO = Nothing
Set oFile = Nothing
Set oFolder = Nothing
'Applicaton.ScreenUpdating = True
End Sub
I tried to do what I have to do to fulfill what the subject specified but not quite satisfied with what I got.
I have about 5 workbooks for each model. I was asked to copy all the tabs in each book and place them in a single workbook. However, we have 10 to 20% of the tabs have the same tab name before they are put together, which is not allowed to coexist in one workbook.
I set up an utility procedure to list all the unique tab name and the duplicated tab name as well, which helps me rename the tab name in the process and make them all unique. Unfortunately life is not that easy. Because those input workbooks are kept updating, i.e., the rename part of the process works now but may not work next week or the week after.
My question is if we can set up a WHILE/WEND loop to keep checking and renaming the tabs 'on the fly' or dynamically, instead of checking the dups first with the utility macro and then renaming the tabs with IF/THEN or SELECT statement.
thanks in advance.
The following is the code I used to test it. Even it's not working, hope it can help as a reference.
Sub CombineMultiBooksMultiTabs()
'Application.ScreenUpdating = False
Dim strPath As String
Dim oFSO As Object
Dim oFile As Object
Dim oFolder As Object
Dim obook As Object
strPath = SourceFolder
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(strPath)
For Each oFile In oFolder.Files
If oFile.Name Like "*.xlsx" Then
Set obook = Workbooks.Open(oFile)
For Each st In obook.Worksheets
If Len(st.Name) > 26 Then st.Name = Mid(st.Name, 1, 26)
Set nusheet = ThisWorkbook.Worksheets.Add
nusheet.Name = st.Name
x = 1
While st.Name = nusheet.Name
st.UsedRange.Copy ThisWorkbook.Worksheets(nusheet.Name).Cells(1, 1)
If Err = 0 Then
nusheet.Name = st.Name & "_" & x
Else: nusheet.Name = st.Name & "_" & x * 2
End If
' MsgBox st.Name & "*" & nusheet.Name
x = x + 1
Wend
Next
End If
obook.Close SaveChanges:=False
Next
Set oFSO = Nothing
Set oFile = Nothing
Set oFolder = Nothing
'Applicaton.ScreenUpdating = True
End Sub