我对VBA很新,所以这可能是一个基本问题。
每周我都会导出包含各种数据的文件。在这个文件中,我每次都要删除相同的行范围。我可以通过基于单元格位置定义范围来轻松实现此自动化,但此范围有时不会在同一行上启动。但是,范围以相同的值开始,每次都以相同的值结束。
我是否有可能自动删除从开始到最低范围内的所有行?无需根据单元格位置指定范围?
答案 0 :(得分:0)
这就是你的样子
Option Explicit
Sub TargetRows()
Dim wb As Workbook
Dim wsTarget As Worksheet
Dim startCell As Range
Dim endCell As Range
Dim startMatch As String
Dim endMatch As String
startMatch = "Artikelgroep: Promotional material"
endMatch = "Artikelgroep: (totaal)"
Set wsTarget = ThisWorkbook.Worksheets("Sheet2") 'change as required
Set startCell = wsTarget.Columns(1).EntireColumn.Find(what:=startMatch, LookIn:=xlValues, lookat:=xlPart)
Set endCell = wsTarget.Columns(1).EntireColumn.Find(what:=endMatch, LookIn:=xlValues, lookat:=xlPart)
Dim deleteRange As Range
If Not startCell Is Nothing And Not endCell Is Nothing And startCell.Row <= endCell.Row Then
Set deleteRange = wsTarget.Range("A" & startCell.Row & ":A" & endCell.Row)
Else
Debug.Print "1 or both values not found or end text found before start text."
End If
If Not deleteRange Is Nothing Then deleteRange.EntireRow.Delete
End Sub
参考:
Excel VBA Find Method for specific column(@shai rado)