vba delete rows and find &replace based on criteria

djossh

Board Regular
Joined
Jul 27, 2009
Messages
243
hi. I would like to delete some rows based on the textbox value (as criteria) then to replace the next column with "cancel" please see below sample data

Sample data below (in this example i will input "224" as criteria in my textbox)
ABCD
1123XXX12541
2123DDS24524
3123GGS5424
4224HRR55465
5224SDFS64584
6415SDG2544
7415HRE56487
8415REE69756

<tbody>
</tbody>

RESULT data should be like this. (1 row with "224" was deleted, and retain 1 data but replace the column B with "Cancel" and other data were remove)

ABCD
1123XXX12541
2123DDS24524
3123GGS5424
4224CANCEL
5415SDG2544
6415HRE56487
7415REE69756

<tbody>
</tbody>

thanks in advance
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Your subject title say Delete Rows

But your image shows cancelled being entered.
So I assume you do not want to delete the row.
Otherwise there would be no purpose putting Cancelled into column B if we were to delete the row.

Try this:
Code:
Sub Look_For_Value()
'Modified  12/9/2018  10:32:37 PM  EST
Application.ScreenUpdating = False
Dim ans As String
ans = ActiveSheet.TextBox1.Value
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To Lastrow
    If Cells(i, 1).Value = ans Then
        Cells(i, 2).Value = "Canceled"
        Cells(i, 3).Resize(, 2).Value = ""
    End If
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
I assumed your data is sorted by column A.
Try this:

Code:
Sub a1080281b()
'https://www.mrexcel.com/forum/excel-questions/1080281-vba-delete-rows-find-replace-based-criteria.html
Dim n As Long, c As Long, d As Long
Dim rng As Range
Dim v As String


n = Range("A" & Rows.count).End(xlUp).Row
Set rng = Range("A1:A" & n)

v = 123
c = rng.Find(What:=v, LookIn:=xlValues, After:=Cells(n, "A"), lookAt:=xlWhole, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Row
    Cells(c, "B").Resize(1, 3).ClearContents
    Cells(c, "B") = "cancel"

d = rng.Find(What:=v, LookIn:=xlValues, After:=Cells(1, "A"), lookAt:=xlWhole, _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False, SearchFormat:=False).Row
    If d > c Then Range(Cells(c + 1, "A"), Cells(d, "A")).EntireRow.Delete


End Sub
 
Upvote 0
I assumed your data is sorted by column A.
Try this:

Code:
Sub a1080281b()
'https://www.mrexcel.com/forum/excel-questions/1080281-vba-delete-rows-find-replace-based-criteria.html
Dim n As Long, c As Long, d As Long
Dim rng As Range
Dim v As String


n = Range("A" & Rows.count).End(xlUp).Row
Set rng = Range("A1:A" & n)

v = 123
c = rng.Find(What:=v, LookIn:=xlValues, After:=Cells(n, "A"), lookAt:=xlWhole, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Row
    Cells(c, "B").Resize(1, 3).ClearContents
    Cells(c, "B") = "cancel"

d = rng.Find(What:=v, LookIn:=xlValues, After:=Cells(1, "A"), lookAt:=xlWhole, _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False, SearchFormat:=False).Row
    If d > c Then Range(Cells(c + 1, "A"), Cells(d, "A")).EntireRow.Delete


End Sub

THANK YOU SO MUCH! GOD Bless.
 
Upvote 0
Your subject title say Delete Rows

But your image shows cancelled being entered.
So I assume you do not want to delete the row.
Otherwise there would be no purpose putting Cancelled into column B if we were to delete the row.

Try this:
Code:
Sub Look_For_Value()
'Modified  12/9/2018  10:32:37 PM  EST
Application.ScreenUpdating = False
Dim ans As String
ans = ActiveSheet.TextBox1.Value
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To Lastrow
    If Cells(i, 1).Value = ans Then
        Cells(i, 2).Value = "Canceled"
        Cells(i, 3).Resize(, 2).Value = ""
    End If
Next
Application.ScreenUpdating = True
End Sub


Thank you!! God Bless
 
Upvote 0

Forum statistics

Threads
1,214,625
Messages
6,120,598
Members
448,973
Latest member
ksonnia

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