This is an annoyingly simple concept...but so far I have had no luck implementing it.
I want to fill an array named arrType() with the values of the cells that make up Range("A40:A45").
Can someone point me in the right direction?
This is an annoyingly simple concept...but so far I have had no luck implementing it.
I want to fill an array named arrType() with the values of the cells that make up Range("A40:A45").
Can someone point me in the right direction?
-Randy Shea
I'm a programmer, but I'm also pro-grammar!
If your issue is resolved, please use Thread Tools to mark your thread as Solved!
PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003
You can set the range equal to a variant. Depending on the range, you may need to use WorkSheetFunction.Transpose.
Here is something that I did to fill an array with constants though looping is a good way too.
[vba]Option Explicit
Sub Test1dArray()
Dim a(1 To 10) As Variant, b As Variant
Dim r As Integer
b = InitArray(a, "Hello World!")
For r = 1 To 10
Debug.Print "b(" & r & ")", b(r)
Next r
End Sub
Sub Test2dArray()
Dim a(1 To 10, 1 To 5) As Variant, b As Variant
Dim r As Integer, c As Integer
b = InitArray(a, "Hello World!")
For r = 1 To 10
For c = 1 To 5
Debug.Print "b(" & r & "," & c & ")", b(r, c)
Next c
Next r
End Sub
Function InitArray(ByRef MyArray As Variant, val As Variant) As Variant
Dim w As Worksheet
Dim r As Range
Set w = ThisWorkbook.Worksheets.Add()
On Error Resume Next
Range("A1").Resize(UBound(MyArray, 1), 1) = val
Range("A1").Resize(UBound(MyArray, 1), UBound(MyArray, 2)) = val
Set r = w.UsedRange
If Err = 9 Then
InitArray = WorksheetFunction.Transpose(r)
Else: InitArray = r
End If
Set r = Nothing
Application.DisplayAlerts = False
w.Delete
Application.DisplayAlerts = True
Set w = Nothing
End Function[/vba]
[vba]
Dim arrType As Variant
arrType = Range("A40:A45")
[/vba]
or, if you want a single dimension array
[vba]
Dim arrType As Variant
arrType = Application.Transpose(Range("A40:A45"))
[/vba]
____________________________________________
Nihil simul inventum est et perfectum
Abusus non tollit usum
Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
James Thurber
I must be missing something, because this isn't working for me.
I'm unable to iterate through the array values. Here's what I've got so far:
[VBA]Sub ArrayTest()
Dim arrType() As Variant
Dim i As Integer
arrType = Application.Transpose(Range("A40:A45"))
i = 0
Do Until i > 5
Debug.Print arrType(i)
i = i + 1
Loop
End Sub[/VBA]
I keep getting Run-Time error 9 (subscript out of range)- The debugger highlights this line: 'Debug.Print arrType(i)'
-Randy Shea
I'm a programmer, but I'm also pro-grammar!
If your issue is resolved, please use Thread Tools to mark your thread as Solved!
PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003
[vba]
Sub ArrayTest()
Dim arrType() As Variant
Dim i As Integer
arrType = Application.Transpose(Range("A40:A45"))
i = LBound(arrType)
Do Until i > UBound(arrType)
Debug.Print arrType(i)
i = i + 1
Loop
End Sub
[/vba]
____________________________________________
Nihil simul inventum est et perfectum
Abusus non tollit usum
Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
James Thurber
That works perfectly... but I can't understand why. What is the difference between using the LBound and UBound functions, and me manually setting the value of 'i'?Originally Posted by xld
-Randy Shea
I'm a programmer, but I'm also pro-grammar!
If your issue is resolved, please use Thread Tools to mark your thread as Solved!
PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003
Because the lower bound was 1 not 0. It is ALWAYS safer to use the bounds than to make assumptions about what the bounds are.
I posted about this the other day on another thread, but someone responded with how to avoid it, thereby obliterating the impact of my comment.
____________________________________________
Nihil simul inventum est et perfectum
Abusus non tollit usum
Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
James Thurber
Okay, that makes sense- I'm just surprised because I'm not using 'Base 1', so I did assume that the array would start with 0.Because the lower bound was 1 not 0. It is ALWAYS safer to use the bounds than to make assumptions about what the bounds are.
Thanks for the explination!
-Randy Shea
I'm a programmer, but I'm also pro-grammar!
If your issue is resolved, please use Thread Tools to mark your thread as Solved!
PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003
Even when setting Option Base, some arrays will always start at 0, some will always start at 1. I repeat, it is not safe to make assumption, it is ALWAYS safer to use the bounds.
____________________________________________
Nihil simul inventum est et perfectum
Abusus non tollit usum
Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
James Thurber