[英]Excel VBA need to remove all rows with the #REF! value
我有以下代碼,該代碼在L列中使用單詞“ Record”查找值,然后將行復制到單獨的工作表中。
我在值#REF的行上收到類型不匹配錯誤! 發生在L列中。
修改以下內容以完全刪除這些行,然后繼續執行預期功能的最佳方法是什么?
NB LastRow是'Long'變量類型
Sheets("Demand").Rows("1").Copy Sheets("Data").Range("A1")
With Sheets("Demand")
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
pasteRowIndex = 2
For r = 1 To LastRow
If LCase(.Cells(r, "L").Value) Like LCase("Record*") Then
If UBound(Split(.Cells(r, "L"), ",")) > 0 Then
i = i + 1
ReDim v(1 To i)
v(i) = pasteRowIndex
End If
Sheets("Demand").Rows(r).Copy Sheets("Data").Rows(pasteRowIndex)
pasteRowIndex = pasteRowIndex + 1
End If
Next r
End With
首先,您需要從最后一行迭代到第一行:
For r = LastRow to 1 step -1
'Next, add if statement inside the loop checking if cell returns error:
If IsError(.Cells(r, "L").Value) then
'if so, delete this row:
.Cells(r, "L").entirerow.delete
'..... the rest of your code as ElseIf part of conditional statement
ElseIf LCase(.Cells(r, "L").Value) Like LCase("Record*") Then
If UBound(Split(.Cells(r, "L"), ",")) > 0 Then
i = i + 1
ReDim v(1 To i)
v(i) = pasteRowIndex
End If
Sheets("Demand").Rows(r).Copy Sheets("Data").Rows(pasteRowIndex)
pasteRowIndex = pasteRowIndex + 1
End If
Next r
像這樣...
For r = 1 To LastRow
If Not IsError(.Cells(r, "L").Value) Then
If LCase(.Cells(r, "L").Value) Like "record*" Then
If UBound(Split(.Cells(r, "L"), ",")) > 0 Then
i = i + 1
ReDim v(1 To i)
v(i) = pasteRowIndex
End If
Sheets("Demand").Rows(r).Copy Sheets("Data").Rows(pasteRowIndex)
pasteRowIndex = pasteRowIndex + 1
End If
End If
Next r
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.