Excel rk,我们应该仔细研究函数的代码。是的,问题是,尽管section_Del按预期工作,但stringFound对象在这个过程中被销毁了。嗨,Brettdj!我喜欢在删除之前检查stringFound是否存在的想法,但是在stringFound被销毁后如何重
Excel rk,我们应该仔细研究函数的代码。是的,问题是,尽管section_Del按预期工作,但stringFound对象在这个过程中被销毁了。嗨,Brettdj!我喜欢在删除之前检查stringFound是否存在的想法,但是在stringFound被销毁后如何重,excel,vba,Excel,Vba,rk,我们应该仔细研究函数的代码。是的,问题是,尽管section_Del按预期工作,但stringFound对象在这个过程中被销毁了。嗨,Brettdj!我喜欢在删除之前检查stringFound是否存在的想法,但是在stringFound被销毁后如何重置它?谢谢我想我明白你的意思了。使用union在循环中添加需要一起删除的范围,然后在循环外删除该范围。因此,在循环中不需要删除。@LawrenceKnowlton Yes-在一个结果范围内工作通常比在每次标记检查时运行更干净。文章链接展示了一个
rk,我们应该仔细研究函数的代码。是的,问题是,尽管section_Del按预期工作,但stringFound对象在这个过程中被销毁了。嗨,Brettdj!我喜欢在删除之前检查stringFound是否存在的想法,但是在stringFound被销毁后如何重置它?谢谢我想我明白你的意思了。使用union在循环中添加需要一起删除的范围,然后在循环外删除该范围。因此,在循环中不需要删除。@LawrenceKnowlton Yes-在一个结果范围内工作通常比在每次标记检查时运行更干净。文章链接展示了一个这样的例子。我试着使用union,但无法使它工作。因此,我的下一个想法是在循环中将范围收集到一个数组中。现在的问题是如何访问该数组,以便删除实际范围(即行范围)?
Union
上述添加的方法。从阵列中恢复是困难的-这是可能的,但很困难。最好使用该范围
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
On Error Goto ExitLoop
ExitLoop:
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
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