array problems, setting a limit

caselton

New Member
Joined
Apr 21, 2018
Messages
13
Hi all
Im trying to figure out how i can set the max value of an array from a value obtained from another source, like the number of rows in a range for example

Code:
Public Sub test()

'Const x = 12
Range("e12").Select
 nld = Range(Selection, Selection.End(xlUp)).Rows.Count
 y = nld
Const x = y


MsgBox nld

Dim myarr(1 To x) As Variant


For I = 1 To nld

myarr(I) = Cells(12 + I, 5).Value
Cells(I, 7).Value = myarr(I)
Next I


End Sub

This is for future projects when i will need to work in arrays ,and arrays that will be changing size depending on a given range

but it keeps telling me i must have a constant, and when i try to set it as a constant , its says no you cant do that :(

Any ideas how to get around this.
Ive worked in matlab a lot and im trying to see if i can switch to VBA
as always any help is much appreciated

Best,
J
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
You cannot assign a variable to a constant. Here you're trying to assign the variable y to the constant x. Since the number of rows will vary, you'll want to assign the number to a regular variable, not a constant, and declare it as Long. Also, it looks like you want the data below E12, not above it. If so, you should use xlDown instead of xlUp. In any case, it would be much more efficient to transfer the values to your array all in one go. Here's an example...

Code:
    Dim vData As Variant
    Dim i As Long
    
    vData = Range("E12:E" & Cells(Rows.Count, "E").End(xlUp).Row)
    
    For i = LBound(vData, 1) To UBound(vData, 1)
        Debug.Print vData(i, 1)
    Next i

Note that since the values are being transferred from a worksheet range the result is a two-dimensional array, not a one dimensional one.

Hope this helps!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,213
Members
452,618
Latest member
Tam84

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