0

What I am trying to do seems basic enough, however I don't know where I am going wrong with the code.

I want to run the selected cell through a loop of the worksheets and select the worksheet that matches the selected cell located in cell B1.

Dim SelectedCell as Range
Dim ws As Worksheet
Set SelectedCell = Range(ActiveCell.Address)
For Each ws In ActiveWorkbook.Worksheets
If ws.Range("B1").Value = SelectedCell.Value Then
ActiveSheet.Select
End If
Next ws
End Sub

Thanks in advance for all the help!

2 Answers 2

1

Try instead

Dim ws As Worksheet
SelectedCell = ActiveCell
For Each ws In ActiveWorkbook.Worksheets
  If ws.cells(1,2) = SelectedCell Then
    ws.Select
  End If
Next ws
End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

I think you need to use Set in this line SelectedCell = Activecell since you dimensioned it as Range?
Good point. Perhaps it is best to not even dimension it at all. That is part of the beauty of vba after all, isn't it? :)
0

Select cell run macro will select the sheet name that matches the selected cell. (Case sensitive)

Dim SelectCell As String
Dim ws As Worksheet
SelectCell = ActiveCell.Value2
For Each ws In ActiveWorkbook.Worksheets
    If ws.Name = SelectCell Then
        ws.Select
    End ID  
    Next ws

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.