Excel VBA 取得陣列的個數

使用  UBound( ) 函數可以得知陣列的個數,如下:

'宣告陣列
Dim ay_temp01() As Variant
    ay_tem01 = Array("JOHN", "MAY", "LEE")

'取得陣列個數,因為陣列由0開始,所以數量需要加1
Dim temp01 as Integer
temp01=UBound(ay_temp01) + 1

'可以配合LBound( )函數來組成廻圈,取得出陣列每一個元素
Dim a,b as Integer
Dim show_text as String
'陣列重0開始編號,所以不用加 1 
For a LBound(ay_temp01) To UBound(ay_temp01)
    b=msgbox(ay_temp01(a),vbOKonly)
next a