我試圖將公共列鏈接在一起,但是當我單擊PolicyComponents Sheet中的一個單元格時,for循環不運行 - 它只是退出sub。For循環不運行在工作表中
代碼段:
Sub LinkName()
Dim i As Long
Dim ShtUsedRange, ShtUsedRangeCol
Dim name As String
Dim name1 As String
Dim lookup_range As Range
Dim box
ShtUsedRange = ActiveSheet.UsedRange.Rows.Count
'Count the used rows in the Activesheet
ShtUsedRangeCol = ActiveSheet.UsedRange.Columns.Count
'Count the used Column in the Activesheet
name = ActiveCell.Row
'Row of the Selected Cell
name1 = ActiveSheet.Cells(name, 1).Value
'name of the row selected
'MsgBox name1
Set lookup_range = ThisWorkbook.Sheets("PolicyDetails").Range("a1:z5000")
'set the range of the Policy details to search from
box = Application.WorksheetFunction.VLookup(name1, lookup_range, 1, False)
'to match the name to the policy details
MsgBox box
For i = 1 To ThisWorkbook.Sheets("PolicyComponents").Rows.Count Step -1
If ThisWorkbook.Sheets("PolicyComponents").Cells(i, 1).Value = box Then
ThisWorkbook.Sheets("Policy Viewer").Cells(16, 2).Value = ThisWorkbook.Sheets("PolicyComponents").Cells(i, 4).Value
End If
Next i
End Sub
您正在使用* name *作爲字符串類型變量,但將其分配給行號值。 Thi9s表示* name *是*「2」*而不是* 2 *,並且在需要數字時不能使用。 – Jeeped
好吧我已經修復了,但是當我調試一行一行時,它只是完全跳過for循環。 @Jeeped –
我不清楚下面的代碼修改是否解決了問題。如果你仍然需要一些調試幫助,你可以通過編輯添加對你的問題的任何修改嗎? – Jeeped