UserForm hangs on error check

LwarenceD

New Member
Joined
Nov 24, 2005
Messages
29
Here's one thats got me bugged,

If the error check finds nothing in textbox12 of the userform1, it reports the error correctly.

Entering the info into textbox12 AFTER the check triggers, the code stops and the workbook hangs, the only way to clear is to close the UserForm and begin again.

If I remove the first Exit Sub the error check does not trap.

Any Suggestions?

Code:
Private Sub CommandButton1_Click()

   If Not IsNumeric([TextBox12].Value) Or [TextBox12].Value < 1 Then _
        MsgBox "Number of Lines not defined or less than zero.", , _
               "Try Again":
              'Range("TextBox12").Select
        Exit Sub


ActiveWorkbook.Sheets("Remarks").Activate

Range("A1:A11").ClearContents
Range("A1") = TextBox1.Text
Range("A2") = TextBox2.Text
Range("A3") = TextBox3.Text
Range("A4") = TextBox4.Text
Range("A5") = TextBox5.Text
Range("A6") = TextBox6.Text
Range("A7") = TextBox7.Text
Range("A8") = TextBox8.Text
Range("A9") = TextBox9.Text
Range("A10") = TextBox10.Text
Range("A11") = TextBox11.Text

Unload UserForm1

Sheets("Main Menu").Select

End Sub
 

Some videos you may like

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

acw

MrExcel MVP
Joined
Feb 13, 2004
Messages
4,814
Hi

Try changing it to

Code:
Private Sub CommandButton1_Click() 

   If Not IsNumeric([TextBox12].Value) Or [TextBox12].Value < 1 Then _ 
        MsgBox "Number of Lines not defined or less than zero.", , _ 
               "Try Again" 
              TextBox12.setfocus
        Exit Sub 
  end if


ActiveWorkbook.Sheets("Remarks").Activate 

Range("A1:A11").ClearContents 
Range("A1") = TextBox1.Text 
Range("A2") = TextBox2.Text 
Range("A3") = TextBox3.Text 
Range("A4") = TextBox4.Text 
Range("A5") = TextBox5.Text 
Range("A6") = TextBox6.Text 
Range("A7") = TextBox7.Text 
Range("A8") = TextBox8.Text 
Range("A9") = TextBox9.Text 
Range("A10") = TextBox10.Text 
Range("A11") = TextBox11.Text 

Unload UserForm1 

Sheets("Main Menu").Select 

End Sub


Tony
 

LwarenceD

New Member
Joined
Nov 24, 2005
Messages
29
Returns the Compile error

"End If without Block If"

It was one of the first thing I tried.

Only thing I can think of is the logic flow is out of whack. But I've stared at it for too long and cant see it =P

Thanks for the reply tho.. =)
 

XLGibbs

Well-known Member
Joined
Feb 25, 2005
Messages
2,446
You have your If statement followed by _ which continues the If statement in one line of code.

Code:
Private Sub CommandButton1_Click()

   If Not IsNumeric([TextBox12].Value) Or [TextBox12].Value < 1 Then 
        MsgBox "Number of Lines not defined or less than zero.", , _
               "Try Again"
              TextBox12.setfocus
        Exit Sub
  end if 
.....rest of code
 

LwarenceD

New Member
Joined
Nov 24, 2005
Messages
29
Thanks guys..

My eyes were WAY too close to the screen.. I missed the

TextBox12.setfocus

Its now working..


Thanks again

Cheers
 

Watch MrExcel Video

Forum statistics

Threads
1,118,448
Messages
5,572,166
Members
412,447
Latest member
immy
Top