VBA

Moderators: PavlinII
Number of threads: 1673
Number of posts: 3078

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Why wont it run? Posted by chiquites on 20 Jan 2005 at 9:57 PM
Can someone please, please tell me why this wont run..I've already done the first part of my program (numbers 1-60), now i'm in the second part(numbers 61-120) and it gives me an error message...Does anyone have an idea why?

Here is my code:

Private Type DataFile
Trial As Integer
Condition As String
Presentation As Integer
TrialType As String
End Type
Dim LagFile(120) As DataFile
Dim i, x, a, b, c, f As Integer
Dim TrialFilled(120) As Boolean
Dim IntCounter As Integer



Private Sub Command1_Click()
Randomize
StartAllOver:
For i = 1 To 60
LagFile(i).Trial = 0
LagFile(i).Condition = ""
LagFile(i).Presentation = 0
LagFile(i).TrialType = ""
TrialFilled(i) = False
Next i

IntCounter = 0


For i = 1 To 4
DoAgain:
'Generate random number from 1-41
x = Int(41 * Rnd + 1)
If TrialFilled(x) Or TrialFilled(x + 2) Or TrialFilled(x + 7) Or TrialFilled(x + 18) Then
IntCounter = IntCounter + 1
If IntCounter = 501 Then
Debug.Print "we're starting over"
GoTo StartAllOver:
End If
GoTo DoAgain:
Else
LagFile(x).Trial = x
LagFile(x).Condition = "1410"
LagFile(x).Presentation = 1
LagFile(x).TrialType = "Study"
LagFile(x + 2).Trial = x + 2
LagFile(x + 2).Condition = "1410"
LagFile(x + 2).Presentation = 2
LagFile(x + 2).TrialType = "Test"
LagFile(x + 7).Trial = x + 7
LagFile(x + 7).Condition = "1410"
LagFile(x + 7).Presentation = 3
LagFile(x + 7).TrialType = "Test"
LagFile(x + 18).Trial = x + 18
LagFile(x + 18).Condition = "1410"
LagFile(x + 18).Presentation = 4
LagFile(x + 18).TrialType = "Test"
TrialFilled(x) = True
TrialFilled(x + 2) = True
TrialFilled(x + 7) = True
TrialFilled(x + 18) = True
End If
Next i

For c = 1 To 4
DoAgain2:
'Generate random number from 1-41
x = Int(41 * Rnd + 1)
If TrialFilled(x) Or TrialFilled(x + 6) Or TrialFilled(x + 12) Or TrialFilled(x + 18) Then
IntCounter = IntCounter + 1
If IntCounter = 501 Then
Debug.Print "we're starting over"
GoTo StartAllOver:
End If
GoTo DoAgain2:
Else
LagFile(x).Trial = x
LagFile(x).Condition = "555"
LagFile(x).Presentation = 1
LagFile(x).TrialType = "Study"
LagFile(x + 6).Trial = x + 6
LagFile(x + 6).Condition = "555"
LagFile(x + 6).Presentation = 2
LagFile(x + 6).TrialType = "Test"
LagFile(x + 12).Trial = x + 12
LagFile(x + 12).Condition = "555"
LagFile(x + 12).Presentation = 3
LagFile(x + 12).TrialType = "Test"
LagFile(x + 18).Trial = x + 18
LagFile(x + 18).Condition = "555"
LagFile(x + 18).Presentation = 4
LagFile(x + 18).TrialType = "Test"
TrialFilled(x) = True
TrialFilled(x + 6) = True
TrialFilled(x + 12) = True
TrialFilled(x + 18) = True
End If
Next c
IntCounter = 0
For x = 1 To 57
If x = 57 And IntCounter < 3 Then GoTo StartAllOver
If TrialFilled(x) = False And TrialFilled(x + 1) = False And TrialFilled(x + 2) = False And TrialFilled(x + 3) = False Then
IntCounter = IntCounter + 1
If IntCounter > 4 Then Exit For
If x = 57 And IntCounter < 4 Then GoTo StartAllOver

LagFile(x).Trial = x
LagFile(x).Condition = "000"
LagFile(x).Presentation = 1
LagFile(x).TrialType = "Study"
LagFile(x + 1).Trial = x + 1
LagFile(x + 1).Condition = "000"
LagFile(x + 1).Presentation = 2
LagFile(x + 1).TrialType = "Test"
LagFile(x + 2).Trial = x + 2
LagFile(x + 2).Condition = "000"
LagFile(x + 2).Presentation = 3
LagFile(x + 2).TrialType = "Test"
LagFile(x + 3).Trial = x + 3
LagFile(x + 3).Condition = "000"
LagFile(x + 3).Presentation = 4
LagFile(x + 3).TrialType = "Test"
TrialFilled(x) = True
TrialFilled(x + 1) = True
TrialFilled(x + 2) = True
TrialFilled(x + 3) = True
End If
Next x


For f = 1 To 60
'Check if filled
If TrialFilled(f) = False Then
LagFile(f).Trial = f
LagFile(f).Condition = "filler"
LagFile(f).Presentation = 0
LagFile(f).TrialType = "filler"
TrialFilled(f) = True
End If
Next f


'Print it out please
Open "c:\MyFiles.dat" For Output As #1
For i = 1 To 60
Print #1, LagFile(i).Trial, LagFile(i).Condition, LagFile(i).Presentation, LagFile(i).TrialType
Next i
Close #1



'Let's do the second set of 60 trials
Randomize
StartAllOver2:
For i = 61To 120
LagFile(i).Trial = 0
LagFile(i).Condition = ""
LagFile(i).Presentation = 0
LagFile(i).TrialType = ""
TrialFilled(i) = False
Next i
IntCounter = 0

For i = 1 To 4
DoAgain3:
'Generate random number from 61-101
x = Int((101 - 61 + 61) * Rnd + 61)
If TrialFilled(x) Or TrialFilled(x + 2) Or TrialFilled(x + 7) Or TrialFilled(x + 18) Then
IntCounter = IntCounter + 1
If IntCounter = 501 Then
Debug.Print "we're starting over"
GoTo StartAllOver2:
End If
GoTo DoAgain3:
Else
LagFile(x).Trial = x
LagFile(x).Condition = "1410"
LagFile(x).Presentation = 1
LagFile(x).TrialType = "Study"
LagFile(x + 2).Trial = x + 2
LagFile(x + 2).Condition = "1410"
LagFile(x + 2).Presentation = 2
LagFile(x + 2).TrialType = "Test"
LagFile(x + 7).Trial = x + 7
LagFile(x + 7).Condition = "1410"
LagFile(x + 7).Presentation = 3
LagFile(x + 7).TrialType = "Test"
LagFile(x + 18).Trial = x + 18
LagFile(x + 18).Condition = "1410"
LagFile(x + 18).Presentation = 4
LagFile(x + 18).TrialType = "Test"
TrialFilled(x) = True
TrialFilled(x + 2) = True
TrialFilled(x + 7) = True
TrialFilled(x + 18) = True
End If
Next i

For c = 1 To 4
DoAgain4:
'Generate random number from 61-101
x = Int(101 * Rnd + 61)
If TrialFilled(x) = False And TrialFilled(x + 6) = False And TrialFilled(x + 12) = False Or TrialFilled(x + 18) = False Then
IntCounter = IntCounter + 1
If IntCounter = 501 Then
Debug.Print "we're starting over"
GoTo StartAllOver2:
End If
GoTo DoAgain4:
Else
LagFile(x).Trial = x
LagFile(x).Condition = "555"
LagFile(x).Presentation = 1
LagFile(x).TrialType = "Study"
LagFile(x + 6).Trial = x + 6
LagFile(x + 6).Condition = "555"
LagFile(x + 6).Presentation = 2
LagFile(x + 6).TrialType = "Test"
LagFile(x + 12).Trial = x + 12
LagFile(x + 12).Condition = "555"
LagFile(x + 12).Presentation = 3
LagFile(x + 12).TrialType = "Test"
LagFile(x + 18).Trial = x + 18
LagFile(x + 18).Condition = "555"
LagFile(x + 18).Presentation = 4
LagFile(x + 18).TrialType = "Test"
TrialFilled(x) = True
TrialFilled(x + 6) = True
TrialFilled(x + 12) = True
TrialFilled(x + 18) = True
End If
Next c
IntCounter = 0
For x = 61 To 117
If x = 117 And IntCounter < 3 Then GoTo StartAllOver2
If TrialFilled(x) = False And TrialFilled(x + 1) = False And TrialFilled(x + 2) = False And TrialFilled(x + 3) = False Then
IntCounter = IntCounter + 1
If IntCounter > 4 Then Exit For
If x = 117 And IntCounter < 4 Then GoTo StartAllOver2

LagFile(x).Trial = x
LagFile(x).Condition = "000"
LagFile(x).Presentation = 1
LagFile(x).TrialType = "Study"
LagFile(x + 1).Trial = x + 1
LagFile(x + 1).Condition = "000"
LagFile(x + 1).Presentation = 2
LagFile(x + 1).TrialType = "Test"
LagFile(x + 2).Trial = x + 2
LagFile(x + 2).Condition = "000"
LagFile(x + 2).Presentation = 3
LagFile(x + 2).TrialType = "Test"
LagFile(x + 3).Trial = x + 3
LagFile(x + 3).Condition = "000"
LagFile(x + 3).Presentation = 4
LagFile(x + 3).TrialType = "Test"
TrialFilled(x) = True
TrialFilled(x + 1) = True
TrialFilled(x + 2) = True
TrialFilled(x + 3) = True
End If
Next x


For f = 61 To 120
'Check if filled
If TrialFilled(f) = False Then
LagFile(f).Trial = f
LagFile(f).Condition = "filler"
LagFile(f).Presentation = 0
LagFile(f).TrialType = "filler"
TrialFilled(f) = True
End If
Next f










'Print it out please
Open "c:\Lags.dat" For Output As #1
For i = 1 To 120
Print #1, LagFile(i).Trial, LagFile(i).Condition, LagFile(i).Presentation, LagFile(i).TrialType
Next i
Close #1
End


End Sub
Report
Re: Why wont it run? Posted by markyc on 22 Jan 2005 at 12:34 PM
Hey,

The problem seems to occur when you generate your random number between 61 and 101. In two parts you have the following lines:
x = Int((101 - 61 + 61) * Rnd + 61)
x = Int(101 * Rnd + 61)
These both should be replaced with the following line:
x = Int((101 - 61) * Rnd + 61)

Ciao,
Mark


: Can someone please, please tell me why this wont run..I've already done the first part of my program (numbers 1-60), now i'm in the second part(numbers 61-120) and it gives me an error message...Does anyone have an idea why?
:
: Here is my code:
:
: Private Type DataFile
: Trial As Integer
: Condition As String
: Presentation As Integer
: TrialType As String
: End Type
: Dim LagFile(120) As DataFile
: Dim i, x, a, b, c, f As Integer
: Dim TrialFilled(120) As Boolean
: Dim IntCounter As Integer
:
:
:
: Private Sub Command1_Click()
: Randomize
: StartAllOver:
: For i = 1 To 60
: LagFile(i).Trial = 0
: LagFile(i).Condition = ""
: LagFile(i).Presentation = 0
: LagFile(i).TrialType = ""
: TrialFilled(i) = False
: Next i
:
: IntCounter = 0
:
:
: For i = 1 To 4
: DoAgain:
: 'Generate random number from 1-41
: x = Int(41 * Rnd + 1)
: If TrialFilled(x) Or TrialFilled(x + 2) Or TrialFilled(x + 7) Or TrialFilled(x + 18) Then
: IntCounter = IntCounter + 1
: If IntCounter = 501 Then
: Debug.Print "we're starting over"
: GoTo StartAllOver:
: End If
: GoTo DoAgain:
: Else
: LagFile(x).Trial = x
: LagFile(x).Condition = "1410"
: LagFile(x).Presentation = 1
: LagFile(x).TrialType = "Study"
: LagFile(x + 2).Trial = x + 2
: LagFile(x + 2).Condition = "1410"
: LagFile(x + 2).Presentation = 2
: LagFile(x + 2).TrialType = "Test"
: LagFile(x + 7).Trial = x + 7
: LagFile(x + 7).Condition = "1410"
: LagFile(x + 7).Presentation = 3
: LagFile(x + 7).TrialType = "Test"
: LagFile(x + 18).Trial = x + 18
: LagFile(x + 18).Condition = "1410"
: LagFile(x + 18).Presentation = 4
: LagFile(x + 18).TrialType = "Test"
: TrialFilled(x) = True
: TrialFilled(x + 2) = True
: TrialFilled(x + 7) = True
: TrialFilled(x + 18) = True
: End If
: Next i
:
: For c = 1 To 4
: DoAgain2:
: 'Generate random number from 1-41
: x = Int(41 * Rnd + 1)
: If TrialFilled(x) Or TrialFilled(x + 6) Or TrialFilled(x + 12) Or TrialFilled(x + 18) Then
: IntCounter = IntCounter + 1
: If IntCounter = 501 Then
: Debug.Print "we're starting over"
: GoTo StartAllOver:
: End If
: GoTo DoAgain2:
: Else
: LagFile(x).Trial = x
: LagFile(x).Condition = "555"
: LagFile(x).Presentation = 1
: LagFile(x).TrialType = "Study"
: LagFile(x + 6).Trial = x + 6
: LagFile(x + 6).Condition = "555"
: LagFile(x + 6).Presentation = 2
: LagFile(x + 6).TrialType = "Test"
: LagFile(x + 12).Trial = x + 12
: LagFile(x + 12).Condition = "555"
: LagFile(x + 12).Presentation = 3
: LagFile(x + 12).TrialType = "Test"
: LagFile(x + 18).Trial = x + 18
: LagFile(x + 18).Condition = "555"
: LagFile(x + 18).Presentation = 4
: LagFile(x + 18).TrialType = "Test"
: TrialFilled(x) = True
: TrialFilled(x + 6) = True
: TrialFilled(x + 12) = True
: TrialFilled(x + 18) = True
: End If
: Next c
: IntCounter = 0
: For x = 1 To 57
: If x = 57 And IntCounter < 3 Then GoTo StartAllOver
: If TrialFilled(x) = False And TrialFilled(x + 1) = False And TrialFilled(x + 2) = False And TrialFilled(x + 3) = False Then
: IntCounter = IntCounter + 1
: If IntCounter > 4 Then Exit For
: If x = 57 And IntCounter < 4 Then GoTo StartAllOver
:
: LagFile(x).Trial = x
: LagFile(x).Condition = "000"
: LagFile(x).Presentation = 1
: LagFile(x).TrialType = "Study"
: LagFile(x + 1).Trial = x + 1
: LagFile(x + 1).Condition = "000"
: LagFile(x + 1).Presentation = 2
: LagFile(x + 1).TrialType = "Test"
: LagFile(x + 2).Trial = x + 2
: LagFile(x + 2).Condition = "000"
: LagFile(x + 2).Presentation = 3
: LagFile(x + 2).TrialType = "Test"
: LagFile(x + 3).Trial = x + 3
: LagFile(x + 3).Condition = "000"
: LagFile(x + 3).Presentation = 4
: LagFile(x + 3).TrialType = "Test"
: TrialFilled(x) = True
: TrialFilled(x + 1) = True
: TrialFilled(x + 2) = True
: TrialFilled(x + 3) = True
: End If
: Next x
:
:
: For f = 1 To 60
: 'Check if filled
: If TrialFilled(f) = False Then
: LagFile(f).Trial = f
: LagFile(f).Condition = "filler"
: LagFile(f).Presentation = 0
: LagFile(f).TrialType = "filler"
: TrialFilled(f) = True
: End If
: Next f
:
:
: 'Print it out please
: Open "c:\MyFiles.dat" For Output As #1
: For i = 1 To 60
: Print #1, LagFile(i).Trial, LagFile(i).Condition, LagFile(i).Presentation, LagFile(i).TrialType
: Next i
: Close #1
:
:
:
: 'Let's do the second set of 60 trials
: Randomize
: StartAllOver2:
: For i = 61To 120
: LagFile(i).Trial = 0
: LagFile(i).Condition = ""
: LagFile(i).Presentation = 0
: LagFile(i).TrialType = ""
: TrialFilled(i) = False
: Next i
: IntCounter = 0
:
: For i = 1 To 4
: DoAgain3:
: 'Generate random number from 61-101
: x = Int((101 - 61 + 61) * Rnd + 61)
: If TrialFilled(x) Or TrialFilled(x + 2) Or TrialFilled(x + 7) Or TrialFilled(x + 18) Then
: IntCounter = IntCounter + 1
: If IntCounter = 501 Then
: Debug.Print "we're starting over"
: GoTo StartAllOver2:
: End If
: GoTo DoAgain3:
: Else
: LagFile(x).Trial = x
: LagFile(x).Condition = "1410"
: LagFile(x).Presentation = 1
: LagFile(x).TrialType = "Study"
: LagFile(x + 2).Trial = x + 2
: LagFile(x + 2).Condition = "1410"
: LagFile(x + 2).Presentation = 2
: LagFile(x + 2).TrialType = "Test"
: LagFile(x + 7).Trial = x + 7
: LagFile(x + 7).Condition = "1410"
: LagFile(x + 7).Presentation = 3
: LagFile(x + 7).TrialType = "Test"
: LagFile(x + 18).Trial = x + 18
: LagFile(x + 18).Condition = "1410"
: LagFile(x + 18).Presentation = 4
: LagFile(x + 18).TrialType = "Test"
: TrialFilled(x) = True
: TrialFilled(x + 2) = True
: TrialFilled(x + 7) = True
: TrialFilled(x + 18) = True
: End If
: Next i
:
: For c = 1 To 4
: DoAgain4:
: 'Generate random number from 61-101
: x = Int(101 * Rnd + 61)
: If TrialFilled(x) = False And TrialFilled(x + 6) = False And TrialFilled(x + 12) = False Or TrialFilled(x + 18) = False Then
: IntCounter = IntCounter + 1
: If IntCounter = 501 Then
: Debug.Print "we're starting over"
: GoTo StartAllOver2:
: End If
: GoTo DoAgain4:
: Else
: LagFile(x).Trial = x
: LagFile(x).Condition = "555"
: LagFile(x).Presentation = 1
: LagFile(x).TrialType = "Study"
: LagFile(x + 6).Trial = x + 6
: LagFile(x + 6).Condition = "555"
: LagFile(x + 6).Presentation = 2
: LagFile(x + 6).TrialType = "Test"
: LagFile(x + 12).Trial = x + 12
: LagFile(x + 12).Condition = "555"
: LagFile(x + 12).Presentation = 3
: LagFile(x + 12).TrialType = "Test"
: LagFile(x + 18).Trial = x + 18
: LagFile(x + 18).Condition = "555"
: LagFile(x + 18).Presentation = 4
: LagFile(x + 18).TrialType = "Test"
: TrialFilled(x) = True
: TrialFilled(x + 6) = True
: TrialFilled(x + 12) = True
: TrialFilled(x + 18) = True
: End If
: Next c
: IntCounter = 0
: For x = 61 To 117
: If x = 117 And IntCounter < 3 Then GoTo StartAllOver2
: If TrialFilled(x) = False And TrialFilled(x + 1) = False And TrialFilled(x + 2) = False And TrialFilled(x + 3) = False Then
: IntCounter = IntCounter + 1
: If IntCounter > 4 Then Exit For
: If x = 117 And IntCounter < 4 Then GoTo StartAllOver2
:
: LagFile(x).Trial = x
: LagFile(x).Condition = "000"
: LagFile(x).Presentation = 1
: LagFile(x).TrialType = "Study"
: LagFile(x + 1).Trial = x + 1
: LagFile(x + 1).Condition = "000"
: LagFile(x + 1).Presentation = 2
: LagFile(x + 1).TrialType = "Test"
: LagFile(x + 2).Trial = x + 2
: LagFile(x + 2).Condition = "000"
: LagFile(x + 2).Presentation = 3
: LagFile(x + 2).TrialType = "Test"
: LagFile(x + 3).Trial = x + 3
: LagFile(x + 3).Condition = "000"
: LagFile(x + 3).Presentation = 4
: LagFile(x + 3).TrialType = "Test"
: TrialFilled(x) = True
: TrialFilled(x + 1) = True
: TrialFilled(x + 2) = True
: TrialFilled(x + 3) = True
: End If
: Next x
:
:
: For f = 61 To 120
: 'Check if filled
: If TrialFilled(f) = False Then
: LagFile(f).Trial = f
: LagFile(f).Condition = "filler"
: LagFile(f).Presentation = 0
: LagFile(f).TrialType = "filler"
: TrialFilled(f) = True
: End If
: Next f
:
:
:
:
:
:
:
:
:
:
: 'Print it out please
: Open "c:\Lags.dat" For Output As #1
: For i = 1 To 120
: Print #1, LagFile(i).Trial, LagFile(i).Condition, LagFile(i).Presentation, LagFile(i).TrialType
: Next i
: Close #1
: End
:
:
: End Sub
:

markyc

I made a new friend today.
Real or imaginary?
Imaginary.





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.