OS: Windows XP
Excel: 2003
Hi Guy's
I have the loo code which loops through 6 texts boxes to check that there is a value in each one, if a textbox has no value a message box is displayed advising which textbox has no value. When I originally wrote this I wanted the code to exit the loop and set the focus to the 1st textbox encountered that had no value.
This worked fine, or so I thought, when I run the code if all textboxes are empty the 1st message box appears advising textbox1 in incomplete, sets the focus to textbox1 then exits the sub.
If commandbutton1 is pressed again after a value has been entered into textbox1 and there are other textboxes with or without values it crashes excel completely closing it down. I am at a complete loss. I commented out the exit sub part of the code to check that the loop was working and sure enough I received a message box for every blank textbox. Below is my code, I am at a loss at to why it is not working.
any help would be great,
thanks in advance
G
Excel: 2003
Hi Guy's
I have the loo code which loops through 6 texts boxes to check that there is a value in each one, if a textbox has no value a message box is displayed advising which textbox has no value. When I originally wrote this I wanted the code to exit the loop and set the focus to the 1st textbox encountered that had no value.
This worked fine, or so I thought, when I run the code if all textboxes are empty the 1st message box appears advising textbox1 in incomplete, sets the focus to textbox1 then exits the sub.
If commandbutton1 is pressed again after a value has been entered into textbox1 and there are other textboxes with or without values it crashes excel completely closing it down. I am at a complete loss. I commented out the exit sub part of the code to check that the loop was working and sure enough I received a message box for every blank textbox. Below is my code, I am at a loss at to why it is not working.
Code:
Private Sub CommandButton1_Click()
Dim BNa As String 'File Name
Dim BNo As Integer ' Number(which will also be used to form part of the name)
Dim iName As Variant ' BNa and BNo combined to create file name
BNa = "TextBox"
BNo = 1
iName = BNa & BNo
'**Loop through textboxes 1 - 6***
Do While BNo < 7
iName = BNa & BNo
If Controls(iName).Value = "" Then
MsgBox iName & " Not completed"
Controls(iName).SetFocus
BNo = BNo + 1
'Exit Sub
End If
Loop
'**End of Loop*********************
End Sub
any help would be great,
thanks in advance
G
Last edited: