Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default Error control?

Hi all, I have put below the code I use to look a number that has been
inputted into textbox1. The problem is when there is an illegal code put in
it comes up with the invalid code message box as expected but goes onto the
next textbox. I need it to highlight where the mistake was made.

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)

Dim ans

On Error Resume Next


ans = Application.Match(CLng(TextBox1.Text), Range("A:A"), 0)
If Not IsError(ans) Then
TextBox2.Text = Application.Index(Range("B:B"), ans)
TextBox3.Text = Application.Index(Range("C:C"), ans)
TextBox4.Text = Application.Index(Range("D:D"), ans)
TextBox5.Text = Application.Index(Range("E:E"), ans)

Else
MsgBox "Invalid code"
End If
On Error GoTo 0
End Sub

How do I do this?

Thanks in advance

Greg


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default Error control?

You can get to those other values (in column B, C, D, and E) via VBA techniques
and I added that check for isnumeric() just in case.

And I used the _exit event--"cancel = true" if there's an error:

Option Explicit
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub Textbox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

Dim ans As Variant

With Worksheets("sheet1")
If IsNumeric(Me.TextBox1) Then
ans = Application.Match(CLng(Me.TextBox1.Text), .Range("A:A"), 0)
Else
ans = Application.Match(Me.TextBox1.Text, .Range("a:a"), 0)
End If
If Not IsError(ans) Then
Me.TextBox2.Text = .Range("B:B")(ans)
Me.TextBox3.Text = .Range("C:C")(ans)
Me.TextBox4.Text = .Range("d:d")(ans)
Me.TextBox5.Text = .Range("e:e")(ans)
Me.Label1.Caption = ""
Else
Me.TextBox2.Text = ""
Me.TextBox3.Text = ""
Me.TextBox4.Text = ""
Me.TextBox5.Text = ""
Me.Label1.Caption = "Invalid Code"
Cancel = True
End If
End With
End Sub
Private Sub UserForm_Initialize()
Me.Label1.Caption = ""
End Sub

(I also added a label right above textbox1 to show any error messages. It makes
life a little simpler.)


Greg wrote:

Hi all, I have put below the code I use to look a number that has been
inputted into textbox1. The problem is when there is an illegal code put in
it comes up with the invalid code message box as expected but goes onto the
next textbox. I need it to highlight where the mistake was made.

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)

Dim ans

On Error Resume Next

ans = Application.Match(CLng(TextBox1.Text), Range("A:A"), 0)
If Not IsError(ans) Then
TextBox2.Text = Application.Index(Range("B:B"), ans)
TextBox3.Text = Application.Index(Range("C:C"), ans)
TextBox4.Text = Application.Index(Range("D:D"), ans)
TextBox5.Text = Application.Index(Range("E:E"), ans)

Else
MsgBox "Invalid code"
End If
On Error GoTo 0
End Sub

How do I do this?

Thanks in advance

Greg


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default Error control?

Thank you Dave

Greg


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Control Box Error ynissel Excel Discussion (Misc queries) 1 June 3rd 05 10:57 PM
Error Control no work :( CodeSponge[_2_] Excel Programming 5 January 12th 05 09:47 PM
Error 50290: Error writing to Worksheet while using an ActiveX Control emblair3 Excel Programming 3 February 24th 04 06:03 PM
error control Graeme[_3_] Excel Programming 1 February 3rd 04 10:17 PM
webbrowser control error No Name Excel Programming 0 November 18th 03 05:18 AM


All times are GMT +1. The time now is 12:57 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"