How do i read this message?

wai2kit

New Member
Joined
Mar 31, 2023
Messages
4
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
Hi all,

I'm fairly new to VBA and recently i was working on a file with a VBA code as per below which deletes rows that has zero in column 'I'.

Dim i As Long, n As Long
Dim rg As Range
Application.ScreenUpdating = False
With ActiveSheet
Set rg = Intersect(.UsedRange, .Columns("I"))
n = rg.Rows.Count
For i = n To 8 Step -1
If Not IsError(rg.Cells(i, 1).Value) Then
If rg.Cells(i, 1).Value = 0 Then rg.Rows(i).EntireRow.Delete
End If
Next
End With

I was having trouble understanding this whole sequence so i did a test where i input a single row in column 'J' to zero. Then i changed the above Columns("I") to Columns ("J") and it then deleted a whole bunch of rows which are not supposed to be deleted.

Can someone assist me with how the vba code above works? Many thanks
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi,

Below is your macro commented ...
Keep in mind a macro is very often tailor-made to the worksheet ... to a specific situation ... and might need to be adapted if conditions require it ...
VBA Code:
Sub DeleteRows()
Dim i As Long, n As Long
Dim rg As Range
Application.ScreenUpdating = False
    With ActiveSheet
        ' Define the Range to operate on
        Set rg = Intersect(.UsedRange, .Columns("I"))
        ' Define the Number of rows in Column I
        n = rg.Rows.Count
        ' Loop from the bottom row up to row 8 ... with a step of  -1
            For i = n To 8 Step -1
                ' Test cell - if cell = 0 Then delete the entire row
                If Not IsError(rg.Cells(i, 1).Value) Then
                    If rg.Cells(i, 1).Value = 0 Then rg.Rows(i).EntireRow.Delete
                End If
            Next i
    End With
End Sub
 
Upvote 1
Solution

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

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