VBA Whitelist function

sky3205

New Member
Joined
Oct 10, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello,
I'm trying to figure out a solution for this:

I have a table similiar to this one and need to have a "whitelist" that can filter every line from the J column and replace it with "DONE" if it is not on the whitelist.


I currently have a simple vba written for a blacklist but because I have so many entries it is getting to long...


This is what I have:

VBA Code:
Find_Text = "brown"
Replace_Text = "done"

Set Rng = Range("J2:J190")

For i = 1 To Rng.Rows.Count
    For j = 1 To Rng.Columns.Count
        Count = 0
        For k = 1 To Len(Rng.Cells(i, j))
            If Mid(Rng.Cells(i, j), k, Len(Find_Text)) = Find_Text Then
                Count = Count + 1
            End If
        Next k
        If Count > 0 Then
            For k = 1 To Len(Rng.Cells(i, j)) + Count * Abs(Len(Replace_Text) - Len(Find_Text)) - Len(Find_Text) + 1
                If Mid(Rng.Cells(i, j), k, Len(Find_Text)) = Find_Text Then
                    Rng.Cells(i, j) = Left(Rng.Cells(i, j), k - 1) + Replace_Text + Right(Rng.Cells(i, j), Len(Rng.Cells(i, j)) - k + 1 - Len(Find_Text))
                    k = k + Len(Replace_Text)
                End If
            Next k
        End If
    Next j
Next i

Find_Text = "grey"
Replace_Text = "tareas tratadas"

Set Rng = Range("J2:J190")

For i = 1 To Rng.Rows.Count
    For j = 1 To Rng.Columns.Count
        Count = 0
        For k = 1 To Len(Rng.Cells(i, j))
            If Mid(Rng.Cells(i, j), k, Len(Find_Text)) = Find_Text Then
                Count = Count + 1
            End If
        Next k
        If Count > 0 Then
            For k = 1 To Len(Rng.Cells(i, j)) + Count * Abs(Len(Replace_Text) - Len(Find_Text)) - Len(Find_Text) + 1
                If Mid(Rng.Cells(i, j), k, Len(Find_Text)) = Find_Text Then
                    Rng.Cells(i, j) = Left(Rng.Cells(i, j), k - 1) + Replace_Text + Right(Rng.Cells(i, j), Len(Rng.Cells(i, j)) - k + 1 - Len(Find_Text))
                    k = k + Len(Replace_Text)
                End If
            Next k
        End If
    Next j
Next i

and so on...
 

Attachments

  • example table.jpg
    example table.jpg
    150.2 KB · Views: 6

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
It seems like you're just doing a lot of find/replace actions, unless I'm missing something? The following code creates two zero-based arrays containing 3 items (0, 1 and 2) called findlist and replacelist. You can add as many find/replace values as you want, just adjust the code accordingly. If you had 100 words, the Dim statements should reflect 99, and the assignment lines would go from 0 to 99 (100 entries).

This replaces "blue" with "done", "red" with "not done", and "yellow" with "" in range J2:J190.
VBA Code:
Sub sky()
Dim findlist(2) As Variant, replacelist(2) As Variant, i As Long, j As Long

findlist(0) = "blue"
replacelist(0) = "done"
findlist(1) = "red"
replacelist(1) = "not done"
findlist(2) = "yellow"
replacelist(2) = ""

For i = 2 To 190
    For j = 0 To UBound(findlist)
        Cells(i, 10).Replace What:=findlist(j), Replacement:=replacelist(j), LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Next j
Next i

End Sub
 
Upvote 0

Forum statistics

Threads
1,213,517
Messages
6,114,089
Members
448,548
Latest member
harryls

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