Get array index for selected item

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Code:
Sub TestIndex()
Dim arr, List$, i%

List = "10,12,14,16,18"

arr  = Split(List, ",")

For i  = 10 To 18 Step 2
       Select Case i 
                  Case LBound(arr) To UBound(arr)
                   MsgBox arr 'Here, I want msgbox show say 0 to 4
       End Select
Next i 
End Sub

So when the i reach 10, I want the msgbox show 0. In that other.

I don't know what to do to get that. Can someone help?
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
HI
the method of select For i = 10 To 18 Step 2 Select Case i
Code:
 Select Case i 
                 Case arr(1)
                   MsgBox "0"
       End Select
 
Last edited:
Upvote 0
It is very unclear what you are actually trying to do - or why. :confused:

In any case, if List = "10,12,10, 16,18", what result would you expect for "10" - 0 or 2?
 
Upvote 0
As Peter has said, it's unclear what you are trying to do, but with your example maybe
Code:
Sub TestIndex()
Dim arr, List$, i%

List = "10,12,14,16,18"

arr = Split(List, ",")

For i = 10 To 18 Step 2
      MsgBox Application.Match(CStr(i), arr, 0) - 1
Next i
End Sub
 
Upvote 0
HI
the method of select For i = 10 To 18 Step 2 Select Case i
Code:
 Select Case i 
                 Case arr(1)
                   MsgBox "0"
       End Select

Code:
Sub TestIndex()
Dim arr, List$, i%

List = "10,12,14,16,18"

arr  = Split(List, ",")

For i  = 10 To 18 Step 2
       Select Case i 
                  Case arr(LBound(arr)) To arr(UBound(arr))
                   MsgBox arr 'Here, I want msgbox show say 0 to 4
       End Select
Next i 
End Sub

Okay but I want a way to loop instead of writing many case statements. There was an error with my first code.
 
Upvote 0
As Peter has said, it's unclear what you are trying to do, but with your example maybe
Code:
Sub TestIndex()
Dim arr, List$, i%

List = "10,12,14,16,18"

arr = Split(List, ",")

For i = 10 To 18 Step 2
      MsgBox Application.Match(CStr(i), arr, 0) - 1
Next i
End Sub


Yeah!!!

You nailed it!!!!!!

This is what I want to get!!!!!

I appreciate that
 
Upvote 0
Glad we could help & thanks for the feedback.

However if you have the same value more than once in the list, it will report the first match.
 
Last edited:
Upvote 0
It is very unclear what you are actually trying to do - or why. :confused:

In any case, if List = "10,12,10, 16,18", what result would you expect for "10" - 0 or 2?

I think you overlooked the list:).

There is only one 10 in the list. By the way Fluff's code has taken care of the issue.
 
Upvote 0
No, I didn't overlook the list. I was asking what you would want to happen if the list was different and had repeated values.

Oh okay. Nice point over there. But that will not happen in the current universe I am living in. :cool:
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,186
Members
452,615
Latest member
bogeys2birdies

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