for loop not working

J

Joanne

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)
 
J

Jay Freedman

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
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

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