I am trying to use a variable to determine how many times to go thru a loop. The code that I am trying to get to work is shown below. If I use 'For S1 = 1 to 2", I go inside my loop 2 times. If I use 'For S1 = 1 to NumofTimes', I do not get inside my loop.
The Msgboxes show a value of 1 and a value of 2 when I cannot get into my loop. The msgbox display right after the evaluate function show a value of 2. I can uncomment the 'For s1 = 1 to 2' and comment the 'for s1 = 1 to numoftimes' and i get inside the loop.
If there is a different way to go thru a range of values in a column or something simple to change in my attempt I cannot figure it out. What should I do?
Sub testforloop()
'Define Variables
Dim s1 As Integer
Dim numoftimes As Integer
'Activate Schedule sheet and create variable values
Sheets("schedule").Activate
numoftimes = Evaluate("=SUM(IFERROR(1/COUNTIF(B:B,B:B),0))") - 1
MsgBox "NumofTimes = " & numoftimes
' Loop thru values using variable to calculate how many times to loop
For s1 = 1 To Num0fTimes
'For s1 = 1 To 2
MsgBox "Inside Loop s1 = " & s1
Next
MsgBox "out side of loop s1 = " & s1 & " num of times = " & numoftimes
End Sub
The Msgboxes show a value of 1 and a value of 2 when I cannot get into my loop. The msgbox display right after the evaluate function show a value of 2. I can uncomment the 'For s1 = 1 to 2' and comment the 'for s1 = 1 to numoftimes' and i get inside the loop.
If there is a different way to go thru a range of values in a column or something simple to change in my attempt I cannot figure it out. What should I do?
Sub testforloop()
'Define Variables
Dim s1 As Integer
Dim numoftimes As Integer
'Activate Schedule sheet and create variable values
Sheets("schedule").Activate
numoftimes = Evaluate("=SUM(IFERROR(1/COUNTIF(B:B,B:B),0))") - 1
MsgBox "NumofTimes = " & numoftimes
' Loop thru values using variable to calculate how many times to loop
For s1 = 1 To Num0fTimes
'For s1 = 1 To 2
MsgBox "Inside Loop s1 = " & s1
Next
MsgBox "out side of loop s1 = " & s1 & " num of times = " & numoftimes
End Sub