Switching tabs - VBA code

aymericdv

New Member
Joined
Jan 27, 2016
Messages
2
Private Sub Macro12()
Dim SheetNo As String

SheetNo = Worksheets("Select Sheet").Range("B2").Text

If Worksheets("Select Sheet").Range("B2") <> "" Then
Worksheets("SheetNo").Activate
Range("A1").Activate

End If

End Sub

Can anyone tell me why the above does not work?

I am trying to acivate the relevant worksheet using a button linked to "Macro12".

In B2 of the sheet "Select Sheet", I have data validated the cell to give me the names of 7 other worksheets (Sheet1 - Sheet7).

MsgBox SheetNo = Worksheets("Select Sheet").Range("B2").Text gives Sheet3 if for example in cell B2 I have chosen Sheet3.

I get an error from the line coloured in red above.

Thanks in advance
Aymeric
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
In this line
Worksheets("SheetNo").Activate

It's not actually using the SheetNo as a variable
Because of the Quote marks, it's literally looking for a sheet named "SheetNo"
Not whatever the value of the Variable SheetNo

Try
Worksheets(SheetNo).Activate
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,921
Messages
6,175,379
Members
452,639
Latest member
RMH2024

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