Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default creating an array from range help

i am wondering why this doesn't work:
Sub test()
Dim arr As Variant
Dim i As Long
arr = Range("h2:h11").Value
For i = LBound(arr) To UBound(arr)
Debug.Print arr(i) ' error here
Next
End Sub


but this does:

Sub test()
Dim arr As Variant
Dim i As Long
arr = array("a","b","c")
For i = LBound(arr) To UBound(arr)
Debug.Print arr(i)
Next
End Sub
--

how can i access each element when i create the array from a range?

Gary



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,073
Default creating an array from range help

Gary Keramidas wrote:
i am wondering why this doesn't work:
Sub test()
Dim arr As Variant
Dim i As Long
arr = Range("h2:h11").Value
For i = LBound(arr) To UBound(arr)
Debug.Print arr(i) ' error here
Next
End Sub


but this does:

Sub test()
Dim arr As Variant
Dim i As Long
arr = array("a","b","c")
For i = LBound(arr) To UBound(arr)
Debug.Print arr(i)
Next
End Sub
--

how can i access each element when i create the array from a range?

Gary


Hi Gary,

Try Debug.Print arr(i,1) in the first Sub. That type of array is always
2 dim in keeping with the Sheets 2 dim nature of rows and columns.

Ken Johnson

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,073
Default creating an array from range help

Hi Gary,

and if that doesn't work (I always get confused with these arrays) then
try...

Debug.Print arr(1,i)

Ken Johnson

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default creating an array from range help

this one worked, thanks ken

Debug.Print arr(i, 1)

--


Gary


"Ken Johnson" wrote in message
ups.com...
Hi Gary,

and if that doesn't work (I always get confused with these arrays) then
try...

Debug.Print arr(1,i)

Ken Johnson



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default creating an array from range help

in the first case, the array is always two dimensional:

Sub test()
Dim arr As Variant
Dim i As Long
arr = Range("h2:h11").Value
For i = LBound(arr,1) To UBound(arr,1)
Debug.Print arr(i,1) '<== fix that works
Next
End Sub

The change to Lbound(Arr,1) is for clarity. It would default to that
without the change.

--
Regards,
Tom Ogilvy


"Gary Keramidas" <GKeramidasATmsn.com wrote in message
...
i am wondering why this doesn't work:
Sub test()
Dim arr As Variant
Dim i As Long
arr = Range("h2:h11").Value
For i = LBound(arr) To UBound(arr)
Debug.Print arr(i) ' error here
Next
End Sub


but this does:

Sub test()
Dim arr As Variant
Dim i As Long
arr = array("a","b","c")
For i = LBound(arr) To UBound(arr)
Debug.Print arr(i)
Next
End Sub
--

how can i access each element when i create the array from a range?

Gary







  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default creating an array from range help

thanks tom

--


Gary


"Tom Ogilvy" wrote in message
...
in the first case, the array is always two dimensional:

Sub test()
Dim arr As Variant
Dim i As Long
arr = Range("h2:h11").Value
For i = LBound(arr,1) To UBound(arr,1)
Debug.Print arr(i,1) '<== fix that works
Next
End Sub

The change to Lbound(Arr,1) is for clarity. It would default to that without
the change.

--
Regards,
Tom Ogilvy


"Gary Keramidas" <GKeramidasATmsn.com wrote in message
...
i am wondering why this doesn't work:
Sub test()
Dim arr As Variant
Dim i As Long
arr = Range("h2:h11").Value
For i = LBound(arr) To UBound(arr)
Debug.Print arr(i) ' error here
Next
End Sub


but this does:

Sub test()
Dim arr As Variant
Dim i As Long
arr = array("a","b","c")
For i = LBound(arr) To UBound(arr)
Debug.Print arr(i)
Next
End Sub
--

how can i access each element when i create the array from a range?

Gary







Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
creating an array Richard New Users to Excel 4 March 15th 07 01:06 PM
creating an array Richard Excel Worksheet Functions 4 March 15th 07 01:06 PM
creating an array on the fly Gary Keramidas Excel Programming 4 August 9th 06 09:34 PM
Creating an Array from a Range using Offset Frank & Pam Hayes[_2_] Excel Programming 3 September 10th 04 01:22 AM
Creating an array Eric[_6_] Excel Programming 1 January 12th 04 08:25 PM


All times are GMT +1. The time now is 12:36 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"