VBA - Setting Workbook Names After Opening Multiple Workbooks

beartooth91

Board Regular
Joined
Dec 15, 2024
Messages
64
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
The below vba works.

VBA Code:
Sub Open_WB()
' Opens each workbook in the Standard-Format IO Lists subfolder
Dim SfFolder As String, SfList As String
SfFolder = Dir(ThisWorkbook.Path & "/Standard-Format IO Lists")
SfList = Dir(ThisWorkbook.Path & "/Standard-Format IO Lists" & "\*.xlsx")

Do While SfList <> ""
  Workbooks.Open FileName:=ThisWorkbook.Path & "/Standard-Format IO Lists" & "\" & SfList
  SfList = Dir
  
Loop
End Sub
The result is I have 6+ open workbooks that I want to copy out of. Is there a **simple** vba way to reference the open workbooks, either by number or name? The number of workbooks - which I will have to routinely copy out of - dynamically grows and shrinks, but will probably have 30+ workbooks to copy out of as this project moves along.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
[Edit] It occurs to me the better way to do this - which I need help with - is to open/copy out of/close one workbook at a time before moving to the next......
How can I refer to each open workbook name without hard coding?

VBA Code:
Sub Open_WB()
' Opens each workbook in the Standard-Format IO Lists subfolder
Dim SfFolder As String, SfList As String
SfFolder = Dir(ThisWorkbook.Path & "/Standard-Format IO Lists")
SfList = Dir(ThisWorkbook.Path & "/Standard-Format IO Lists" & "\*.xlsx")

Do While SfList <> ""
  Workbooks.Open FileName:=ThisWorkbook.Path & "/Standard-Format IO Lists" & "\" & SfList
  SfList = Dir

' vba to copy out of this workbook...how to reference/refer to the open workbook without hard coding?
'vba to close workbook

Loop
End Sub
 
Upvote 0
Untested here. Advise if this works.

VBA Code:
Sub Open_WB()
    ' Opens each workbook in the Standard-Format IO Lists subfolder
    Dim SfFolder As String, SfList As String
    Dim OpenedWorkbook As Workbook
    
    ' Define the folder and the file list pattern
    SfFolder = ThisWorkbook.Path & "\Standard-Format IO Lists"
    SfList = Dir(SfFolder & "\*.xlsx")     '<-- change file type as required
    
    ' Loop through all files in the folder
    Do While SfList <> ""
        ' Open the workbook and set a reference to it
        Set OpenedWorkbook = Workbooks.Open(FileName:=SfFolder & "\" & SfList)
        
        ' Perform actions on the opened workbook
        ' For example, copying a sheet:
        OpenedWorkbook.Sheets(1).Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
        
        ' Close the workbook (save changes if needed)
        OpenedWorkbook.Close SaveChanges:=False
        
        ' Move to the next file
        SfList = Dir
    Loop
End Sub
 
Upvote 0
Solution
Untested here. Advise if this works.

VBA Code:
Sub Open_WB()
    ' Opens each workbook in the Standard-Format IO Lists subfolder
    Dim SfFolder As String, SfList As String
    Dim OpenedWorkbook As Workbook
   
    ' Define the folder and the file list pattern
    SfFolder = ThisWorkbook.Path & "\Standard-Format IO Lists"
    SfList = Dir(SfFolder & "\*.xlsx")     '<-- change file type as required
   
    ' Loop through all files in the folder
    Do While SfList <> ""
        ' Open the workbook and set a reference to it
        Set OpenedWorkbook = Workbooks.Open(FileName:=SfFolder & "\" & SfList)
       
        ' Perform actions on the opened workbook
        ' For example, copying a sheet:
        OpenedWorkbook.Sheets(1).Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
       
        ' Close the workbook (save changes if needed)
        OpenedWorkbook.Close SaveChanges:=False
       
        ' Move to the next file
        SfList = Dir
    Loop
End Sub
Tested and works. Thanks!
 
Upvote 0
Wonderful ! Glad you have an answer.
 
Upvote 0

Forum statistics

Threads
1,225,610
Messages
6,185,986
Members
453,333
Latest member
BioCoder84

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