VBA select all sheets containing "-DR"

hfler

Board Regular
Joined
Jun 10, 2008
Messages
95
Hi,

How do I write a macro that will select all sheets containing the test "-DR"? Hopefully someone can also tell me how to select all pivot tables in those sheets once I've done this...

Thanks!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
This will do something to each sheet ending in "DR". What you want to do is up to you, but note that you generally don't need to select objects in order to work with them.

<font face=Calibri><SPAN style="color:#00007F">Sub</SPAN> foo()<br>    <SPAN style="color:#00007F">Dim</SPAN> ws <SPAN style="color:#00007F">As</SPAN> Worksheet<br>        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> ws <SPAN style="color:#00007F">In</SPAN> ActiveWorkbook.Worksheets<br>            <SPAN style="color:#00007F">If</SPAN> Right(ws.Name, 2) = "DR" <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#007F00">'  Do something here</SPAN><br>        <SPAN style="color:#00007F">Next</SPAN> ws<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

HTH,
 
Upvote 0
Thanks, but I'm fairly inept with Excel, and here's what I've tried to do: the formula runs without any errors, but it isn't working. I'm trying to select all pivot tables on the worksheets containing "-DR" in the title and reset their data range to the entire "Database" worksheet. Here's the code I tried to insert:

Code:
Sub Update()
    Dim ws As Worksheet
        For Each ws In ActiveWorkbook.Worksheets
            If Right(ws.Name, 2) = "DR" Then Activate.PivotTableWizard SourceType:=xlDatabase, SourceData:= _
        "'Direct Database'!R1C1:R60000C150"
        Next ws
    
End Sub

Any advice?
 
Upvote 0

Forum statistics

Threads
1,214,659
Messages
6,120,784
Members
448,992
Latest member
prabhuk279

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