Compile Error: Constant Expression Required

G

Guest

I receive the above error message when trying to execute the below code. The
For...Next loop increment variable, I, is highlighted. What is wrong with the
code. Is it a declaration error?
--
Software Cub.

The Code is:

Dim intNumberOfForkLiftTests As Integer
Dim intIncrementTopRange As Integer
Dim I As Integer
Dim strForkLiftPersonTested As String

If SpecialInstructions > 0 Then
SpecialInstructions = SpecialInstructions & Chr(13) & Chr(10) &
"Forklift Testing For:" & _
Chr(13) & Chr(10) & " "
Else
SpecialInstructions = "Forklift Testing For:" & Chr(13) & Chr(10) &
" "
End If

intNumberOfForkLiftTests = InputBox("Enter the Number of Fork Lift Tests
Administered.")

intIncrementTopRange = intNumberOfForkLiftTests

For I = 1 To intIncrementTopRange
Dim strName(I) As String
strForkLiftPersonTested = InputBox("Enter the Name of the Fork Lift
Person Tested.")
strName(I) = strForkLiftPersonTested
If I = 1 And I = intIncrementTopRange Then
SpecialInstructions = SpecialInstructions & strName(I) & "."
ElseIf I = 1 And (I + 1) = intIncrementTopRange Then
SpecialInstructions = SpecialInstructions & strName(I) & ", And "
ElseIf I = 1 And (I + 1) < intIncrementTopRange Then
SpecialInstructions = SpecialInstructions & strName(I) & ", "
ElseIf I > 1 Then
If I Mod 2 = 0 And I = intIncrementTopRange Then
SpecialInstructions = SpecialInstructions & strName(I) & "."
ElseIf I Mod 2 = 0 And (I + 1) = intIncrementTopRange Then
SpecialInstructions = SpecialInstructions & strName(I) & ",
And" & Chr(13) & Chr(10) & " "
ElseIf I Mod 2 = 0 And (I + 1) < intIncrementTopRange Then
SpecialInstructions = SpecialInstructions & strName(I) & ","
& Chr(13) & Chr(10) & " "
ElseIf I Mod 2 > O And I = intIncrementTopRange Then
SpecialInstructions = SpecialInstructions & strName(I) & "."
ElseIf I Mod 2 > O And (I + 1) = intIncrementTopRange Then
SpecialInstructions = SpecialInstructions & strName(I) & ",
And "
ElseIf I Mod 2 > O And (I + 1) < intIncrementTopRange Then
SpecialInstructions = SpecialInstructions & strName(I) & ", "
End If
End If
Next I

SpecialInstructions = SpecialInstructions & Chr(13) & Chr(10) &
"Dispensed on " & DateDispensed

Refresh
 
B

Brendan Reynolds

Perhaps your I (uppercase letter i) is really a 1 (digit one) or an l
(lowercase letter L) They can be very difficult to distinguish in some
fonts. For this reason, if you use single-letter variable names at all, it
is better to always use lowercase. Try changing your 'I' to 'i'.
 
G

Guest

Sorry Brenden,

That's not the problem.

Steve

Brendan Reynolds said:
Perhaps your I (uppercase letter i) is really a 1 (digit one) or an l
(lowercase letter L) They can be very difficult to distinguish in some
fonts. For this reason, if you use single-letter variable names at all, it
is better to always use lowercase. Try changing your 'I' to 'i'.
 
B

Brendan Reynolds

Well, in a way, it is.

First, though, there's another problem. It's the dynamic array. You need to
declare the array first, then redimension it with ReDim. In other words,
instead of ...

For I = 1 To intIncrementTopRange
Dim strName(I) As String

.... you need to do this ...

Dim strName() As String
For I = 1 To intIncrementTopRange
ReDim strName(I)

The 'Dim' line could, of course, be at the top of the procedure with your
other declarations, I've put it in here just to make it easy to compare it
with the original code.

So, what did I mean by 'in a way, it is'? Well, further down in the code,
you have this ...

ElseIf I Mod 2 = 0 And (I + 1) < intIncrementTopRange Then
SpecialInstructions = SpecialInstructions & strName(I) &
"," & Chr(13) & Chr(10) & " "
ElseIf I Mod 2 > O And I = intIncrementTopRange Then

I'm pretty sure that capital 'O' in the third line should be a zero! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top