Command button delete rows... sort of

KVT Holland

New Member
Joined
May 10, 2017
Messages
21
Hi,

I found a peace of code written bij
Joe Was in:

https://www.mrexcel.com/forum/excel-questions/9165-entire-row-delete.html

I modified it to work on a command button. But it removes every other row :confused: Lets say row 11 till 18 are empty/< 1 then it removes 11, 13, 15, 17. I've tried it with a ' in front of the N = N + 1, but that didn't change anything.

Code:
Private Sub CommandButton2_Click()

ActiveSheet.Unprotect Password:="PW"


Worksheets("Calculatie").Select
Range("A11:A30").Select
n = 1
n = n + 1
For Each r In Worksheets("Calculatie").UsedRange.Rows
n = r.Row
If Worksheets("Calculatie").Cells(n, 1) < 1 Then
Worksheets("Calculatie").Cells(n, 1).Select
Selection.EntireRow.Delete
End If
Next r
Application.ScreenUpdating = True
Range("A11").Select


ActiveSheet.Protect Password:="PW"


End Sub

Kind regards,

Koen.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
I thought it was pretty idiot prove, but I've been asked to adjust the code so that rows that are left blank in the middle of the sheet also get erased.

I managed to do it by;

Code:
Private Sub CommandButton2_Click()

ActiveSheet.Unprotect Password:="calc1"

Do While x < 4
    Dim r As Range, c As Range

Set r = Range("Calculatie!A10:Calculatie!A80")

    Application.ScreenUpdating = False
    
For Each c In r
    If Len(c.Text) = 0 Then
        
        c.EntireRow.Select       
        Selection.EntireRow.Delete
        
    End If
Next c

x = x + 1
Loop

Application.ScreenUpdating = True

ActiveSheet.Protect Password:="calc1"

End Sub

This works and i learned a lot about VBA, but I would like to clean it up a bit to get better at VBA.

I've used a loop because at some point 'c' get set to high and it misses an empty row every now and then.

Kind regards,


Koen.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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