Sorting worksheets in a workbook

davidtaylor598

Board Regular
Joined
Oct 5, 2010
Messages
84
Hi all,

I have made a macro that copies worksheets from a selection of workbooks, inputs them in the original Excel file, and then sorts them in a specified order using the code below:

Code:
Sheets("Valuation Summary").Move After:=Sheets(1)
Sheets("Assets And Liabilities").Move After:=Sheets(2)
Sheets("Foreign Currency").Move After:=Sheets(3)
Sheets("B1").Move After:=Sheets(4)
Sheets("B2").Move After:=Sheets(5)
Sheets("C2").Move After:=Sheets(6)

etc.
This is quite limiting as if just one of the worksheets defined above does not exist then the macro falls down.

I would like to include an IF statement (I think) so that if one of the tabs in the code above is not in the workbook then it will carry on with the rest of the macro and then come up with a message at the end saying something like 'Worksheet XX not copied over as it does not exist in your selection'...

I am also looking to include something that will let me sort the sheets even if the name of the sheet is slightly off (ie maybe spelled incorrectly). Say I have the code to sort the sheet called 'Foreign Currency', but the sheet that has been imported actually has the name 'Foreign_Currency', how would I adapt my code - using the LIKE function? Another example - my code states 'Securities Purchased Payable' but the sheet that has been imported has the name 'Sec_Purch_Pay'...


Any other ways you think I could improve this macro?

If you would like to see the whole code let me know and i'll put it up.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Forum statistics

Threads
1,225,071
Messages
6,182,687
Members
453,132
Latest member
nsnodgrass73

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