我正在尝试解析Excel 2007中的报表。它基本上是会计费用例外的报告。该报告包含每种类型的异常标题的部分。从报告中删除了一些类型的例外。我正在使用Do While循环来查找每个标题,如果需要删除该部分,我会这样做。如果不需要删除任何代码,代码工作正常,但在删除一个部分后,我得到一个“无法获取Range类的FindNext属性”错误。这是我的代码:
Sub merge_All_Section_Headers()
' Description:
' The next portion macro will find and format the Tranaction Source rows in the file
' by checking each row in column A for the following text: TRANSA. If a cell
' has this text in it, it is selected and a function called merge_text_cells
' is run, which performs concatenation of each Transaction Source header row and
' deletes the text from the rest of the cells with broken up text.
'
lastRow = ActiveSheet.UsedRange.Rows.Count + 1
Range(lastRow & ":" & lastRow).Delete
ActiveSheet.PageSetup.Orientation = xlLandscape
With ActiveSheet.Range("A:A")
Dim searchString As String
searchString = "TRANSA"
'The following sets stringFound to either true or false based on whether or not
'the searchString (TRANSA) is found or not):
Set stringFound = .Find(searchString, LookIn:=xlValues, lookat:=xlPart)
If Not stringFound Is Nothing Then
firstLocation = stringFound.Address
Do
stringFound.Select
lastFound = stringFound.Address
merge_Text_Cells
If ((InStr(ActiveCell.Text, "CHARGE FILER") = 0) And _
(InStr(ActiveCell.Text, "CREDIT FILER") = 0) And _
(InStr(ActiveCell.Text, "PA MIDNIGHT FINAL") = 0) And _
(InStr(ActiveCell.Text, "BAD DEBT TURNOVER") = 0)) Then
section_Del 'Function that deletes unwanted sections
End If
Range(lastFound).Select
Set stringFound = .FindNext(stringFound)
Loop While Not stringFound Is Nothing And stringFound.Address <> firstLocation
End If
End With
'-----------------------------------------------------------------------------------
'BELOW CONTAINS THE CODE THAT WORKS:
Sub merge_All_Section_Headers()
' Description:
' The next portion macro will find and format the Tranaction Source rows in the file
' by checking each row in column A for the following text: TRANSA. If a cell
' has this text in it, it is selected and a function called merge_text_cells
' is run, which performs concatenation of each Transaction Source header row and deletes
' the text from the rest of the cells with broken up text.
'
lastRow = ActiveSheet.UsedRange.Rows.Count + 1
Range(lastRow & ":" & lastRow).Delete
ActiveSheet.PageSetup.Orientation = xlLandscape
With ActiveSheet.Range("A:A")
Dim searchString As String
Dim arrRangesToDelete(0 To 9) As Range
searchString = "TRANSA"
'The following sets stringFound to either true or false based on whether or not
'the searchString (TRANSA) is found or not):
Set stringFound = .Find(searchString, LookIn:=xlValues, lookat:=xlPart)
If Not stringFound Is Nothing Then
firstLocation = stringFound.Address
counter = 0
Do
stringFound.Select
lastFound = stringFound.Address
merge_Text_Cells
If ((InStr(ActiveCell.Text, "CHARGE FILER") = 0) And _
(InStr(ActiveCell.Text, "CREDIT FILER") = 0) And _
(InStr(ActiveCell.Text, "PA MIDNIGHT FINAL") = 0) And _
(InStr(ActiveCell.Text, "BAD DEBT TURNOVER") = 0)) Then
firstRowOfSection = ActiveCell.Row
lastRowOfSection = (ActiveSheet.Range(ActiveCell.Offset(2, 1).Address).End(xlDown).Row + 2)
Set arrRangesToDelete(counter) = Range(firstRowOfSection & ":" & lastRowOfSection)
counter = counter + 1
End If
Range(lastFound).Select
Set stringFound = .FindNext(stringFound)
Loop While Not stringFound Is Nothing And stringFound.Address <> firstLocation
End If
End With
For i = 0 To counter - 1
arrRangesToDelete(i).Delete
Next i
Range(firstLocation).Select
End Sub
因此,数组工作并完成工作,而不会破坏任何对象。我仍然想尝试使用Union方法,看看我是否可以使用它,这也很酷!
答案 0 :(得分:3)
由于StrFound
中的范围对象已被销毁,您的代码会崩溃 - 因此当您申请时,Is Nothing
有几种替代方法可以提出错误处理bu Juri(如果你使用的话应该立即重置)
Union
的新范围,然后在循环外的单个镜头中删除此范围。我在文章Using Find and FindNext to efficiently delete any rows that contain specific text .FindNext
之后而不是之前移动删除代码,并在运行stringfound
代码Section_Del
是否存在
联盟方法
Sub UnionAPp()
Dim c As Range
Dim rng1 As Range
With Worksheets(1).Range("a1:a500")
Set c = .Find(2, LookIn:=xlValues)
If Not c Is Nothing Then
firstaddress = c.Address
Set rng1 = c
Do
Set c = .FindNext(c)
Set rng1 = Union(rng1, c)
Loop While c.Address <> firstaddress
End If
MsgBox "Your working range is " & rng1.Address
End With
End Sub
因此,您可以从
修改FindNext
的标准Excel帮助
标准强>
Sub TestInit()
With Worksheets(1).Range("a1:a500")
Set c = .Find(2, LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Value = 5
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
End Sub
新
Sub TestA()
With Worksheets(1).Range("a1:a500")
Set c = .Find(2, LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Value = 5
Set c = .FindNext(c)
If Not c Is Nothing Then c.Clear
'your code: If Not StrFound Is Nothing Then Call Section_Del
Loop While Not c Is Nothing
End If
End With
End Sub
答案 1 :(得分:0)
如果删除所有匹配项,Findnext
应该出错。
一行
On Error Goto ExitLoop
应在Set stringFound = .FindNext(stringFound)
之前添加。
一行
ExitLoop:
应在Loop While...