I have some code which loops through all visible worksheets and deletes rows in each sheet, dependent on the contents of columns A and D.
The first macro below is the one that works. However, I don't understand why the second version doesn't work.
Any advice welcome.
Mark
Excel 2010
The first macro below is the one that works. However, I don't understand why the second version doesn't work.
Any advice welcome.
Mark
Excel 2010
Code:
Sub DeleteUnwantedRows2()
Dim i As Integer
Dim x As Integer
Dim Sh As Worksheet
For Each Sh In ThisWorkbook.Worksheets
If Sh.Visible = True Then
Sh.Activate
Sh.Range(Cells(Range("D2").End(xlDown).Row, 1), Cells(Range("A1").End(xlDown).Row + 1, 1)).EntireRow.Delete
Sh.Range("A1").Select
End If
Next Sh
End Sub
Code:
Sub DeleteUnwantedRows2()
Dim i As Integer
Dim x As Integer
Dim Sh As Worksheet
For Each Sh In ThisWorkbook.Worksheets
If Sh.Visible = True Then
i = Range("A1").End(xlDown).Row + 1
x = Range("D2").End(xlDown).Row
Sh.Activate
Sh.Rows(i).Range(Cells(x, 1), Cells(i, 1)).EntireRow.Delete
Sh.Range("A1").Select
End If
Next Sh
End Sub