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!
 

Smitty

MrExcel MVP
Joined
May 15, 2003
Messages
29,536
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,
 

hfler

Board Regular
Joined
Jun 10, 2008
Messages
95
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?
 

Forum statistics

Threads
1,085,540
Messages
5,384,317
Members
401,887
Latest member
Somesh

Some videos you may like

This Week's Hot Topics

Top