Write Code to Open Other WOrkbooks

Justinian

Well-known Member
Joined
Aug 9, 2009
Messages
1,557
Office Version
  1. 365
Platform
  1. Windows
How do I write code to open and insert eight workbooks into one main active workbook? The individual eight workbooks are named 1-8.xls.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Run this in a sub from your "main" workbook:

Code:
For i = 1 To 8

Set OtherWB = Workbooks.Open("C:\Folder\SubFolder\" & i & ".xls") '// Change path as required
   For Each WS in OtherWB.Sheets
      WS.Copy After:=ThisWorkbook.Sheets(Sheets.Count)
   Next WS
   OtherWB.Close False
Set OtherWB = Nothing

Next i
 
Upvote 0
I got a run time error that highlighted this line:

Set OtherWB = Workbooks.Open("C:\Users\justin\Desktop" & i & ".xls")
 
Upvote 0
How silly of me. Ok, I added the | after desktop but got another subscript error highlighting this line:

WS.Copy After:=ThisWorkbook.Sheets(Sheets.Count)
 
Upvote 0
my bad - active workbook will be different at that point in the code.

Try amending to this:

Code:
[COLOR=#333333]WS.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)[/COLOR]
 
Upvote 0
Now I am getting a 1004 run-time rror saying I cannot rename a sheet to the same name as another sheet.
 
Upvote 0
it would appear that you have sheets with the same name in your workbook(s) - Seeing as Excel doesn't allow worksheets with the same name in a single workbook, you need to decide how you want to tackle this issue...
 
Upvote 0
Well, the sheets I import from the other workbooks have the same name but I rename name after I add them. So I add the sheet, then rename it 1, 2, all the way through 8. How do I incorporate the renaming of the sheets? Here is my code:

Sub Macro1()
'
'
Workbooks.Open Filename:= _
"C:\Users\justin\Desktop\1.xls"
Sheets("Cyber Rain Controller Events").Select
Sheets("Cyber Rain Controller Events").Move Before:=Workbooks( _
"Cyber-Rain Macro.xlsm").Sheets(1)
Sheets("Cyber Rain Controller Events").Select
Sheets("Cyber Rain Controller Events").Name = "1"
Workbooks("1.xls").Close SaveChanges:=False
End Sub

I know this will insert a sheet and rename it so i guess my question is how to loop it?
 
Upvote 0
Just out of curiosity - try this:

Code:
For i = 1 To 8
   With CreateObject("C:\Users\justin\Desktop\" & i & ".xls").Sheets("Cyber Rain Controller Event")
        .Name = i
        .Copy Before:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
   End With
Next i

If that doesn't work, then try this:

Code:
For i = 1 To 8


Set OtherWB = Workbooks.Open("C:\Users\justin\Desktop\" & i & ".xls")
   With OtherWB.Sheets("Cyber Rain Controller Events")
        .Name = i
        .Copy Before:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
   End With
   OtherWB.Close False
Set OtherWB = Nothing


Next i
 
Upvote 0

Forum statistics

Threads
1,215,024
Messages
6,122,729
Members
449,093
Latest member
Mnur

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