Use variable name in array to move sheets

bwgreg

New Member
Joined
Feb 5, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I have a macro that moves sheets to a new workbook. However, the sheet names are hard coded into the code. For example:
WB.Sheets(Array("Al", "MS", "TX")).Move
WB.Sheets(Array("GA", "WV")).Move
Looking for a way to assign the sheet names to a variable so I can loop through the code vs. typing a unique line for all sheets.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Does this help?
VBA Code:
Sub test()
    Dim v As Variant
    v = Array("Sheet1", "Sheet2", "Sheet3")
    Sheets(v).Move
End Sub
 
Upvote 0
Thanks---
I'm looking to pull the array from cell names contained inside an excel sheet.
For example, I'd like to be able to enter the sheet names in excel and then have the macro move those sheets . One day it may be 3 sheets to move the next day 5 sheets all depending on what the user types in.
 
Upvote 0
How about:
VBA Code:
Sub test()
    Dim v As Variant
    v = Array(Range("A1").Value, Range("A2").Value, Range("A3").Value)
    Sheets(v).Copy
End Sub
The most efficient way would be to create a userform which lists all the sheets in your workbook with a check mark beside each name. You would simply select the desired sheets and click a button to perform the action. Are you interested in this approach?
 
Upvote 0
You are very welcome. :) Are you interested in the approach I suggested?
 
Upvote 0
How about:
VBA Code:
Sub test()
    Dim v As Variant
    v = Array(Range("A1").Value, Range("A2").Value, Range("A3").Value)
    Sheets(v).Copy
End Sub
The most efficient way would be to create a userform which lists all the sheets in your workbook with a check mark beside each name. You would simply select the desired sheets and click a button to perform the action. Are you interested in this approach?
This line from your code...
VBA Code:
v = Array(Range("A1").Value, Range("A2").Value, Range("A3").Value)
can be replaced by the following one making the selection of the cells with sheet names in them automatic...
VBA Code:
v = Application.Transpose(Range("A1", Cells(Rows.Count, "A").End(xlUp)).Value)
 
Upvote 0
That Last post is exactly what I'm trying to do..... :)
I can now reduce my 100 line code and make the file MUCH more user friendly....
 
Upvote 0

Forum statistics

Threads
1,213,492
Messages
6,113,967
Members
448,537
Latest member
Et_Cetera

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