Compile error: Constant expression required

luolovepi

Board Regular
Joined
Jun 9, 2011
Messages
116
I have two predefined integer variables "tableContentRows" and "NoOfRowsInsert". And they both have been assigned a value. But strangely, I got a compile error when I tried to run the code. How come?

Code:
Dim tempClick(1 To (tableContentRows + NoOfRowInsert)) As Integer


Yours Sincerely,
lolo^^
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Can you explain what you think that line of code should do, because syntactically it makes no sense to me... you seem to be combining a variable declaration with some kind of for-next loop??
 
Upvote 0
DIM statements aren't executed when the program runs - they're directives telling the compiler to allocate space to the variable or, in this case, the array, when your code is compiled.

Since those variables don't have a value at compile time, the compiler cannot use them to allocate the space your array requires.

You would have to declare the array as Dim tempClick() As Integer which tells the compiler you're going to provide the size of the array during execution, and then size it like this:-
Code:
ReDim tempClick(1 To (tableContentRows + NoOfRowInsert))
 
Upvote 0
Got it! Thank you Ruddles!
DIM statements aren't executed when the program runs - they're directives telling the compiler to allocate space to the variable or, in this case, the array, when your code is compiled.

Since those variables don't have a value at compile time, the compiler cannot use them to allocate the space your array requires.

You would have to declare the array as Dim tempClick() As Integer which tells the compiler you're going to provide the size of the array during execution, and then size it like this:-
Code:
ReDim tempClick(1 To (tableContentRows + NoOfRowInsert))
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,730
Members
452,939
Latest member
WCrawford

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