gibger


Hi,

When I run the following code:

Code Snippet

Private Sub btnClickMe_Click()
Dim intrandom As Integer
Dim index
Dim person
Dim match As Boolean
Dim personx
Dim persony
Dim n
Dim birthday()
Dim counter
match = False
ReDim Preserve birthday(23)
intrandom = (Rnd * 364) + 1

Randomize

counter = 0


For n = 1 To 100
For persony = 1 To 23
birthday(person) = intrandom
Next

match = False

For personx = 1 To 22
For persony = personx + 1
If birthday(personx) = birthday(persony) Then
match = True
End If
Next
Next

If match = True Then

counter = counter + 1
End If


For index = 0 To UBound(birthday)
lblResults.Caption = lblResults.Caption & birthday(index) & vbCrLf

Next


Next

End Sub

I get compile error:syntax error

When I have in read is what the problem might be.

This is the old birthday problem.

Thanks.




Re: Compile rrror: syntax error

Cringing Dragon


I'm not quite sure what you're trying to achieve, but the red line contains a For without a To. That's why you get a syntax error.

Should the red line be:

For persony = 1 To personx + 1

Or is that For a typo - should it be

persony = personx + 1

and the following Next would have to be deleted as well.





Re: Compile rrror: syntax error

gibger

Here's the psudeocode for it.

Code Snippet

Begin birthday problem

Set counter to 0

For N = 1 to 100

For person = 1 to 23

Set birthday [person] to a random number between 1 and 365

End for

Set match to false

For person x = 1 to 22

For person y = person x + 1

If birthday [person x] = birthday [person y]

Set match to true

End If

End for

End for

If match is True add 1 to counter

End if

End For

Display counter + %

End

Thanks






Re: Compile rrror: syntax error

Cringing Dragon

It doesn't matter whether I understand the purpose of the code or not.

Your syntax error is cause by the line

For person y = person x + 1

because it has a FOR statement without a TO.

You haven't given it a counter to step through.






Re: Compile rrror: syntax error

Cringing Dragon

Assuming that you're using the Microsoft Visual Basic Editor, there are a number of features that can help you debug code.

One of them is that when you enter a line it doesn't think is valid VBA, it tells you so immediately.

  • View your code in the editor
  • Move your cursor to the end of the line that you'd previously correctly identified as being the cause
  • Hit enter (so that the editor thinks you've finished the line)
    • The editor will immediately pop up a message telling you
      Compile error:
      Expected: To

It also has a relatively useful help. If you move your cursor into the word For, and hit [F1], it will bring up help for the For statement, which will help you find the correct syntax.

Have fun.






Re: Compile rrror: syntax error

gibger

Thanks for your help. I will take a gander through everything and see what happens