我在定義該檢查時遇到了一些問題。它只是帶有Do Until回圈的整個代碼的一部分。
如果我的作業表中的任何行與搜索值匹配,則定義FindRow為“1”。
如果找到匹配的行,則FindRow =該行號。
With ThisWorkbook.Sheets("TKSLIST").Range("A:A")
FindRow = 1
strTKSname = ws.Cells(i 1, 11).Text
If .Find(What:=strTKSname, LookAt:=xlPart, LookIn:=xlValues, MatchCase:=False).Row Is Nothing Then
FindRow = 1
Else
FindRow = .Find(What:=strTKSname, LookAt:=xlPart, LookIn:=xlValues, MatchCase:=False).Row
End If
End With
我錯過了一些東西。每次找不到我的搜索值時,我都會收到錯誤訊息。
uj5u.com熱心網友回復:
正確宣告你的變數
獲取發現某物的范圍
FoundAt并將其與Nothing洗掉 else 部分,因為
FindRow = 1已經設定為初始值。
With ThisWorkbook.Sheets("TKSLIST").Range("A:A")
Dim FindRow As Long
FindRow = 1 ' this needs to be inside the loop (and you can omit the else part below)
Dim strTKSname As String
strTKSname = ws.Cells(i 1, 11).Text
Dim FoundAt As Range
Set FoundAt = .Find(What:=strTKSname, LookAt:=xlPart, LookIn:=xlValues, MatchCase:=False)
If Not FoundAt Is Nothing Then
FindRow = FoundAt.Row
End If 'no else needed because initialized as `FindRow = 1`
End With
uj5u.com熱心網友回復:
在回圈中使用 Find 方法
Sub Whatever()
' somewhere before the loop
Dim fCell As Range
Dim FindRow As Long
With ThisWorkbook.Sheets("TKSLIST").Range("A:A")
strTKSname = ws.Cells(i 1, 11).Value
' Just a reminder:
' Without the 'After' the search starts with `A2`.
' If that is ok with you, you don't need it.
Set fCell = .Find(What:=strTKSname, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, LookAt:=xlPart)
If fCell Is Nothing Then
FindRow = 1
Else
FindRow = fCell.Row
End If
End With
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/358510.html
