Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Activate sheet when clicking on cell based on cell value

I was attempting to set up a way to jump to sheets in the same workbook by clicking on a cell and activating the sheet with the same value. My code works when the cell value is text. However, it does not work when the value is a number. The majority (99%) of the sheets will be only numbers (6 digits) for the sheet name. An example of a sheet name would be 110110. Here is my code I used:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.ScreenUpdating = False
If Selection.Count = 1 Then
If Not Intersect(Target, Range("B4:B800")) Is Nothing Then
If Target.Value <> "" Then
Sheets(Target.Value).Activate
End If
End If
End If
End Sub

I receive:
Run-time error ‘9’:

Subscript out of range

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

whenever I click on the

>Solution :

In Excel, you can refer to a sheet by the name or index.

In your case, you are referring by index (number) that does not exist in the Excel.
You can simple convert the number to string by using CStr():

Sheets(CStr(Target.Value)).Activate
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading