VBA Find first instance of word

decadence

Well-known Member
Joined
Oct 9, 2015
Messages
525
Office Version
  1. 365
  2. 2016
  3. 2013
  4. 2010
  5. 2007
Platform
  1. Windows
Hi, I have a column of data and every cell may or may not contain specific words, what I am trying to do is once the first instance of a word (from an array) is found then insert a column before the data column then exit completely out of the loop without looking in any other cells in the data column, Can someone help with this please. This is what I am using

Code:
Sub InsertByWord()
    '
    Dim RRng as range, oCell as Range, Arr as Variant, k As Integer

    Arr = Array("*Apple*", "*Banana*", "*Orange*")
    Set RRng = ActiveSheet.range("C:C")
    For k = 0 To 2
        For Each oCell In RRng
            If oCell.Value Like Arr(k) Then
                LR = oCell.Row
                    RRng.Offset(0, -1).EntireColumn.Insert
                Exit For
            End If
        Next oCell
    Next k
End Sub
 
Last edited:

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Try this:
Exit Sub
Not Exit Loop

Code:
Sub InsertByWord()
'Modified  7/19/2018  4:16:42 AM  EDT
    Dim RRng As Range, oCell As Range, Arr As Variant, k As Integer
    Arr = Array("*Apple*", "*Banana*", "*Orange*")
    Set RRng = ActiveSheet.Range("C:C")
    For k = 0 To 2
        For Each oCell In RRng
            If oCell.Value Like Arr(k) Then
                LR = oCell.Row
                    RRng.Offset(0, -1).EntireColumn.Insert
                Exit Sub
            End If
        Next oCell
    Next k
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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