VBA for Deleting entire rows if cell contains certain text or value

TheresavG

New Member
Joined
Aug 6, 2019
Messages
6
Please help

I need a vba in excel to the following please:
1 If Col C contains date starting with 2022-06-28 then keep it, delete the rest entire rows
2 Then if Col J Contains text " Declined" - Then delete whole row
3 Then if Col J Contains text " Refund" - Then the amount in Col G must get a Minus in front

So first I want to only keep transactions for a certain date starting with example: 2022-06-28
Then from those I want to delete all the declined lines in col J
and then I want to put a minus in front of Amounts In Col G if Col J shows " Declined"

I tried the XL2BB way it doesnt paste my mini sheet here, I also tried a jpeg but I cannot get it small enough to upload



Thank you so much

Theresa
 
Last edited by a moderator:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi Theresa,

This is the code I would you
VBA Code:
Sub RunMacro()
Dim LastRow As Long, X as long
LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row

For X = LastRow to 2 step -1  'This is assuming your start data at row 2 
    If cells(X, "J").value = " Declined" then
          cells(X, "J").entirerow.delete
    elseif cells(X, "J").value = " Refund" then
          cells(X, "G").value = -1 * cells(C, "G").value
    elseif cells(X, "C").value = "2022-06-28" then
          cells(X, "C").entirerow.delete
    end if

Next X

End Sub
 
Upvote 0
Hi Lexusap
Thanks for the fast reply
I copied it to my excel, changed the 2 to 4, cause the vba should only consider from col A4 (the rest will be headings)
But it doesnt seem to run, just does nothing,
Will still play around and see what happens
I just also want to know for the date the VBA must be setup so if the cell CONTAINS / STARTS WITH date as 2022-06-28 it should be kept, rest of the dates should be deleted (entire rows)
Do you have a mail then I can send you my sheet?
Im struggling to upload a picture?
Thanks for the help
Theresa
 
Upvote 0
I am assuming issue with LastRow . use this LastRow instead of the first one, assuming that you have no data on column A ?

VBA Code:
LastRow = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row
 
Upvote 0
Hi
I just copied it directly from Excel so you can have an idea of the data and cells
A1 is Exported data
A2 Showing
A3 Merchant
The data on which this VBA should run starts at A4


Exported data
Showing

the last


1000 receipt(s) for


Merchant

07867







MerchantTerminalReceiptDateTimeBatchNumberSequenceNumberReceiptNumberTotalAmount(R)CardUTITypeAuthCodeInvoiceNumberAction
VFVF (00118253)2022-06-29 09:28:05995055557.99Visa CardUTI0009SaleA00009View
VFVF (00118253)2022-06-29 09:19:56101094671224.55Debit MastercarUTI0010SaleA00010View
VFVF (00118253)2022-06-29 01:27:57161632802500.00Visa CreditUTI0016SaleA00016View
VFVF (00118253)2022-06-28 23:33:3117173279281.70Debit MastercarUTI0017SaleA00017View
VFVF (00118253)2022-06-28 21:34:36191932773000.00MASTERCARDUTI0019RefundA00019View
VFVF (00118253)2022-06-28 18:55:34232350441500.00MASTERCARDUTI0023RefundA00023View
VFVF (00118253)2022-06-28 18:55:34232350441500.00MASTERCARDUTI0023DeclinedA00023View
VFVF (00118253)2022-06-28 18:55:34232350441500.00MASTERCARDUTI0023RefundA00023View
VFVF (00118253)2022-06-26 18:55:35232350441500.00MASTERCARDUTI0024SaleA00024View
VFVF (00118253)2022-06-2918:55:36232350441500.00MASTERCARDUTI0025RefundA00025View
VFVF (00118253)2022-06-28 18:55:37232350441500.00MASTERCARDUTI0026DeclinedA00026View
VFVF (00118253)2022-06-28 18:55:38232350441500.00MASTERCARDUTI0027RefundA00027View
VFVF (00118253)2022-06-28 18:55:39232350441500.00MASTERCARDUTI0028SaleA00028View
VFVF (00118253)2022-06-26 18:55:40232350441500.00MASTERCARDUTI0029DeclinedA00029View
VFVF (00118253)2022-06-28 18:55:41232350441500.00MASTERCARDUTI0030DeclinedA00030View
VFVF (00118253)2022-06-26 18:55:42232350441500.00MASTERCARDUTI0031RefundA00031View
VFVF (00118253)2022-06-26 18:55:43232350441500.00MASTERCARDUTI0032DeclinedA00032View
VFVF (00118253)2022-06-26 18:55:44232350441500.00MASTERCARDUTI0033SaleA00033View
VFVF (00118253)2022-06-26 18:55:45232350441500.00MASTERCARDUTI0034DeclinedA00034View
VFVF (00118253)2022-06-26 18:55:46232350441500.00MASTERCARDUTI0035RefundA00035View
VFVF (00118253)2022-06-26 18:55:47232350441500.00MASTERCARDUTI0036DeclinedA00036View
VFVF (00118253)2022-06-26 18:55:48232350441500.00MASTERCARDUTI0037SaleA00037View
VFVF (00118253)2022-06-26 18:55:49232350441500.00MASTERCARDUTI0038RefundA00038View
VFVF (00118253)2022-06-26 18:55:50232350441500.00MASTERCARDUTI0039DeclinedA00039View
 
Upvote 0
Try this code !


VBA Code:
Sub RunMacro()
Dim LastRow As Long, X as long
LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row

For X = LastRow to 4 step -1  'Data start at row 4
    If cells(X, "J").value = "Declined" then     'I remove leading space from " Declined" to "Declined"
          cells(X, "J").entirerow.delete
    elseif cells(X, "J").value = "Refund" then     'I remove leading space from " Refund" to "Refund"
          cells(X, "G").value = -1 * cells(X, "G").value
    elseif Left(cells(X, "C"),10 ) <> "2022-06-28" then
          cells(X, "C").entirerow.delete
    end if

Next X

End Sub
 
Upvote 0
Solution
Thank you once again, I really appreciate the help

It now almost works

Only problem still have is it doesnt delete all other dates exept for 2022-06-28
Still see transactions with dates 2022-06-26 etch after I run the VBA
 
Upvote 0
Replace the date formula with this and see whether it works or not.

VBA Code:
elseif Trim(Left(cells(X, "C").Text, 10)) <> "2022-06-28" then
 
Upvote 0
Hi, Thank you so much,
It works now,
I just placed the Date formula before the Refund formula

Thanks again :)
 
Upvote 0
Sorry I just have one more question please

how would I write this formula if I want to keep dates for instance between lets say: 2022-06-24 until 2022-06-30 - rest to delete entire rows

elseif Trim(Left(cells(X, "C").Text, 10)) <> "2022-06-28" then

Thanks
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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