Simple VBA question

acsmiamiguy

New Member
Joined
Jan 1, 2018
Messages
8
Need to jump to sheets based on a range name
Item = Range("B2")
MsgBox (Item)
Sheets(Item).Select

The MsgBox code works and gives the result of '100', which is the contents of cell B2. I thought if that is true, then the next line Sheets(Item).Select would work, since 'Item' is equal to '100)'. Only it doesn't. Very frustrating.
What am I missing?
Thanks!
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Since you are using Item as a sheet index number, this will work only if Range("B2") holds the number 100 and you have at least 100 sheets in your workbook. If B2 has a text value "100". Your code will incur a run time error. Ditto if there are not at least 100 sheets present.
 
Upvote 0
If Range("B2") contains a 100 in the TEXT format (you said the message box returns '100')
Then the sheets(Item).Select is looking for a sheet literally named 100 (not the 100th sheet by index number)

Try

Item = Val(Range("B2"))
 
Upvote 0
Are you looking to select the Index (100th sheet), or is there a sheet named "100" that you are trying to select.
If Index, you must have at least 100 sheets.
If sheet named "100", declare Item to be a String first, i.e.
Code:
Dim Item As String
Item = Range("B2")
MsgBox (Item)
Sheets(Item).Select
 
Upvote 0
Bearen 2it
 
Last edited:
Upvote 0
So is it possible to pass a cell value to name a sheet?
Yes, but the code you posted is selecting a sheet not naming a newly added sheet or changing the name of an existing sheet.
 
Upvote 0

Forum statistics

Threads
1,214,956
Messages
6,122,465
Members
449,085
Latest member
ExcelError

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