how do i create a macro to select a specific worksheet using a value in my active worksheet.

Eddie Phan

New Member
Joined
Mar 23, 2021
Messages
1
Office Version
  1. 2019
Platform
  1. Windows
Hi All,
I am trying to assign a macro to a button to select a worksheet based on the value on my active worksheet.
e.g. The value on cell "B4" on the active worksheet is "GL1500", i am trying to assigned a macro to select Worksheet Name "GL1500" out of the multiple worksheet of GL1300 to GL2000. Therefore once i run the macro, it go to worksheet name GL1500 and become my active worksheet.

I am wondering can this be done with vba?

Thank in advance.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Welcome to the MrExcel board!

I am wondering can this be done with vba?
Sure, try this.
It assumes that B4 will definitely contain a name of one of the worksheets in the workbook.

VBA Code:
Sub Go_To_Sheet()
  Sheets(Range("B4").Value).Activate
End Sub
 
Upvote 0
I would try this:
VBA Code:
Private Sub CommandButton1_Click()
'Modified  3/23/2021  5:03:21 AM  EDT
On Error GoTo M
Dim ans As String
ans = Range("B4").Value
Sheets(ans).Activate
Exit Sub
M:
MsgBox "There is no sheet named" & vbNewLine & ans, , "Oops"
End Sub
 
Upvote 0
If you wanted to do this in any sheet in your workbook without needing a button.
Try this:
The script is activated when you enter any value in Range("B4") in any sheet in your workbook.

This is an auto sheet event script
Your Workbook must be Macro enabled
To install this code:
Right-click on any sheet tab
Select View Code from the pop-up context menu
In top left corner of window double click on This Workbook
Paste the code in the VBA edit window
VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Cells.CountLarge > 1 Or IsEmpty(Target) Then Exit Sub
On Error GoTo M
    If Target.Address = Range("B4").Address Then Sheets(Target.Value).Activate
    
Exit Sub
M:
MsgBox "There is no sheet named" & vbNewLine & ans, , "Oops"
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,796
Members
449,095
Latest member
m_smith_solihull

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