2014-06-12 113 views
0

我想要做的似乎基本夠用,但是我不知道我在哪裏出錯了代碼。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 

在此先感謝您的幫助!

回答

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

我覺得你需要使用Set這行'SelectedCell = Activecell',因爲你將它定爲Range? – L42

+0

好點。也許最好甚至根本不用維度。那畢竟是vba之美的一部分,不是嗎? :) –

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 
相關問題