VBA to run macro only if specific text shows anywhere in cell.

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
774
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Greetings,

The cell I have below works perfect, and I just need to run the macro ONLY if cell "K6" contains the word "Domestic" anywhere within the cell. It can before or after another word, or after a special character.

The Code below is fine, I would love just to add this one little restriction before it.

Thank you,

VBA Code:
Sub Domestic_Operator()
   Dim i As Long, n As Variant, v As Variant
  
    For i = 6 To Split(Worksheets("MSN Decoder").UsedRange.Address, "$")(4)

        n = Mid(Worksheets("MSN Decoder").Cells(i, "K").Value, 4, 2) ''' Extracts the 4th and 5th characters

        v = Application.VLookup(n, Worksheets("Operator").Range("D:E"), 2, 0)

        If IsError(v) Then v = Application.VLookup(Val(n), Worksheets("Operator").Range("D:E"), 2, 0)

        If Not IsError(v) Then Worksheets("MSN Decoder").Cells(i + 4, "H").Value = v

    Next i

End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Maybe
Dim i As Long, n As Variant, v As Variant

IF Instr(Range.("K6"),"Domestic") > 0 Then
. . For... etc.

Be warned that UsedRange detects formatting, so if you have formatted cells 10,000 rows down, that's how far your loop is going.
 
Upvote 0
Maybe
Dim i As Long, n As Variant, v As Variant

IF Instr(Range.("K6"),"Domestic") > 0 Then
. . For... etc.

Be warned that UsedRange detects formatting, so if you have formatted cells 10,000 rows down, that's how far your loop is going.
Thank you, is there an issue with the brackets? I get an bracket or expression error.
 
Upvote 0
Remove the dot after Range --> Range("K6"), ...
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,021
Latest member
pingpong7117

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