Converting a range into an Array

geordieboyx

New Member
Joined
Jul 7, 2007
Messages
15
I am trying to write a vba program which will read data from my spreadsheet and then write it to another application

I have a list of cells refering to ranges in different areas of my spreadsheet. The cells containing the ranges are formatted as text and take the form of =A1:B12

All ranges are in the same format with the first three rows being merged and the rest containing letters and their values example A4 = C, B4 = 20 and so on.

I need to put these values into a two dimentional array in VBA. The ranges are different in length.

I suspect the code might be simple but I can't find any refference to get me started with this

Geordieboy
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

GlennUK

Well-known Member
Joined
Jul 8, 2002
Messages
11,723
How are you expecting the three merged rows to be stored ( or written to another application )?
 
Upvote 0

geordieboyx

New Member
Joined
Jul 7, 2007
Messages
15
When reffering to the merged cell within the spreadsheet it has the ref of the first column so data stored in merged cells A1B1 is refered to as A1 so I thoght in an array it might be refered to as for example (1,1). In my forth row which begin unmerged cells would be refered to as (4,1) and (4,2) repectfully. Would I be on the right lines in assuming this?
 
Upvote 0

Cindy Ellis

MrExcel MVP
Joined
Jun 9, 2006
Messages
1,802
Glenn,
I was trying to do essentially the same thing today...when I used the approach you proposed I got the error "Cannot assign to array". The help text said that each element had to be assigned individually, but I also seem to remember being able to assign the values in a range to the elements of an array without doing it individually. Is there something I missed?
Thanks,
Cindy
 
Upvote 0

GlennUK

Well-known Member
Joined
Jul 8, 2002
Messages
11,723
Hi Cindy,

can you show all of your code please? It may be that you are doing something that's preventing the assignment.
 
Upvote 0

Rasm

Well-known Member
Joined
Feb 9, 2011
Messages
500
It puts it in aa array like shown below - you can see it all under

view -Local watch point

I wrote it out so it is very clear- but insert a breakpoint and the view Local variables.

Code:
Public Sub ArrayFromRange()
    Dim MyArray()
    MyArray = ActiveSheet.Range("B6:B12").Value
    
 
    MyArray(1, 1) = MyArray(1, 1) 'Remove
    MyArray(2, 1) = MyArray(2, 1) 'remove
    Stop'remove

End Sub
 
Upvote 0

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Glenn,
I was trying to do essentially the same thing today...when I used the approach you proposed I got the error "Cannot assign to array". The help text said that each element had to be assigned individually, but I also seem to remember being able to assign the values in a range to the elements of an array without doing it individually. Is there something I missed?
Thanks,
Cindy

The trick is not to dimension the array in advance. A simple example

Code:
Sub ArryTest()
Dim Myarray
Dim i As Long, j As Long
Myarray = Range("A1:B10")
For i = 1 To 10
    For j = 1 To 2
        Cells(i, j).Offset(, 2).Value = Myarray(i, j)
    Next j
Next i
End Sub
 
Upvote 0

Cindy Ellis

MrExcel MVP
Joined
Jun 9, 2006
Messages
1,802
Thanks everyone. Not dimensioning the array ahead of time solved my problem. (Not sure about the one from the original poster though.)
 
Upvote 0

Forum statistics

Threads
1,190,801
Messages
5,982,992
Members
439,811
Latest member
DB38

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
Top