Return error or message if a randomly created text contains a word that is contained on a separate a list

robbo2830

New Member
Joined
Jun 4, 2021
Messages
2
Office Version
  1. 2013
Platform
  1. Windows
I have to create a random 11 character string containing letters and numbers, which will be used to create a barcode that will be printed on an envelope. As the barcode text will also be printed it must not contain any profanities.
I have 2 tabs on my spreadsheet, tab one contains the randomly created barcode text. Tab two contains the list of profanities.
What I want to do is check if any of my barcodes contain any of the words that are on the list of profanities, and ideally return an error, or message of some sort.

The best I can do is find a specific word and return an error, which works. See below:

Dim i As Integer, intValueToFind As Integer
intValueToFind = anus
For i = 1 To 1500 ' Revise the 1500 to include all of your values
If Cells(i, 1).Value = intValueToFind Then
MsgBox ("Found value on row " & i)
Exit Sub
End If
Next i

' This MsgBox will only show if the loop completes with no success
MsgBox ("Value not found in the range!")
End Sub


What I want to change is this line:
intValueToFind = anus

So instead of looking for one word, it looks through all the words (1500 in total) in my second tab and returns an error if it sees any of them. The range I am searching in is:
=PROFANITY!A1:A1500

Many thanks in advance
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
13,924
Office Version
  1. 2007
Platform
  1. Windows
Hi and welcome to MrExcel.

Try this:

VBA Code:
Sub test()
  Dim a As Variant, b As Variant, c As Variant
  Dim sh1 As Worksheet
  Dim i As Long, j As Long
  '
  'tab name with randomly barcode text
  Set sh1 = Sheets("Sheet1")
  '
  a = sh1.Range("A1", sh1.Range("A" & Rows.Count).End(3)).Value
  b = Sheets("PROFANITY").Range("A1", Sheets("PROFANITY").Range("A" & Rows.Count).End(3)).Value
  ReDim c(1 To UBound(a, 1), 1 To 1)
  For i = 1 To UBound(a, 1)
    For j = 1 To UBound(b, 1)
      If InStr(1, a(i, 1), b(j, 1), vbTextCompare) > 0 Then
        c(i, 1) = b(j, 1)
        Exit For
      End If
    Next j
  Next i
  '
  'Output in column C
  sh1.Range("C1").Resize(UBound(a, 1)).Value = c
End Sub
 
Last edited:
Solution

robbo2830

New Member
Joined
Jun 4, 2021
Messages
2
Office Version
  1. 2013
Platform
  1. Windows
Hi and welcome to MrExcel.

Try this:

VBA Code:
Sub test()
  Dim a As Variant, b As Variant, c As Variant
  Dim sh1 As Worksheet
  Dim i As Long, j As Long
  '
  'tab name with randomly barcode text
  Set sh1 = Sheets("Sheet1")
  '
  a = sh1.Range("A1", sh1.Range("A" & Rows.Count).End(3)).Value
  b = Sheets("PROFANITY").Range("A1", Sheets("PROFANITY").Range("A" & Rows.Count).End(3)).Value
  ReDim c(1 To UBound(a, 1), 1 To 1)
  For i = 1 To UBound(a, 1)
    For j = 1 To UBound(b, 1)
      If InStr(1, a(i, 1), b(j, 1), vbTextCompare) > 0 Then
        c(i, 1) = b(j, 1)
        Exit For
      End If
    Next j
  Next i
  '
  'Output in column C
  sh1.Range("C1").Resize(UBound(a, 1)).Value = c
End Sub
Thank you very much
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
13,924
Office Version
  1. 2007
Platform
  1. Windows
I'm glad to help you. Thanks for the feedback.
 

Forum statistics

Threads
1,147,823
Messages
5,743,406
Members
423,792
Latest member
travisds

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
Top