Excel VBA循环&单元格值匹配

时间:2014-06-12 18:56:08

标签: excel vba excel-vba

我想做的事似乎很基本,但我不知道我的代码出错了。

我希望通过工作表循环运行选定的单元格,并选择与单元格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

提前感谢所有帮助!

2 个答案:

答案 0 :(得分:1)

尝试改为

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

答案 1 :(得分:0)

选择单元格运行宏将选择与所选单元格匹配的工作表名称。 (区分大小写)

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