VBA Help With an If Statement and having a Cell with multiple values to compare from

smithk901

New Member
Joined
May 26, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello,

Need help on trying to use an if statement that will compare one cell to the other. The issue is one cell has multiple answers separated by a comma and I need it to treat each as its own answer to compare against. How would I write this code?

Basically trying to compare R50 to U33 if R50 contains the value in one of the commas it should display a 1 on U50 (See picture Below)

What I have now is:


Dim K As Integer
For K = 50 To 149

If Range("R" & K).Value = Range("U33").Value Then
Range("U" & K).Value = 1
Else
Range("U" & K).Value = 0
End If
Next K


This statement only helps if the Cells are an exact match

1653622531528.png
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
If a formula is an option, then the following principle can be applied:

Book1
ABCD
1SelectionOption 1Option 2Option 3
2Option 1, Option 2, Option 3111
3Option 2010
4Option, 2, Option 1100
Sheet1
Cell Formulas
RangeFormula
B2:D4B2=COUNTIF($A2,"*"&B$1&"*")
 
Upvote 0
If a formula is an option, then the following principle can be applied:

Book1
ABCD
1SelectionOption 1Option 2Option 3
2Option 1, Option 2, Option 3111
3Option 2010
4Option, 2, Option 1100
Sheet1
Cell Formulas
RangeFormula
B2:D4B2=COUNTIF($A2,"*"&B$1&"*")
this helps, but what about when you have an option 13 or 11? this formula will still mark it as a 1 even though it is not an exact match
 
Upvote 0
Maybe

Pasta1
ABCD
1SelectionOption 1Option 2Option 3
2Option 1, Option 2, Option 3111
3Option 2010
4Option, 2, Option 1100
5Option 11, Option 3, Option 21001
Plan8
Cell Formulas
RangeFormula
B2:D5B2=--ISNUMBER(SEARCH(","&SUBSTITUTE(B$1," ","")&",",","&SUBSTITUTE($A2," ","")&","))


M.
 
Upvote 0

Forum statistics

Threads
1,214,566
Messages
6,120,266
Members
448,953
Latest member
Dutchie_1

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