Vba to search for a value in column A, if found enter specific text then delete the row

StevenChin

New Member
Joined
Sep 3, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi,

Hope you can help.

I receive a similar report from different person. In the last row of column A, some reports have Grand Summaries and some reports don't. I have 2 macros that I use depending if there is a grand summaries which is a bit annoying because i always have to check first.

1725370823937.png


I have been searching for a VBA code but could find a suitable one. I really hope someones can help to create a VBA that can to go to last row and if Grand Summaries exist, I need to run below commends which is select the entire row of the grand summaries and down to the last row of the sheet and delete everything. This is what i run daily.
Range("A1").Select
Selection.End(xlDown).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp

If the last row doesn't have Grand Summaries on the report then move 1 row down and select the entire row and down to the last row of the sheet and delete everything which is what I am using below.
Range("A1").End(xlDown).Offset(1, 0).Select
ActiveCell.EntireRow.Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearFormats
Selection.Delete Shift:=xlUp

Thanks in advance.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Welcome to the MrExcel forum. Please accept my warmest greetings.

With this macro, consider the last row with data starting from A1 and down (just like you have in your examples).
VBA Code:
Sub macro1()
  Dim f As Range
 
  Set f = Range("A:A").Find("Grand Summaries", , xlValues, xlPart, , , False)
  If Not f Is Nothing Then
    Range(f.Offset(1), Range("A" & Rows.Count)).Delete Shift:=xlUp
  Else
    Range(Range("A1").End(xlDown).Offset(1, 0), Range("A" & Rows.Count)).Select
  End If
End Sub


With this other macro, consider the last row, but starting from the last row of the sheet and going up.
VBA Code:
Sub macro2()
  Dim f As Range
 
  Set f = Range("A:A").Find("Grand Summaries", , xlValues, xlPart, , , False)
  If Not f Is Nothing Then
    Range(f.Offset(1), Range("A" & Rows.Count)).Delete Shift:=xlUp
  Else
    Range(Range("A" & Rows.Count).End(3)(2), Range("A" & Rows.Count)).Select ' Shift:=xlUp
  End If
End Sub

I show it to you, because with the first method, you would only go as far as finding the first empty cell, but there may be several intermediate empty cells and then more data.
Example with first method:
1725375966872.png



With the second method you will find the last cell with data regardless of whether there are empty intermediate cells above.
Exampel with second method:
1725376000783.png

Note: There are other ways to find the last row with data on the sheet, but the second method is the most common. You can find them on the web.

----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
 
Upvote 0

Forum statistics

Threads
1,223,883
Messages
6,175,168
Members
452,615
Latest member
bogeys2birdies

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