ThisWorkbook.Sheets with variable

BobtBuilder

New Member
Joined
Sep 1, 2023
Messages
45
Office Version
  1. 365
Platform
  1. Windows
Hi folks,
I am trying to create a generic code that could use the sheet name, which is stored in a cell.
Dim wsName As String
Dim ws As Worksheet
Dim targetCell As Range

' Set the target cell (N3 in this case) for the active sheet
Set targetCell = ActiveSheet.Range("N3")

' Get the value from the target cell
wsName = targetCell.Value

Set ws = ThisWorkbook.Sheets("wsName") ' Here is where I would like the name of the sheet to be, (wsname is correct) but cannot set ws, keep getting subscript out of range, but yet wsname is correct.

Please advise
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Set ws = ThisWorkbook.Sheets(wsName)

wsName is a variable so you don't need the quotes around it
 
Upvote 0
Solution
So, are you sure that Activesheet is the sheet you want where the N3 stores a name of a sheet? Did you try to Debug the code one step at at time using F8? When you get to the line
wsName = targetCell.Value
Hover over targetcell.value and see what is store in the variable
 
Upvote 0
So, are you sure that Activesheet is the sheet you want where the N3 stores a name of a sheet? Did you try to Debug the code one step at at time using F8? When you get to the line
wsName = targetCell.Value
Hover over targetcell.value and see what is store in the variable
the targetcell and wsname are correct it seems the ws is not being initialised
 
Upvote 0
Are you sure that there are no leading or trailing spaces in both the name on the sheet tab and the value in N3?
 
Upvote 0
Are you sure that there are no leading or trailing spaces in both the name on the sheet tab and the value in N3?
I found the problem, although cell n3 did have the name it did not have the underscore. thank you for your help
 
Upvote 0
The only problem in the provided code was using the quotes around the variable name, and post #2 is the solution of the original question as it clearly explains that variable names shouldn't be surrounded by quotes in VBA.
VBA Code:
Set ws = ThisWorkbook.Sheets("wsName")

The underscore issue is a typo error that has nothing to do with the syntax error in the provided code.

Therefore, the solution post has been changed accordingly to provide consistency and help to the future readers.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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