Delete rows VBA

kbp

New Member
Joined
May 14, 2012
Messages
36
Hi there

Need some help please.

I am trying to write a VBA macros in my huge data sheet. What I want is, column A has a lot of data starting with Alpha and Numeric text or even blank. I want to write the macros saying, if in Column A the text starts with the number "4", keep that row as is, and delete the rest of the rows in the sheet. Now the text starting with number 4 can be a 4 or a 5 digit number, that does not matter, but it always starts with 4. I wrote the below code, but it's doing the opposite. what am I doing wrong here? Thanks.

Macros:

Dim c As Range
Dim SrchRng

Set SrchRng = ActiveSheet.Range("A1", ActiveSheet.Range("A65536").End(xlUp))
Do
Set c = SrchRng.Find("4", LookIn:=xlValues)
If Not c Is Nothing Then c.EntireRow.Delete
Loop While Not c Is Nothing



Sample of data

DPR BC058-03 ONLINE MONETARY TRANSACTIONS BY OPERATOR PAGE 1
ACQ CORP :12 REPORT OF CLOSED BATCHES AS OF DATE: 06/10/14
DEPARTMENT :O 03131 CURRENCY CODE: 554 RUN DATE : 06/10/14
OPERATOR :AB342593
BATCH # TC RC CP ACCOUNT NUMBER MICROFILM REFERENCE CHECK NO AMOUNT DESCRIPTION REJ RSN
____________________________________________________________________________________________________________________________________
PLAN SEQ FIX PAY AMOUNT FIX PAY # STORE FL INT FL INS FL RATE
____________________________________________________________________________________________________________________________________
42047 71 13 12 4564-910-109-058-107 7-456491-4279-000-420-47001-4 0000000 15.00- LATE FEE REVERSAL
0.00 0 0.00 0.00 0.000000
42047 71 13 12 4564-910-133-488-106 7-456491-4279-000-420-47002-2 0000000 15.00- LATE FEE REVERSAL
0.00 0 0.00 0.00 0.000000
42047 71 13 12 4548-601-477-497-107 7-454860-4279-000-420-47003-8 0000000 15.00- LATE FEE REVERSAL
0.00 0 0.00 0.00 0.000000
42047 71 13 12 4548-601-133-907-127 7-454860-4279-000-420-47004-6 0000000 30.00- LATE FEE REVERSAL

<colgroup><col span="11"></colgroup><tbody>
</tbody>
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Is your data always structured as shown... rows starting with 4's contiguous with each other and always located at the bottom of the data?
 
Upvote 0
No, not always structured but the rows that start with numbers anywhere between 40000 to 49999, we need to keep and the rest, it can be header rows, or lines or 0.00 or blank whatever needs to be deleted.
 
Upvote 0
Okay, depending on how huge your data sheet is, the following macro may work (if not, then it will take a little slower piece of code to do what you want)...
Code:
Sub KeepRowsStartingWith4()
  Dim Addr As String
  Addr = "A1:A" & Cells(Rows.Count, "A").End(xlUp).Row
  Range(Addr) = Evaluate("IF(ROW(),IF(LEFT(" & Addr & ")=""4""," & Addr & ",""""))")
  Range(Addr).SpecialCells(xlBlanks).EntireRow.Delete
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,431
Messages
6,119,458
Members
448,899
Latest member
maplemeadows

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