
Loop statement doesn't work right
I have this loop statement in a macro and it is just not working right.
totalCell2 keeps the same value when it hits the next TOTAL: Here is my code
below. Any ideas?
Sub NewTotalLineTest()
With Worksheets("glffile").Range("F1:F999")
Set r = .Find("REFND MAN")
If Not r Is Nothing Then
firstAddress = r.Address
Do
rrow = r.Row
NewTotalLine
Set r = .FindNext(r)
Loop While Not r Is Nothing And r.Address <> firstAddress
'TrueTotalLine
End If
End With
End Sub
'Sub TrueTotalLine()
'Dim totalCell2 As Range
'End Sub
Sub NewTotalLine()
With Worksheets("glffile").Range("F1:F999")
Set t = .Find("TOTAL:")
If Not t Is Nothing Then
'firstaddress2 = t.Address
'Do
trow = t.Row
Columns("F:F").Find(What:="TOTAL:").Activate
trow = trow + 1
Range("F" & trow).Select
Set totalCell2 = ActiveCell
Columns("F:F").Find(What:="REFND MAN").Activate
rrow = rrow + 1
Rows(rrow).EntireRow.Insert
Range("F" & rrow).Select
With Selection
.Value = "NEW TOTAL LINE"
.Font.Bold = True
.Font.Color = RGB(0, 0, 255)
.Offset(0, 1).Value = totalCell2.Offset(0, 1).Value
.Offset(0, 1).Font.Bold = True
.Offset(0, 1).Font.Color = RGB(0, 0, 255)
.Offset(0, 3).Value = "<- Enter a new total here if
necessary"
.Offset(0, 3).Font.Italic = True
End With
Columns("F:F").AutoFit
End If
End With
End Sub
Sub NameTotalsLine()
Dim totalCell As Range
Cells.Select
Selection.Find(What:="NEW TOTAL LINE", after:=ActiveCell,
LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).Activate
Set totalCell = ActiveCell
totalCell.Offset(0, 1).Activate
ActiveWorkbook.Names.Add Name:="TotalLine", RefersTo:=ActiveCell
End Sub