如果标题始终以相同的特定字符开头,您可以遍历行,找到标题文本的任何辅助实例,并删除所需的行集。在下面的代码中,您需要指定 CommonHeaderText 字符串以及报告中的第一个单元格。
Sub HeaderRemover()
' dim the variables
Dim FirstCell As Range
Dim CommonHeaderText, DeletionRowString As String
Dim HeaderInstances As Integer
' modify to grab the right header and start in the right place
CommonHeaderText = "Test Header"
Set FirstCell = Range("A1")
' initialize the counter
HeaderInstances = 0
' loop through the rows, removing extra header lines
For i = 0 To FirstCell.CurrentRegion.Rows.Count
' check if header text starts the specific cell
If CommonHeaderText = Left(FirstCell.Offset(i), Len(CommonHeaderText)) Then
HeaderInstances = HeaderInstances + 1
' remove the desired rows if this is this isn't the first instance of the header text
If HeaderInstances > 1 Then
' remove 10 sequential rows, starting 3 rows before the header row
DeletionRowString = i - 2 & ":" & i + 7
Rows(DeletionRowString).Delete Shift:=xlUp
End If
End If
Next i
End Sub
请注意,如果有空行,FirstCell.CurrentRegion.Rows.Count 将无法正常工作。你可以做一些聪明的事情,或者只是硬编码一个足够大的整数,因为这不是计算密集型的。