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

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
17,651
Office Version
  1. 2013
Platform
  1. Windows
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
 

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
Thanks My Answer Is This, so simple :rolleyes:
 

Watch MrExcel Video

Forum statistics

Threads
1,132,685
Messages
5,654,746
Members
418,149
Latest member
amamiche67

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