Getting Index of Last instance of an element in arrary in VBA.

mathsbeauty

Board Regular
Joined
Apr 23, 2011
Messages
89
The array for example is
array = {12,,12,0,,12,0,,}
It contains some null empty characters. Here array contains 9 elements. I want to find the index of the last occurrence of element 12. So the answer would be 6 (assuming base 1). How could it be done?
 
Last edited:
I'll give it a go...
module code...
Code:
Public Function SplitFn(StrArr As String, Delim As String, Num As Integer) As Integer
Dim Arr As Variant, Cnt As Integer, Last As Integer, TempStr As String
'remove array brackets
TempStr = Right(Left(StrArr, Len(StrArr) - 1), Len(Left(StrArr, Len(StrArr) - 1)) - 1)
Arr = Split(TempStr, Delim)
For Cnt = LBound(Arr) To UBound(Arr)
If Arr(Cnt) = Num Then
Last = Cnt + 1 'option base 1
End If
Next Cnt
SplitFn = Last
End Function
Spreadsheet formula...
Code:
=SplitFn("{12,,12,0,,12,0,,}",",",12)
HTH. Dave
 
Upvote 0

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Forum statistics

Threads
1,225,233
Messages
6,183,756
Members
453,188
Latest member
amenbakr

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