VBA Help needed

vik2slick

New Member
Joined
Dec 28, 2017
Messages
23
Hi,

I am trying to sort/group my excel worksheets based on a customized array.

I currently used this below VBA that I found on this site, but it didn't work. The range is in cells A206:A340 in a sheet named 'Cost Center Summary'

VBA used:

Sub SortWS2()
Dim SortOrder As Variant
Dim Ndx As Long
With Worksheets("Cost Center Summary").Range("A206:A340")
For Ndx = .Cells.Count To 1 Step -1
Worksheets(.Cells(Ndx).Value).Move before:=Worksheets(1)
Next Ndx
End With
End Sub



Please help as this VBA didn't work.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
You haven't told us what "didn't work" means?

If A206:A340 contains a list of worksheet names, then the macro should shuffle the worksheets in this order to the start of the workbook.

But one possible way the code won't work is if your sheet names are numbers, e.g. my default sheet names are 1, 2, 3 etc ...

If so, this code will need adjusting, because there is a difference between Worksheets(1) and Worksheets("1").
 
Upvote 0
At a guess... I think this is what you're after:

Code:
Sub SortWS2()
'// Move worksheets to align with order shown in `Cost Center Summary` Sheet
    Dim i As Long
    Dim wsCost As Worksheet
    
    Set wsCost = Worksheets("Cost Center Summary")
    For i = 340 To 206 Step -1
        Worksheets(wsCost.Range("A" & i).Value).Move before:=ActiveWorkbook.Sheets(1)
    Next i
End Sub

Let me know if thats what you wanted
Thanks
Caleeco
 
Upvote 0
Stephen,

Yes, my worksheets are 6 digit numbers and so when I ran the above macro, it didn't seem to "work" meaning the workbook didn't get sorted but instead came back with an error message. Since the worksheets are #'s, will Worksheets("1") do the trick?
 
Upvote 0
Hello,

On which line?
What is the sheet name with the list of other sheet names? is it "Cost Center Summary"?

Thanks
Caleeco
 
Upvote 0
Yes, my worksheets are 6 digit numbers .....

Yes, the problem looks like your numeric sheet names ...

Worksheets(123456) refers to the 123,456th worksheet, which will cause an Error 9, subscript out of range error.

You actually want Worksheets("123456").

Try changing:

Worksheets(CStr(.Cells(Ndx).Value)).Move before:=Worksheets(1)
 
Upvote 0
Stephen,

Still received Run-time error '9': Subscript out of range

For the code to work as written/amended, every value in 'Cost Center Summary'!A206:A340 must be a valid worksheet name. This also means no blanks.

If you're only listing worksheets from A206:A300, say, then you'll need to start looping backwards from row 300, rather than row 340.

Perhaps easiest if you check the size of the range dynamically using VBA. Let me know if you need help with this?
 
Upvote 0

Forum statistics

Threads
1,216,106
Messages
6,128,863
Members
449,473
Latest member
soumyahalder4

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