UDF - How to write a CONTAINS function in VBA

Sphinx404

Board Regular
Joined
May 2, 2015
Messages
186
Office Version
  1. 365
Platform
  1. Windows
Experts, I need you... I have no idea what I'm doing here.

Sheet1 Column A = Production #
Sheet 2 Column B = Kit #
Sheet2 Column A = Production #
Sheet2 Column B = Kit # (multiple values in single cell)

I need to lookup the production # in sheet 1, match it against the production # in sheet2, then if it's found, I need see if the Kit # in Sheet1 Col. B is found in Sheet2 Col. B

=IF Sheet1!A2 is found in Sheet2!A:A then lookup value Sheet1!B2 in the cell next to the production# (Sheet2!B:?), if value from Sheet1!B2 is found, then say "found", 0)

Does anyone have an idea on how I can accomplish this... either in Excel or VBA?

Thanks!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
How about
Code:
Function GetKit(Rng As Range)
   Dim fnd As Range
   GetKit = 0
   Set fnd = Sheets("new").Range("A:A").Find(Rng.Value)
   If Not fnd Is Nothing Then
      If InStr(1, fnd.Offset(, 1), Rng.Offset(, 1), vbTextCompare) > 0 Then GetKit = "Found"
   End If
End Function
 
Upvote 0
Thank you for responding Fluff. It is appreciated.

I was able to run a function that matched the Production #s and brought both Kit #s over to my main sheet ("AQE Data")

Now I have the KIT # I'm looking for in Column R, and the array of Kit's in Column S. Now I just need to compare or ask Excel (via your function) to look at the data.

Is column R (single value) found in column S (multiple values)

I apologize for my ignorance, but I don't even know how to decipher your function to make this work.

Thanks again.
 
Upvote 0
What sort of values do you have for the Kits?
 
Upvote 0
Example:

Column R
313A
319c
311D
357B
312B
297d
317a
315A

Column S
343b, 354b
341A,358A
358
358c, 362, 345a, 341B
336b, 359D
317a,297D, 357B
 
Upvote 0
In that case try
Code:
Function GetKit(Rng As Range)
   Dim fnd As Range
   Application.Volatile
   GetKit = 0
   If InStr(1, Rng.Offset(, 1), Rng, vbTextCompare) > 0 Then GetKit = "Found"
End Function
In a blank cell type =GetKit(R13)
 
Upvote 0
You know when you create a UDF in VB? When you type in your parameter... in this case "GetKit" or more specifically "=GetKit"... nothing comes up... like it cannot find the function. I'm not expert, but shouldn't a UDF come up automatically when you start typing it in... (i.e. =GetKit.... and as you type it auto-populates for you to select?)

I type in =GetKit and Excel doesn't recognize the function. So I type it in anyway and all I get is #NAME ?

I typed it in just as you stated in your previous reply.
 
Upvote 0
The function needs to go in a regular module. I suspect that you have put it in a sheet module, or the ThisWorkbook module
 
Upvote 0
Ahh, yes of course. Perfect. Works perfectly, thank you Fluff!
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,244
Members
452,622
Latest member
Laura_PinksBTHFT

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