VBA - find word one row above and clear all to the right

bobaol

Board Regular
Joined
Jun 3, 2002
Messages
225
Office Version
  1. 365
  2. 2003 or older
Platform
  1. Windows
Hello, I have column BA lined up with words. in the row above, it contains the same word, but in different places like BC, BG, etc. Is there VBA to quickly clear all the data in the above row to the right, when the word is found? The word will always be found. Take for example, go to BA43, find END date in BB42 to XFC42. when found, clear BC42 to BBW42. or, take BA44, go to BA44, find Business in BB43 to XFC43, when found, clear BK43 to BBW43. it does not have to end at BBW, it can end at 500 columns to the right. Any help is appreciated. thanks in advance.

1712762577481.png
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Can you share a sample dataset using XL2BB so we don't have to create a sample to test on?
 
Upvote 0
Try this on a copy. Not tested.
VBA Code:
Sub SearchAndDelete()
    Dim a
    Dim i As Long, j As Long, k As Long
    Dim ws As Worksheet
 
    Set ws = ThisWorkbook.Worksheets("Sheet2") 'change sheet as needed
    a = ws.Range("BA42").CurrentRegion.Value 'assume cell starts in BA42
 
    For i = 1 To UBound(a, 1)
        For j = 1 To UBound(a, 1)
            For k = 2 To UBound(a, 2)
                If a(i, 1) = a(j, k) Then a(j, k) = ""
            Next k
        Next j
    Next i
 
    ws.Range("BA42").Resize(UBound(a, 1), UBound(a, 2)).Value = a
 
End Sub
 
Upvote 0
Hi Cubist, thanks for your response. the code did not work for me. here is a copy of the sample data. Thanks again in advance.
''cell BA41'cell BR40
Row4REG02xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxStart Date
Start DateEnd Datexxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
End DateStatusApproved
45291​
45304​
Show previous row (inactive button)1-7 of 7Show next row (inactive button) View AllBusiness Unitxxxxxxxxxxxxxxxxxxxxxxxx
Business UnitRow1xxxxxxXXXxxxxxxxxx
10​
0​
xxxxxxxxxxxxxxxxxxxxxxxx
Row1REG02xxxxxxAuto Approved
47848​
xxx
10​
0​
8​
Row2xxxxxxxxxxxxxxxxxxxxx
Row2REG02xxxxxxAuto Approved
47848​
xxx
5​
0​
4​
Row3xxxxxxxxxxxxxxxxxxxxx
Row3REG02xxxxxxApproved
47848​
xxx
25​
0​
20​
Row4xxxxxxxxxxxxxxxxxxxxx
Row4REG02xxxxxxApproved
47848​
xxx
20​
0​
16​
Row5xxxxxxxxxxxxxxxxxxxxx
Row5REG02xxxxxxApproved
48944​
xxx
5​
0​
4​
Row6xxxxxxxxxxxxxxxxxxxxx
Row6REG02xxxxxxAuto Approved
46446​
xxx
25​
0​
20​
Row7xxxxxxxxxxxxxxxxxxxxx
Row7REG02xxxxxxAuto Approved
48760​
xxx
10​
0​
8​
Total Dist%xxxxxxxxxxxxxxxxxxxxx
''cell BA52
 

Attachments

  • clearToRight.PNG
    clearToRight.PNG
    32.5 KB · Views: 22
Upvote 0
It works fine for me. In what way it didn't work?
Book1
BABBBCBDBEBFBGBHBIBJBKBLBMBNBOBPBQBR
42Row4REG02xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
43Start Datexxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
44End DateStatusApproved4529145304Show previous row (inactive button)1-7 of 7Show next row (inactive button) View Allxxxxxxxxxxxxxxxxxxxxxxxx
45Business UnitxxxxxxXXXxxxxxxxxx100xxxxxxxxxxxxxxxxxxxxxxxx
46Row1REG02xxxxxxAuto Approved47848xxx1008xxxxxxxxxxxxxxxxxxxxx
47Row2REG02xxxxxxAuto Approved47848xxx504xxxxxxxxxxxxxxxxxxxxx
48Row3REG02xxxxxxApproved47848xxx25020xxxxxxxxxxxxxxxxxxxxx
49Row4REG02xxxxxxApproved47848xxx20016xxxxxxxxxxxxxxxxxxxxx
50Row5REG02xxxxxxApproved48944xxx504xxxxxxxxxxxxxxxxxxxxx
51Row6REG02xxxxxxAuto Approved46446xxx25020xxxxxxxxxxxxxxxxxxxxx
52Row7REG02xxxxxxAuto Approved48760xxx1008Total Dist%xxxxxxxxxxxxxxxxxxxxx
Sheet2
 
Upvote 0
Hi Cubist, thank you so much for your code and input. I re-ran it again and it mostly worked. what i want is to delete everything to the right, except the word that is found. For example, in cell BF44, I want to keep BF44, but delete the 9999 cells to the right. thanks in advance again.
 
Upvote 0
I misunderstood what you wanted. Will there ever be duplicates? For example, Row4 appears multiple times.
 
Upvote 0
Not as compact as Cubist but see if this works for you.
I have coded the row to start at, as being 42, so you will need to change that to the 1st row in your actual dataset.

VBA Code:
Sub FindValueDeleteToEnd()

    Dim ws As Worksheet
    Dim rng As Range
    Dim arr As Variant
    Dim findString As String, foundYN As String
    Dim rowFirst As Long, rowLast As Long, colLast As Long
    Dim i As Long, j As Long
    
    Set ws = ActiveSheet                                        ' <--- ideally change to Worksheets("NameOfSheet")
    
    With ws
        rowFirst = 42                                           ' <--- Change to 1st row containing data to be checked
        rowLast = .Range("BA" & Rows.Count).End(xlUp).Row
        colLast = .Cells.Find(what:="*" _
            , Lookat:=xlPart _
            , LookIn:=xlFormulas _
            , searchorder:=xlByColumns _
            , searchdirection:=xlPrevious).Column
        Set rng = .Range(.Cells(rowFirst, "BA"), .Cells(rowLast, colLast))
        arr = rng.Value
    End With
    
    For i = 1 To UBound(arr) - 1
        findString = arr(i + 1, 1)                              ' Value from next row, 1st column in array
        foundYN = "N"
        For j = 2 To UBound(arr, 2)
            If arr(i, j) = findString Then
                foundYN = "Y"
            ElseIf foundYN = "Y" Then
                arr(i, j) = ""
            End If
        Next j
    Next i
    
    rng.Value = arr

End Sub
 
Upvote 0
Solution
Thank you both Alex and Cubist for your help. Alex's code worked perfectly.
 
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