Unable to amend Macro range

amandeep08

Board Regular
Joined
Mar 20, 2011
Messages
130
Office Version
  1. 365
I have a macro that will delete rows if Word GROSS found in column G but it is picking data from G1 till G End. I want to amend the macro where the data will be deleted from G4 onwards.



Sub DeleteRowWithContents()
'========================================================================
' DELETES ALL ROWS FROM A2 DOWNWARDS WITH THE WORDs "Gross" IN COLUMN G
'========================================================================
Last = Cells(Rows.Count, "g").End(xlUp).Row
For i = Last To 3 Step -1
If (Cells(i, "g").Value) = "Gross" Then
'Cells(i, "A").EntireRow.ClearContents ' USE THIS TO CLEAR CONTENTS BUT NOT DELETE ROW
Cells(i, "A").EntireRow.Delete
End If
Next i
End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try
VBA Code:
Sub DeleteRowWithContents()
Dim lr As Long, i As Long
lr = Cells(Rows.Count, "g").End(xlUp).Row
For i = lr To 4 Step -1
If (Cells(i, "g").Value) = "Gross" Then
Rows(i).ClearContents ' USE THIS TO CLEAR CONTENTS BUT NOT DELETE ROW
'Rows(i).Delete
End If
Next i
End Sub
 
Upvote 0
Hi Amandeep,

Please check below code:

VBA Code:
Sub DeleteRowWithContents()
'========================================================================
' DELETES ALL ROWS FROM A4 DOWNWARDS WITH THE WORDs "Gross" IN COLUMN G
'========================================================================
last = Cells(Rows.Count, "g").End(xlUp).Row
i = 4
Do While i <= last
    If (Cells(i, "G").Value) = "Gross" Then
        'Cells(i, "A").EntireRow.ClearContents ' USE THIS TO CLEAR CONTENTS BUT NOT DELETE ROW
        Cells(i, "A").EntireRow.Delete
        i = i - 1
        last = last - 1
    End If
    i = i + 1
Loop
End Sub
 
Upvote 0
Shorter
VBA Code:
Sub DeleteRowWithContents()
Dim i As Long
For i = Cells(Rows.Count, "g").End(xlUp).Row To 4 Step -1
If Cells(i, "g").Value = "Gross" Then Rows(i).ClearContents ' OR Rows(i).Delete
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,576
Messages
6,120,350
Members
448,956
Latest member
Adamsxl

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