Unable to amend Macro range

amandeep08

Board Regular
Joined
Mar 20, 2011
Messages
107
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
 

Some videos you may like

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,028
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
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
 

Saurabhj

Active Member
Joined
Jun 6, 2020
Messages
413
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
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
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,028
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,127,136
Messages
5,622,936
Members
415,941
Latest member
georgiana686

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
Top