Vba 使用“查找”删除行
我试图在结果之前删除单词中的行。我试图使用Find命令,但我没有任何运气。以下是我尝试过的众多变体之一:Vba 使用“查找”删除行,vba,excel,Vba,Excel,我试图在结果之前删除单词中的行。我试图使用Find命令,但我没有任何运气。以下是我尝试过的众多变体之一: Sub FindandDelete() Modifications = Range("A1:A1200").Find("Modifications", LookIn:=xlValues) Res = Cells.Find("Results", LookIn:=xlValues) Range("Modifications:Results").Delete End Sub
Sub FindandDelete()
Modifications = Range("A1:A1200").Find("Modifications", LookIn:=xlValues)
Res = Cells.Find("Results", LookIn:=xlValues)
Range("Modifications:Results").Delete
End Sub
有什么想法或建议吗?试试这个:
Sub FindandDelete()
Dim Modifications As Excel.Range
Dim Res As Excel.Range
Set Modifications = Range("A1:A1200").Find(What:="Modifications", LookIn:=xlValues)
Set Res = Range("A1:A1200").Find(What:="Results", LookIn:=xlValues)
If Not (Modifications Is Nothing) And Not (Res Is Nothing) Then
ActiveSheet.Range(Modifications, Res).EntireRow.Delete 'ClearContents '<<edit to delete rows rather than just clear their contents
End If
End Sub
代码不起作用的两个主要原因:
Sub FindandDelete()
Modifications = Range("A1:A1200").Find("Modifications", LookIn:=xlValues).Row
Res = Cells.Find("Results", LookIn:=xlValues).Row
Range(Modifications & ":" & Res).Delete
End Sub
但如果找不到值,则会引发错误
因此,请尝试更精细但更准确的技术:
Sub SomeSub()
'Just to stay clean and make sure we're using the proper workbook/sheet
wb = ThisWorkbook
ws = wb.Sheets("YourSheet") 'Rename with your sheet's name
columnOfInterest = 1 'Replace with the number of the column
'Find the last row of that column
last = ws.Cells(ws.Rows.Count, columnOfInterest).End(xlUp).Row
'Loop from the first row to the last...
For x = 1 To last
'And stop at the first thing that resembles "Results"...
If ws.Cells(x, columnOfInterest) Like "*Results*" Then
Res = x
Exit For
End If
Next x
'Loop from the first row to the last...
For x = 1 To last
'And stop at the first thing that resembles "Modifications"...
If ws.Cells(x, columnOfInterest) Like "*Modifications*" Then
Modif = x
Exit For
End If
Next x
If Res > 0 And Modif > 0 And Res > Modif Then
'Loop from "Results" to "Modifications" (backwards, indeed) to delete the rows
For x = Res To Modif Step -1
ws.Rows(x).Delete
Next x
End If
End Sub
你试过录制宏吗。。。录制的宏将为您提供一些有关如何改进此代码的线索。我尝试过,但我从中得到的只是行号和删除,但行号每次都会根据数据而变化。我建议您声明变量。特别是对于
范围
,您将看到需要使用Set
关键字来分配它们。代码在最后一行不断给我一个错误。我试着用.Delete而不是.ClearContents来编辑它,但是没有运气。不过还是不走运,它在Res=。。。行。我已经测试过了,效果很好:只需编辑第一个代码段,这样它就不会发现任何内容-因为任何内容都不会引发错误。我可以说的是,它显示“运行时错误'91':对象变量或未设置块变量。”@user3709645-我刚刚编辑了这两个脚本以考虑“查找不返回任何内容”的问题。第一个答案是删除“结果”行之前包含“修改”的每一行。如果“修改”或“结果”不存在,您当前的答案将出错?非常正确,可能返回到稍微不同的“以前的答案”(+1我喜欢原作,失去它似乎很遗憾)
Sub SomeSub()
'Just to stay clean and make sure we're using the proper workbook/sheet
wb = ThisWorkbook
ws = wb.Sheets("YourSheet") 'Rename with your sheet's name
columnOfInterest = 1 'Replace with the number of the column
'Find the last row of that column
last = ws.Cells(ws.Rows.Count, columnOfInterest).End(xlUp).Row
'Loop from the first row to the last...
For x = 1 To last
'And stop at the first thing that resembles "Results"...
If ws.Cells(x, columnOfInterest) Like "*Results*" Then
Res = x
Exit For
End If
Next x
'Loop from the first row to the last...
For x = 1 To last
'And stop at the first thing that resembles "Modifications"...
If ws.Cells(x, columnOfInterest) Like "*Modifications*" Then
Modif = x
Exit For
End If
Next x
If Res > 0 And Modif > 0 And Res > Modif Then
'Loop from "Results" to "Modifications" (backwards, indeed) to delete the rows
For x = Res To Modif Step -1
ws.Rows(x).Delete
Next x
End If
End Sub