VBA name within range to select worksheet

EMcK01

Board Regular
Joined
Jun 14, 2015
Messages
125
Hi,

I'm sure its something obvious but can't see it.

I want my code to select the sheet to use to create a Pivot Table based on the name thats in the Read Me worksheet, as this sheet name will vary.

The below works fine as a defined name

VBA Code:
    Dim mySourceWorksheet As Worksheet
    Dim myDestinationWorksheet As Worksheet
    Dim myPivotCache As PivotCache
    Dim myPivotTable As PivotTable
    
    'declare variables to hold source and destination range
    Dim mySourceRange As Range
    Dim myDestinationRange As Range
 
    'identify source and destination worksheets. Add destination worksheet
    With ThisWorkbook
        Set mySourceWorksheet = .Worksheets("Sheet1")
        Set myDestinationWorksheet = .Worksheets.Add(After:=.Sheets(.Sheets.Count))
        myDestinationWorksheet.Name = "Temp"
    End With

I have tried changing

Code:
Set mySourceWorksheet = .Worksheets("Sheet1")

to

Code:
Set mySourceWorksheet = Sheets("READ ME").Range("B5")

but am getting a Type Mismatch
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try
VBA Code:
Set mySourceWorksheet = Sheets(Sheets("READ ME").Range("B5").Value)
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
Fluff kindly helped and solved my issue last week, however I've realised that I want to create the pivot table for multiple sheets in the workbook.

I've listed the sheet names in column P on my "read me" sheet and was intending to loop through the list to create a pivot table for the sheets listed.

I tried changing the line
VBA Code:
Set mySourceWorksheet = Sheets(Sheets("READ ME").Range("B5").Value)
to
Code:
Set mySourceWorksheet = Sheets(Sheets("READ ME").Range(Cells(c,16)).Value)
but I'm now getting a run time error message.

any suggestions?
 
Upvote 0
Try
Excel Formula:
Set mySourceWorksheet = Sheets(Sheets("READ ME").Range("P"&c).Value)
 
Upvote 0
Solution
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

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