0

I got in trouble in a part of my code. I have an variable that changes by the the number of filled rows.I have defined an array from 0 to that number but I receive "Constant Expression Required" Error. I would be thankful if anyone could help me.

 Sub test()
   Lastrow = Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row
   Dim f(0 To Lastrow) As Double
 End Sub

3 Answers 3

1

You can't dim an array with variable, just constant

You need dim the array before, and redim it to your variable. With redim:

Sub test()
   Dim f() As Double
   Lastrow = Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row
   Redim f(0 To Lastrow)
End Sub

With high constant (a bad way)

 Sub test()
       Dim f(35000) As Double
       Lastrow = Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row
    End Sub
Sign up to request clarification or add additional context in comments.

Comments

0

I could be wrong but I don't think you can dim using a variable. You might be able to redim..?

1 Comment

Would you please tell me how I can do that?
0

If you use

sn=cells(1).currentregion

You don't need a 'last row' variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.