excel VBA finding cells that contains multiple word and write at desired cell

huiyin9218

Board Regular
Joined
Aug 7, 2018
Messages
53
Hi,

How do i write a code to find multiple words (for example: AM and PM) in a column and assign a number "1" at the next 2 columns.
I'm able to find "AM" and assign 1 next to it but i don't know how to add PM into the code and move the number "1" to the next column.
I would be so grateful for your help.


Sub MarkText()
Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets

Set ws = ThisWorkbook.Sheets("Sheet1")

Const Text2Find = "AM"
Dim cel As Range
For Each cel In ws.UsedRange.Columns("A").Cells
If InStr(cel.Value, Text2Find) > 0 Then
cel.Offset(0, 1) = 1
Else
cel.Offset(0, 1).ClearContents
End If
Next cel
Next ws
End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I think this will do what you want. You can include additional words where noted by a comment.
Code:
Sub MarkText()
Dim ws As Worksheet, Wrds As Variant
Dim cel As Range

Wrds = Array("AM", "PM")   'Add additional words, if any, here
Application.ScreenUpdating = False
For Each ws In ThisWorkbook.Worksheets
    For Each cel In ws.UsedRange.Columns("A").Cells
        For i = 0 To UBound(Wrds)
            If InStr(cel.Value, Wrds(i)) > 0 Then cel.Offset(0, i + 1) = 1
        Next i
    Next cel
Next ws
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try this. Please note that the code only works if "AM" and "PM" are manually entered. If they appear because of formatting, the code will not find them.

Code:
Sub MarkText()
    Dim ws As Worksheet
   
    For Each ws In ThisWorkbook.Worksheets
   
    Set ws = ThisWorkbook.Sheets("Sheet1")
   
    'Const Text2Find = "AM"
    Dim cel As Range
    For Each cel In ws.UsedRange.Columns("A").Cells
        If InStr(cel.Value, "AM") > 0 Then
            cel.Offset(0, 1) = 1
        ElseIf InStr(cel.Value, "PM") > 0 Then
            cel.Offset(0, 2) = 1
        Else
            cel.Offset(0, 1).ClearContents
        End If 
    Next cel
    Next ws
End Sub
 
Last edited:
Upvote 0
You had two responses to your post. As a courtesy to those who have responded, can you let us know which response is "It"??

Oh, Sorry. yky's code works for me, JoeMo's works too but the "1"s came out at two different column. I'm still really grateful for the help given. Thank you so much!
 
Upvote 0
Oh, Sorry. yky's code works for me, JoeMo's works too but the "1"s came out at two different column. I'm still really grateful for the help given. Thank you so much!
In your initial post you wrote: "I'm able to find "AM" and assign 1 next to it but i don't know how to add PM into the code and move the number "1" to the next column.
That's what the code I posted does. If you have both "AM" and "PM" in a single cell, my code will produce a "1" in the adjacent column and another "1" in the column next to that.
 
Upvote 0
Sorry for my misleading post. I was trying to have all the "1"s in the same column.
I tried it again and made one small change, it works :)

Sub MarkText()
Dim ws As Worksheet, Wrds As Variant
Dim cel As Range


Wrds = Array("AM", "PM") 'Add additional words, if any, here
Application.ScreenUpdating = False
For Each ws In ThisWorkbook.Worksheets
For Each cel In ws.UsedRange.Columns("A").Cells
For i = 0 To UBound(Wrds)
If InStr(cel.Value, Wrds(i)) > 0 Then cel.Offset(0, 1) = 1
Next i
Next cel
Next ws
Application.ScreenUpdating = True
End Sub


Thank you for your help
 
Upvote 0
Sorry for my misleading post. I was trying to have all the "1"s in the same column.
I tried it again and made one small change, it works :)

Sub MarkText()
Dim ws As Worksheet, Wrds As Variant
Dim cel As Range


Wrds = Array("AM", "PM") 'Add additional words, if any, here
Application.ScreenUpdating = False
For Each ws In ThisWorkbook.Worksheets
For Each cel In ws.UsedRange.Columns("A").Cells
For i = 0 To UBound(Wrds)
If InStr(cel.Value, Wrds(i)) > 0 Then cel.Offset(0, 1) = 1
Next i
Next cel
Next ws
Application.ScreenUpdating = True
End Sub


Thank you for your help
You are welcome.

Maybe it works for your purposes, but you're missing the point. With your modification you don't know which word was found in any specific cell that has one or perhaps both words in it because you overwrite the "1" from the first word with a "1" for the second word. And, you have negated the ability to add more words and know which of them might be in any given cell.
 
Upvote 0
I see. excel vba is new to me as I just start learning it recently. Thank you for your guidance, I will take note of it :)
 
Upvote 0

Forum statistics

Threads
1,215,430
Messages
6,124,849
Members
449,194
Latest member
HellScout

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