How to write this code more efficiently..

omairhe

Well-known Member
Joined
Mar 26, 2009
Messages
2,040
Office Version
  1. 2019
Platform
  1. Windows
Hello,

Could someone guide me as to how can I wrtie this code more efficiently without naming each name of the sheet.

VBA Code:
    Sheets(Array("SS", "PQ", "25", "CT", "50", " 100", " 1000", "65", "100 ", "LT ", "SMT", _
        "CD", "PNL")).Copy

Can I type in the code to copy sheets from SS to PNL, without mentioning all the sheets in between? Also if all the names of the sheets are extracted into one sheet named "HSheet" starting from A4 to A17 via formula , could the array be set to look into this range instead of naming every sheet? In both cases using the Array is a must for me.


Thanks and will appreciate.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
What is your current code?

VBA Code:
Sub save_as_workbook()
    Dim arr     As Variant
    Dim lastRow As Long
    With Worksheets("HSheet")
        lastRow = Columns("A").Find("*", , xlValues, , xlByRows, xlPrevious, , , False).Row
        arr = Application.Transpose(.Range("A4:A" & lastRow).Value)
    End With
    Sheets(arr).Copy
End Sub
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,620
Messages
6,120,559
Members
448,970
Latest member
kennimack

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