Group sheets without defined array?

sweeneytime

Board Regular
Joined
Aug 23, 2010
Messages
183
Hi all,

Trying to group sheets with a macro.

I was using an array with the sheet names defined. But I want the code to be able to select new sheets if added. Selecting sheets based on data in "A5".

I adapted code that looped and selected sheets correctly. But it won't group, just selects each sheet individually and moves on.

Any suggestions?

Thanks,
Alan

PHP:
Sub SelectCC()
    Dim ws As Worksheet
    
    'Select sheets with "INCOME" in A5
    'Should only be the Cost Centres
    For Each ws In ActiveWorkbook.Worksheets
            If ws.Range("A5").Text = ("INCOME") Then
                ws.Select
            End If
    Next ws
    

End Sub
 

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
I'll try.

The Replace parameter is Boolean (True or False) with the default being True.
It determines whether to replace the existing sheet selection (if True) or not (if False). If not, then it adds to the existing selection.

The problem was, by omitting the parameter it was using the default value of True and so every time you came to a new sheet that met your criteria, the sheet was selected but the previous one was de-selected.

When I Dim the boolean bStarted, it takes the default value of False. So the first sheet I come to that meets the criteria
Replace:=Not bStarted is really
Replace:=Not False
Replace:=True

This ensures that if your code starts on a sheet that does not meet your criteria, it will be de-selected. If you do start on a sheet that meets your criteria it will also be de-selected but it replaced in the selection by itself - so no problem.

Having done this first sheet, I set bStarted to True so that from then on
Replace:=Not bStarted
Replace:=Not True
Replace:=False

so the sheet selection just gets added to.

Hope that made some sense. :)


Thanks Peter.

i was not aware about this property. I learned a new property of VBA today.also, in the meantime i checked below link.
http://www.tushar-mehta.com/excel/vba/multiple_sheets.htm

Many Thanks
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,952
Members
449,095
Latest member
nmaske

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