我想在上搜索圖表BD
與我的工作表Plan1
上某個值alocacao
匹配的所有條目。然後它應該將Column 2
上的值複製到名爲tecnico1
的單元格中(其他單元格被稱爲tecnico2, tecnico3 and tecnico4
)。我說明下文:vba查找和查找下一個問題
與值TESTE 2所述的細胞是alocacao
。
我試圖使用查找和FindNext中,這是我試過到目前爲止:
Sub VerifProd_Click()
Dim FoundCell As Range
Dim LastCell As Range
Dim FirstAddr As String
Dim fnd As String
Dim i As Long
i = 2
fnd = Sheets(1).Range("alocacao").Value
With Sheets("BD").Columns(5)
Set LastCell = .Cells(.Cells.Count)
End With
Set FoundCell = Sheets("BD").Columns(5).Find(what:=fnd, after:=LastCell)
If Not FoundCell Is Nothing Then
FirstAddr = FoundCell.Address
End If
Do Until FoundCell Is Nothing
Sheets("BD").Cells(i,2).Copy Sheets("Plan1").Range("tecnico" & i).Value
i = i + 1
Set FoundCell = Sheets("BD").Columns(5).FindNext(after:=FoundCell)
If FoundCell.Address = FirstAddr Then
Exit Do
End If
Loop
End Sub
但它不工作,我得到運行時間錯誤1004但代碼未突出顯示。我對Find和FindNext不太熟悉,所以我會很感激任何幫助,以瞭解它爲什麼不能正常工作。
編輯
我嘗試新的東西,我改變了它的一部分只是爲了測試它會粘貼單元格B26的值。現在,我得到運行時錯誤438:
With Sheets("BD").Columns(5)
Set LastCell = .Cells(.Cells.Count)
End With
Set FoundCell = Sheets("BD").Columns(5).Find(what:=fnd, after:=LastCell)
If Not FoundCell Is Nothing Then
FirstAddr = FoundCell.Address
End If
Do Until FoundCell Is Nothing
Sheets("Plan1").Range("B26") = FoundCell.Adress.Offset(0, -3).Value
Set FoundCell = Sheets("BD").Columns(5).FindNext(after:=FoundCell)
If FoundCell.Address = FirstAddr Then
Exit Do
End If
Loop
'FoundCell.Adress.Offset(0,-3).Value'刪除'.Address' –
@ A.S.H謝謝。有趣的是,現在它複製的價值,但它是最後一場比賽,而不是第一次... – paulinhax
正常,因爲你在同一個地方複製所有的比賽;) –