VBA Help - If Cell Contains Partial String Then.......

Johnny Thunder

Well-known Member
Joined
Apr 9, 2010
Messages
693
Office Version
  1. 2016
Platform
  1. MacOS
Hello all,

Have a question that I thought I could easily answer but for whatever reason it doesn't seem to be working so another pair of eyes may help.

I have a For Each Cell in Range Statement that looks at a Doc Type (2 Digit Variable) and when it finds "KR" to Search a Offset Cell for Partial Text, "P Card" if the code finds it to drop in the Text "P Card" in another cell.

Currently when I run the code it completely bypasses the cells with "P Card" and seems like it doesn't see it. When I modify the line to search for only "P" the code seems to work but also tags things that start with "P" that don't say "P Card". I have commented the line that is causing the issue.

An example of a P Card text: "P CARD 20190622" - This is what the code needs to flag with the "P Card" Text
A non P Card but Starts with "P" Text: "P Statement"


Code:
Sub PopulateNonPO()


Dim ws1             As Worksheet, ws2 As Worksheet
Dim LastR           As Long, LastR2 As Long
Dim Cell            As Range
Dim DocType     As String


Set ws1 = Sheets("M_T_KSB1")


LastR = ws1.Cells(Rows.Count, "C").End(xlUp).Row  '2 refers to the row to start on


    For Each Cell In ws1.Range("J2:J" & LastR)
        DocType = Cell.Value
    
            Select Case DocType
            Case "YA"
                Cell.Offset(0, 19).Value = "Accrual" 'Updates PO Field
            Case "SA"
                Cell.Offset(0, 19).Value = "JVWF"   'Updates PO Field
                Cell.Offset(0, 21).Value = Cell.Offset(0, 15).Value 'Drops in "Name" into Vendor Field
            Case "ZO"
                Cell.Offset(0, 19).Value = "T&E"    'Updates PO Field
             Case "KR"
                If InStr(1, Cell.Offset(0, 2), "*P Card*") > 0 Then   'Issue is here <------------------
                    Cell.Offset(0, 19).Value = "P Card"
                Else
                    Cell.Offset(0, 19).Value = "Pay Req"
                End If
            Case "KG"
                If InStr(1, Cell.Offset(0, 2), "P Card") Then
                    Cell.Offset(0, 19).Value = "P Card"
                Else
                    Cell.Offset(0, 19).Value = "Pay Req"
                End If
        End Select
    Next Cell
 
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
try without wildcard

Code:
If InStr(1, Cell.Offset(0, 2), "P Card) > 0 Then
 
Upvote 0
Just curious, was this intentionally written with only a single " ? I would imagine this to need " "
 
Upvote 0
sorry, typo error

Code:
If InStr(1, Cell.Offset(0, 2), [B][COLOR=#ff0000]"[/COLOR][/B]P Card[COLOR=#ff0000][B]"[/B][/COLOR]) > 0 Then
 
Upvote 0

Forum statistics

Threads
1,213,539
Messages
6,114,221
Members
448,554
Latest member
Gleisner2

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