Problem with line of code (If Cell Like "*part of string*" problem...)

kbishop94

Active Member
Joined
Dec 5, 2016
Messages
458
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I have this code that works:

Code:
Dim Cell As Range
    For Each Cell In Range(Cells(6, 4), Cells(45, 58))
        If Cell Like "*CSR18R*" Then Cell.Value = "CSR" Then Cell.Value = "CSR - received incorrect product:"

I am attempting to spilt the returned value into 2 cells, like this:

Code:
If Cell Like "*CSR18R*" Then Cell(-1).Value = "CSR" And Cell.Value = "received incorrect product"

I get an error, however.

I tried several different ways, including referencing the row that the cells are in (still no luck:)

Code:
If Cell Like "*CSR18R*" Then Row(Cell - 1).Value = "CSR" And Row(Cell).Value = "received incorrect product"

Intended result should look like this:

$15.PNG

How do I fix this? Thank you
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
How about
VBA Code:
If cell Like "*CSR18R*" Then
   cell.Offset(, -1).Value = "CSR"
   cell.Value = "received incorrect product"
End If
 
Upvote 0
I just noticed a flubbed up the first code I have where I said it 'works'. here is what it should look like:

Code:
If Cell Like "*CSR18R*" Then Cell.Value = "CSR - received incorrect product"

That is what WORKS.

What doesn't work are the other examples I posted where I am attempting to split the returned result into two different cells that are beside each other.
 
Upvote 0
How about
VBA Code:
If cell Like "*CSR18R*" Then
   cell.Offset(, -1).Value = "CSR"
   cell.Value = "received incorrect product"
End If
Thank you, Fluff. That works(!)

However...

I only showed 1 line of the code that changes the strings it is searching for...

Unfortunately I am actually having the code search for about a hundred different, and separate individual strings. Do I still need to adjust it accordingly using the script you posted, or is there another way if my code includes multiple 'If Cell Like' lines like I actually have?
here's a smidge of the code I'm talking about
Code:
Dim Cell As Range
For Each Cell In Range(Cells(6, 4), Cells(45, 58))
        If Cell Like "*CSR0o*" Then Cell.Value = "CSR related incidents"
        If Cell Like "*CSR21U*" Then Cell.Value = "labeling incidents (CSR)"
        If Cell Like "*CSR22V*" Then Cell.Value = "packaging incidents (CSR) "
        If Cell Like "*CSR23W*" Then Cell.Value = "product related incidents (CSR) "
        If Cell Like "*CSR24X*" Then Cell.Value = "miscellaneous incidents (CSR) "
        If Cell Like "*SEA0o*" Then Cell.Value = "INTERNAL seatex incidents"
        If Cell Like "*MIS0o*" Then Cell.Value = "MISCELLANEOUS incidents"
        If Cell Like "*EHS0o*" Then Cell.Value = "EHD related incidents"
        If Cell Like "*EHS1A*" Then Cell.Value = "injury related incidents"
        If Cell Like "*SEA2B*" Then Cell.Value = "process deviations"
        If Cell Like "*SEA1A*" Then Cell.Value = "all process related"
        If Cell Like "*MIS7G*" Then Cell.Value = "monthly damage"
        If Cell Like "*MIS8H*" Then Cell.Value = "monthly damage"
        If Cell Like "*CSR1A*" Then Cell.Value = "CSR - hazardous labeling problem"
        If Cell Like "*CSR2B*" Then Cell.Value = "CSR - wrong or incorrect labels"
        If Cell Like "*CSR3C*" Then Cell.Value = "CSR - missing labels"
        If Cell Like "*CSR4D*" Then Cell.Value = "CSR - label placement problem"
        If Cell Like "*CSR5E*" Then Cell.Value = "CSR - lids, caps or bungs related"
 
Upvote 0
You could do it like
VBA Code:
If cell Like "*CSR18R*" Then
   cell.Offset(, -1).Value = "CSR"
   cell.Value = "received incorrect product"
ElseIf cell Like "*CSR0o*" Then
   cell.Offset(, -1).Value = "CSR"
   cell.Value = "related incidents"
ElseIf cell Like "*CSR21U*" Then
   '...
End If
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,421
Messages
6,119,392
Members
448,891
Latest member
tpierce

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