VBA Pass string variable to refer to worksheet

szita2000

Board Regular
Joined
Apr 25, 2012
Messages
101
Office Version
  1. 365
Platform
  1. Windows
Hi All.

I am trying to refer to a worksheet by a string which is value of a cell.

This is my code for this part:

Code:
    month = Sheet15.Range("K1").Value                           'This is the string with the name of the worksheet (User selectable)
    Set y = Worksheets("month")                                   'Here I want to set which sheet we will run the code on
    brlastrow = y.Range("A" & Rows.Count).End(xlUp).Row         'here I want to find the lastrow on that sheet
    Set bigrange = y.Range("C2:C" & brlastrow)                  'Here I would set the range for my loop

On the Set y row it craps out with subscript out of range error.

How can I set y to the name of the worksheet that is month?
I was going with the same syntax we use for referring to named ranges. Am I missing something?

Here are my Variables

Dim month As String
Dim y As Worksheet
Dim brlastrow As Integer
Dim bigrange As Range
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Remove the " from month, otherwise the macro thinks you are looking for a sheet called "month"
 
Upvote 0
Thanks Fluff!

This helped.
Admittedly it was a rookie mistake
"Need a brew....." :laugh:
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,975
Messages
6,175,749
Members
452,667
Latest member
vanessavalentino83

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