Copy worksheets to another workbook based on a range

borkybork

New Member
Joined
Aug 5, 2022
Messages
18
Office Version
  1. 365
Platform
  1. Windows
Hi! I have this code to copy multiple worksheets to another workbook :

VBA Code:
Sub Copy_to_Another_Multiple()
Dim Source As String
Dim Destination As String

Source = “Filename1.xlsx”
Destination = “Filename2.xlsx”

Dim Worksheets As Variant
ReDim Worksheets (3)

Worksheet(1) = “ABC”
Worksheet(2) = “DEF”
Worksheet(3) = “GHI”

Dim i as Variant
For i = 1 To UBound (Worksheets)
     Workbooks(Source).Sheets(Worksheets(i)). Copy
           before:=Workbooks(Destination).Sheets(“JKL”)
Is there a way to make this more dynamic and allow you to list and select the worksheet names from a range? The conditions are:

1. The worksheet names change in every source file, and the order matters: ABC, DEF, then GHI for example. Was thinking having the list might help with this?
2. All destination files have a worksheet called “JKL” and the 3 copied worksheets need to come before it. JKL is the first worksheet in the destination file if that helps.
2. The source and destination file names always change.
3. This macro will be in a separate file where we can select the range from.

Thanks in advance!
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi,
see if this update to your code will do what you want

VBA Code:
Sub Copy_to_Another_Multiple()
    Dim wbSource        As Workbook, wbDestination As Workbook
    Dim wsNames         As Worksheet
    Dim SheetNames      As Variant, wsName As Variant
   
   
    Set wbSource = Workbooks("Filename1.xlsx")
    Set wbDestination = Workbooks("Filename2.xlsx")
   
    Set wsNames = ThisWorkbook.Worksheets("Sheet Names")
   
    SheetNames = wsNames.Range("A1").CurrentRegion.Value
   
    For Each wsName In SheetNames
       wbSource.Worksheets(wsName).Copy before:=wbDestination.Worksheets("JKL")
    Next wsName
   
End Sub

Code dynamically reads from a worksheet named Sheet Names

02-09-2022.xls
ABC
1Sheet1
2Sheet2
3Sheet3
4Sheet4
5Sheet5
6Sheet6
Sheet Names


Dave
 
Upvote 0
Solution
Hi,
see if this update to your code will do what you want

VBA Code:
Sub Copy_to_Another_Multiple()
    Dim wbSource        As Workbook, wbDestination As Workbook
    Dim wsNames         As Worksheet
    Dim SheetNames      As Variant, wsName As Variant
  
  
    Set wbSource = Workbooks("Filename1.xlsx")
    Set wbDestination = Workbooks("Filename2.xlsx")
  
    Set wsNames = ThisWorkbook.Worksheets("Sheet Names")
  
    SheetNames = wsNames.Range("A1").CurrentRegion.Value
  
    For Each wsName In SheetNames
       wbSource.Worksheets(wsName).Copy before:=wbDestination.Worksheets("JKL")
    Next wsName
  
End Sub

Code dynamically reads from a worksheet named Sheet Names

02-09-2022.xls
ABC
1Sheet1
2Sheet2
3Sheet3
4Sheet4
5Sheet5
6Sheet6
Sheet Names


Dave
Hi! Thanks for your response! It works perfectly!!
 
Upvote 0

Forum statistics

Threads
1,215,016
Messages
6,122,700
Members
449,092
Latest member
snoom82

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