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:

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
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?

Hi,
Try using Ubound - it returns #elements in array. The index is that value -1. Here's an example
Code:
Dim lastindex&
Dim arr
arr = {12,,12,0,,12,0,,}
lastindex= Ubound(arr)-1
 
Upvote 0
I would loop in the array starting from the last element, and look for the value:

Something like this:
Note: Array index starts from 0 as default, so I added 1 to the found index number. If starting number is mandatory, then Option Base 1 can be also used.

Code:
Sub sample()
Dim a
Dim lookFor As Integer
Dim i As Integer


    a = Array(12, Null, 12, 0, Null, 12, 0, Null, Null)
    lookFor = 12
    For i = UBound(a) To 0 Step -1
        If a(i) = lookFor Then
            MsgBox (i + 1)
            Exit For
        End If
    Next i
End Sub
 
Upvote 0
Hi again,
Just to let you know. The array is index is count from 0 unless you set the following parameter in the mode above subs "Option Base = 1" - than ot counts from 1 on.
Within the code I wrote in previous thread I assumed that you did not set that parameter :)

Regards
Sebastian
 
Upvote 0
Thanks for reply. If array contains only 12 and Null, could we avoid looping? It doesn't contain any other elements. I need to use this in formula.
 
Upvote 0
Hi,
I got you wrong. Thought you wanted to get last index array but you want to get array index of last 12 occurrence. If you want to get the index of last occurrence whilst you do not have a clue where it's actually in the array, you will not avoid looping.
I would go from the last index to first. Here's an example:
Code:
Dim i&, lastindex&
Dim arr
arr= {12,,12,0,,12,0,,}
For i=Ubound(arr) to 1 step -1
    If arr(i-1)=12 then
           lastindex=i-1
           Exit for
    End if
Next element
 
Upvote 0
Hi,
Small fix. I've written at next statement element instead of i :)
Code:
Dim i&, lastindex&
Dim arr
arr= {12,,12,0,,12,0,,}
For i=Ubound(arr) to 1 step -1
    If arr(i-1)=12 then
           lastindex=i-1
           Exit for
    End if
Next i
 
Upvote 0
Hi,
Small fix. I've written at next statement element instead of i :)
Code:
Dim i&, lastindex&
Dim arr
arr= {12,,12,0,,12,0,,}
For i=Ubound(arr) to 1 step -1
    If arr(i-1)=12 then
           lastindex=i-1
           Exit for
    End if
Next i

The syntax for the array is incorrect. You are not checking the first or last element in the array and also you are also producing an incorrect result.
 
Upvote 0
Hi,
The quetion is how fo you get the array? Is it a variabe and changes and where you pick it from (a rage?) or is constant and hardcoded? Here's an example of picking the array from range and checking its last index that matches value of 12.

Code:
Dim i&, lastindex&
Dim arr()
arr= range("A1:A12")
For i=Ubound(arr) to 1 step -1
    If arr(i-1)=12 then
           lastindex=i-1
           Exit for
    End if
Next i
 
Upvote 0
@Mentor82
That code cannot work, also you are still not checking the last value in the array.
 
Upvote 0

Forum statistics

Threads
1,224,179
Messages
6,176,920
Members
452,752
Latest member
oliveiralexrui

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