Array variable declaration question

andydtaylor

Active Member
Joined
Feb 15, 2007
Messages
360
Office Version
  1. 2016
Hi,

Please could someone tell me why the below code to pick up array values from worksheet doesn't work if 'thisarray' is decaired as double? My values were (9,8,7;6,5,4;3,2,1)

I suspect I'm about to learn something fundamental!


Thanks,


Andy

Code:
Sub test3()
Dim thisarray As Variant

thisarray = Selection.Value

Stop

End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Selection.Value returns an array of type variant. You can therefore only assign it directly to a Variant or to a dynamic array of type variant. To assign the values to an array of type Double you would have to size the array and then loop through the cells assigning the values to the array.
HTH
 
Upvote 0
Hi Rory,

I made it this far:

Code:
Option Base 1

Sub test4()
 Dim length As String
 Dim width As String
 Dim input_array() As Double
 
    length = Selection.Rows.Count
    width = Selection.Columns.Count

ReDim input_array(length, width)

    Stop
End Sub

With regard to filling the array I found the below but this hard codes an assumtion about the columns filled. Can you suggest a flexible apporach I feel like I'm nearly there but can't quite get my head around it. Would the offset function be used here?

Code:
Dim arrData (1 to 7, 1 to 2)
Dim i As Integer
For i = 1 to 7
   ' Values from column A fill the first series of the
   ' array. If these values are strings, they become
   ' the labels for the rows.
   arrData(i, 1) = wkbObj.Worksheets(1) _
   .Range("A" & i + 1).Value

   ' Then values from column B fill the second.
   arrData(i, 2) = wkbObj.Worksheets(1) _
   .Range("B" & i + 1).Value
Next i

Thanks,


Andrew
 
Upvote 0
Sure - something like this:
Code:
Sub test4()
 Dim length As Long
 Dim width As Long
Dim x as long, y as long
 Dim input_array() As Double
 
    length = Selection.Rows.Count
    width = Selection.Columns.Count

ReDim input_array(1 to length, 1 to width)
for x = 1 to length
    for y = 1 to width
        input_array(x, y) = Selection.Cells(x, y).Value
    next y
next x
    Stop
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,567
Messages
6,114,344
Members
448,570
Latest member
rik81h

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