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

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
743
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 Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
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,215,608
Messages
6,125,820
Members
449,265
Latest member
TomasTeix

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