Why do Index and Match do not work when you've multiple numbers in Cell

nsa1

New Member
Joined
Jul 11, 2023
Messages
39
Office Version
  1. 2010
Hello,

I am trying to figure out if Index and Match function can work for indexing numbers separated by "," to be assign..

See the snapshot below

Thanks,
NA
 

Attachments

  • IndexMatch.png
    IndexMatch.png
    25 KB · Views: 21
Try the following UDF:


VBA Code:
Function IndexWithComma(rng As Range, cell As String)
  Dim c As Range
  Dim v As Variant, n As Variant
  Dim i As Long
 
  For Each c In rng.Columns(2).Cells
    v = Split(Replace(c.Cells(1).Value, Chr(10), ","), ",")
    n = Split(c.Offset(, -1).Cells(1).Value, ",")
    If UBound(v) = UBound(n) Then
      For i = 0 To UBound(v)
        If LCase(v(i)) = LCase(cell) Then
          IndexWithComma = n(i)
          Exit Function
        End If
      Next
    End If
  Next
  IndexWithComma = ""
End Function


HOW TO INSTALL UDFs (User Defined Functions)
If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use IndexWithComma just like it was a built-in Excel function.​
For example:

Dante Amor
ABCDEFGHIJ
1NumberVariableVariableAYmhellobyez
21,2,3X Y ZValue42 673
34A
45B
56,7hello bye
6
7
8
9
10
Hoja3
Cell Formulas
RangeFormula
E2:J2E2=IndexWithComma($A$2:$B$10,E1)


--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------
Thanks Dante! I ALT+F11 don't pop up anything? Do I click on Developer and click on Visual Basic?
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Do I click on Developer and click on Visual Basic?
Yes please.

Then:
once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use IndexWithComma just like it was a built-in Excel function.
 
Upvote 0
Yes please.

Then:
once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use IndexWithComma just like it was a built-in Excel function.
Thanks. I added that code and saved it. Then went to my Sheet but saw no difference?
 

Attachments

  • VBA.png
    VBA.png
    77.4 KB · Views: 5
  • Split.png
    Split.png
    33.1 KB · Views: 5
Upvote 0
We start from the beginning.

Please follow the steps below:

1. Click on Developer
2. Click on Visual Basic
3. In the VBA menu select the Insert option and then the Module option
1691681166853.png



4. In new panel, you paste the code.
VBA Code:
Function IndexWithComma(rng As Range, cell As String)
  Dim c As Range
  Dim v As Variant, n As Variant
  Dim i As Long
 
  For Each c In rng.Columns(2).Cells
    v = Split(Replace(c.Cells(1).Value, Chr(10), ","), ",")
    n = Split(c.Offset(, -1).Cells(1).Value, ",")
    If UBound(v) = UBound(n) Then
      For i = 0 To UBound(v)
        If LCase(v(i)) = LCase(cell) Then
          IndexWithComma = n(i)
          Exit Function
        End If
      Next
    End If
  Next
  IndexWithComma = ""
End Function

5. You return to excel and in cell E2 you delete the formula you have and put this formula:

Excel Formula:
=IndexWithComma($A$2:$B$10,E1)

6. Copy the formula to the right to cell G2

😇
--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------
 
Upvote 0
We start from the beginning.

Please follow the steps below:

1. Click on Developer
2. Click on Visual Basic
3. In the VBA menu select the Insert option and then the Module option



4. In new panel, you paste the code.
VBA Code:
Function IndexWithComma(rng As Range, cell As String)
  Dim c As Range
  Dim v As Variant, n As Variant
  Dim i As Long
 
  For Each c In rng.Columns(2).Cells
    v = Split(Replace(c.Cells(1).Value, Chr(10), ","), ",")
    n = Split(c.Offset(, -1).Cells(1).Value, ",")
    If UBound(v) = UBound(n) Then
      For i = 0 To UBound(v)
        If LCase(v(i)) = LCase(cell) Then
          IndexWithComma = n(i)
          Exit Function
        End If
      Next
    End If
  Next
  IndexWithComma = ""
End Function

5. You return to excel and in cell E2 you delete the formula you have and put this formula:

Excel Formula:
=IndexWithComma($A$2:$B$10,E1)

6. Copy the formula to the right to cell G2

😇
--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------
Thanks Dante for your patience. I am doing this VBA thing for the first time. So, appreciate all your help and patience. I added the code in the right place but didn't have .. =IndexWithComma($A$2:$B$10, G1). Well this formula gives me #NAME? fault?
 

Attachments

  • VBAtrble.png
    VBAtrble.png
    36.5 KB · Views: 6
Upvote 0
You're not adding the code in the right place, that's why it's giving you the "Name" error.

Start again with the steps.
You must put the code in a Module.
 
Upvote 0
You're not adding the code in the right place, that's why it's giving you the "Name" error.

Start again with the steps.
You must put the code in a Module.
Ok. I believe its in the correct location now...but still an error
1691683141822.png
 
Upvote 0
Ok, now the code is in the right place in Module4.

But now, what does the error message say?

You could show both images the cell with the formula.

Or simply copy the following onto a clean sheet:

varios 10ago2023.xlsm
ABCDEFGHIJ
1NumberVariableVariableAYmhellobyez
21,2,3X Y ZValue42 673
34A
45B
56,7hello bye
Hoja1
Cell Formulas
RangeFormula
E2:J2E2=IndexWithComma($A$2:$B$10,E1)


To copy, click on the icon and paste on a clean sheet
1691683756147.png



🤗
 
Upvote 0
I can see that now you have 4 modules, if you added the code in more than one module that will cause another type of error.
Double click on each module, if you see the code then delete it, only the code should remain in one module.
You return to excel and put the formula.
That must work.


1691684665848.png


🫡
 
Upvote 0
Ok, now the code is in the right place in Module4.

But now, what does the error message say?

You could show both images the cell with the formula.

Or simply copy the following onto a clean sheet:

varios 10ago2023.xlsm
ABCDEFGHIJ
1NumberVariableVariableAYmhellobyez
21,2,3X Y ZValue42 673
34A
45B
56,7hello bye
Hoja1
Cell Formulas
RangeFormula
E2:J2E2=IndexWithComma($A$2:$B$10,E1)


To copy, click on the icon and paste on a clean sheet
View attachment 96964


🤗
Same Error in a new clean sheet
1691687457200.png
 
Upvote 0

Forum statistics

Threads
1,224,833
Messages
6,181,242
Members
453,026
Latest member
cknader

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