dynamic row deletion

mcranmoss

Board Regular
Joined
Dec 13, 2011
Messages
165
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




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
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Try:

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.Range("A" & x & ":A" & i).EntireRow.Delete
            
            Sh.Range("A1").Select
        End If
    Next Sh
End Sub
 
Upvote 0

Forum statistics

Threads
1,207,423
Messages
6,078,441
Members
446,338
Latest member
AliB

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top