Hello, I have the following subroutine that breaks right after it reaches the
last value of "i" which is 102. I've added watches for every variable in the
routine and all the variables are there. The actual For Loop is working
fine, and the bookmark exists but when I put in a message box, it won't even
go to the message box. I have an identical For Loop higher in the code that
works fine. I'm totally flummoxed by this. Thank you for any help you can
provide.
_________________________________
Public Sub AddBarAdmissionsListInfo(varBookmark As String, varRecords As Long)
Dim i As Integer
Dim strListInformation As String
Dim rng As Range

Set rng = ActiveDocument.Bookmarks(varBookmark).Range
For i = 0 To varRecords
If frmAttyBio.ListBarAdmissions.Selected(i) = True Then
strListInformation = _
strListInformation & frmAttyBio.ListBarAdmissions.list(i)
strListInformation = strListInformation & " | "
End If
Next i
MsgBox "Out of ForLoop"
rng.Text = Left(strListInformation, Len(strListInformation) - 2)

Re: for loop not working by Jay

Jay
Thu Jun 01 20:47:02 CDT 2006

I'm not sure where you're getting the value of varRecords that your
passing in as a parameter, but if it's the number if items in the
ListBarAdmissions box, then you need to use

For i = 0 To varRecords - 1

That is, if there are 102 items, then the first one is numbered 0 and
the last one is numbered 101. There is no item numbered 102. You'll
get an error when you try to touch ListBarAdmissions.Selected(102).

This "off by one" error is one of the most common mistakes in all
kinds of programming.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

On Thu, 1 Jun 2006 09:47:02 -0700, Joanne
<Joanne@discussions.microsoft.com> wrote:

>Hello, I have the following subroutine that breaks right after it reaches the
>last value of "i" which is 102. I've added watches for every variable in the
>routine and all the variables are there. The actual For Loop is working
>fine, and the bookmark exists but when I put in a message box, it won't even
>go to the message box. I have an identical For Loop higher in the code that
>works fine. I'm totally flummoxed by this. Thank you for any help you can
>provide.
>_________________________________
>Public Sub AddBarAdmissionsListInfo(varBookmark As String, varRecords As Long)
>Dim i As Integer
>Dim strListInformation As String
>Dim rng As Range
>
> Set rng = ActiveDocument.Bookmarks(varBookmark).Range
> For i = 0 To varRecords
> If frmAttyBio.ListBarAdmissions.Selected(i) = True Then
> strListInformation = _
> strListInformation & frmAttyBio.ListBarAdmissions.list(i)
> strListInformation = strListInformation & " | "
> End If
> Next i
>MsgBox "Out of ForLoop"
> rng.Text = Left(strListInformation, Len(strListInformation) - 2)
>