PDA

View Full Version : Solved: Script out of Range on Variant Array



nb-
06-07-2008, 10:42 AM
The quick bit of code below give an example of the problem I am having with. When the code comes to the debug.print lines at the bottom, I get the error "Script out of range". Can anyone tell me what the cause is?

Also as a side point why does this output the first element of the array 10 times, rather than each of the examples.


Option Explicit

Sub test()

Dim arr_var_array() As Variant
Dim int_matrix_dim%, i As Integer

ReDim arr_var_array(0 To 9)
arr_var_array(0) = "test"
arr_var_array(1) = 59
arr_var_array(2) = "A_random"
arr_var_array(3) = "______________"
arr_var_array(4) = 0.000000001
arr_var_array(5) = 3.27000001
arr_var_array(6) = -3.27000001
arr_var_array(7) = "'''''''''''''''___AAAAA3"
arr_var_array(8) = "12ADC"
arr_var_array(9) = "a_random"

Debug.Print LBound(arr_var_array, 1)
Debug.Print UBound(arr_var_array, 1)
Debug.Print arr_var_array(5)
'// Determin how many dimenssions array has.
On Error GoTo NoMoreDims
int_matrix_dim = 1

Do
i = LBound(arr_var_array, int_matrix_dim)
int_matrix_dim = int_matrix_dim + 1
Loop

NoMoreDims:
int_matrix_dim = int_matrix_dim - 1
On Error GoTo 0
'// End

If int_matrix_dim > 1 Then
int_matrix_dim = UBound(arr_var_array, 2)
End If

'// Sort Data Using Excel Sort.
Sheets.Add

ActiveSheet.Range(Cells(1, 1), Cells(UBound(arr_var_array, 1) - LBound(arr_var_array, 1) + 1, int_matrix_dim)).Select

Selection = arr_var_array

ActiveSheet.Range(Cells(1, 1), Cells(UBound(arr_var_array, 1) + 1, int_matrix_dim)).Select

Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlNo, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom

arr_var_array = Selection.Value 'Range(Selection.Address) '
ActiveWindow.SelectedSheets.Delete

For i = LBound(arr_var_array, 1) To UBound(arr_var_array, 1)
Debug.Print arr_var_array(i)
Next i


End Sub

Norie
06-07-2008, 11:14 AM
Eh, could it possibly be because the array has only 1 dimension?:eek:

And you seem to be referring to a non-existent 2nd dimension with 1 here.

Debug.Print LBound(arr_var_array, 1)

Bob Phillips
06-07-2008, 11:14 AM
Because it is a 2D array when you load from a range, not 1D, even if it is just one column.

nb-
06-08-2008, 04:25 AM
Thanks.

Aussiebear
06-08-2008, 06:26 AM
Hi nb-, If this issue has been resolved to your satisfaction, please mark the thread "Solved" by using the Thread Tools just above your initial post.