The below code was working for the intended purpose: copying some rows from one worksheet to another. But, when I added the parts with rangeToDelete, which is meant to clear out the space I'm copying into before each new running of the code. I'm not sure what's happening, but when I use MsgBox to validate some parts of the code, it looks like it's the loop that isn't functioning. But, without kicking up an error, I don't know why.
Option Explicit
Sub findCells()
Dim topCell As String
Dim leftCell As String
Dim refCell As Range
Dim sht As Worksheet
Dim lastRow As Long
Dim i As Long
Dim cellVal As String
Dim altCounter As Long
Dim crange As Range
Dim rangeToDelete As Range
Set rangeToDelete = Worksheets("Summary").Cells(31, "A").CurrentRegion
rangeToDelete.Value = ""
Set refCell = ActiveCell
topCell = refCell.End(xlUp).Value
leftCell = refCell.End(xlToLeft).Value
Set sht = Worksheets(topCell)
lastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
With sht
.Range("A1:G1").Copy Worksheets("Summary").Range("A31:G31")
altCounter = 31
For i = 1 To lastRow
cellVal = Cells(i, 5).Value
If leftCell = cellVal Then
altCounter = altCounter + 1
Set crange = .Range("A" & i & ":" & "G" & i)
crange.Copy Worksheets("Summary").Range("A" & altCounter & ":" & "G" & altCounter)
End If
Next i
End With
End Sub
rangeToDelete.ClearcellVal = .Cells(i, 5).Value.