Проблема со строкой
Set rng = .Range("A:A").Find(what:="##Section", after:=.Range("A1")).Row
Вы пытаетесь сохранить значение Long
в переменной Range
Попробуйте это
Sub Sample()
Dim rng As Range
Dim sRow As Long, lastRow As Long
With Sheets("import")
'find Terminations
Set rng = .Range("A:A").Find(what:="##Section", after:=.Range("A1"))
If Not rng Is Nothing Then
'~~> Get the start row
sRow = rng.Row + 1
'find last row
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
lastRow = .Cells.Find(what:="*", _
after:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Else
lastRow = 1
End If
'I use lastRow + 1 to prevent deletion "##Section" when it is on lastrow
.Range(sRow & ":" & lastRow + 1).Delete Shift:=xlUp
End If
End With
End Sub
Или краткая версия
Sub Sample()
Dim ws As Worksheet
Dim rng As Range
Dim StartRow As Long, LastRow As Long
Set ws = Sheets("import")
With ws
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
'~~> Find last row
LastRow = .Cells.Find(what:="*", _
after:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row + 1 '<~~ Added + 1 to last row here
'~~> Identify your range and find
Set rng = .Range("A1:A" & LastRow).Find(what:="##Section", after:=.Range("A1"))
If Not rng Is Nothing Then
'~~> Get the start row
StartRow = rng.Row + 1
'~~> Delete the range
.Range(StartRow & ":" & LastRow).Delete Shift:=xlUp
End If
End If
End With
End Sub