Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 468
Default Help with code

When I click my cb1 (below) my cursor jumps inside tb1 - FINE OK

Private Sub CommandButton1_Click()
Worksheets("Sheet1").TextBox1.Activate
End Sub

But If I type Mary -- nothing happens << but should get message


Private Sub TextBox1_Change()
If Not Left(Worksheets("Sheet1").TextBox1.Text, 1) = "A" Or
Left(Worksheets("Sheet1").TextBox1.Text, 1) = "B" Then
MsgBox "Text Input must begin with either an ""A"" or ""B"", Try Again"
End If
TextBox1.Text = ""
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 468
Default Help with code

**CORRECTION**
As I type the first character into the textbox the message pops up,
when I click OK, it pops up again. Grrrrrrrr

I'm missing understanding something BIG here.

Help !!


"JMay" wrote:

When I click my cb1 (below) my cursor jumps inside tb1 - FINE OK

Private Sub CommandButton1_Click()
Worksheets("Sheet1").TextBox1.Activate
End Sub

But If I type Mary -- nothing happens << but should get message


Private Sub TextBox1_Change()
If Not Left(Worksheets("Sheet1").TextBox1.Text, 1) = "A" Or
Left(Worksheets("Sheet1").TextBox1.Text, 1) = "B" Then
MsgBox "Text Input must begin with either an ""A"" or ""B"", Try Again"
End If
TextBox1.Text = ""
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Help with code


Its because you are changing the textbox with every click, i haven't
tried this but:

Private Sub TextBox1_Change()
If Not Left(Worksheets("Sheet1").TextBox1.Text, 1) = "A" Or
Left(Worksheets("Sheet1").TextBox1.Text, 1) = "B" Then
TextBox1.Text = ""
MsgBox "Text Input must begin with either an ""A"" or ""B"", Try Again"
End If
End Sub


--
Simon Lloyd

Regards,
Simon Lloyd
'The Code Cage' (http://www.thecodecage.com)
------------------------------------------------------------------------
Simon Lloyd's Profile: http://www.thecodecage.com/forumz/member.php?userid=1
View this thread: http://www.thecodecage.com/forumz/sh...ad.php?t=37490

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 772
Default Help with code

It works fine for me. Where are you posting the code? The only thing i would
do differently is put the if statement on two lines to make it easier to trap
for other issues. Below i fix it so a is acceptable not just A and also it
doesn't create the error everytime you blank it out.

Private Sub CommandButton1_Click()
Worksheets("Sheet1").TextBox1.Activate

End Sub
Private Sub TextBox1_Change()
If Len(Worksheets("Sheet1").TextBox1.Text) And
UCase(Left(Worksheets("Sheet1").TextBox1.Text, 1)) < "A" Then
If Len(Worksheets("Sheet1").TextBox1.Text) And
UCase(Left(Worksheets("Sheet1").TextBox1.Text, 1)) < "B" Then
TextBox1.Text = ""
MsgBox "Text Input must begin with either an ""A"" or ""B"", Try Again"
End If
End If
End Sub

--
-John
Please rate when your question is answered to help us and others know what
is helpful.


"JMay" wrote:

When I click my cb1 (below) my cursor jumps inside tb1 - FINE OK

Private Sub CommandButton1_Click()
Worksheets("Sheet1").TextBox1.Activate
End Sub

But If I type Mary -- nothing happens << but should get message


Private Sub TextBox1_Change()
If Not Left(Worksheets("Sheet1").TextBox1.Text, 1) = "A" Or
Left(Worksheets("Sheet1").TextBox1.Text, 1) = "B" Then
MsgBox "Text Input must begin with either an ""A"" or ""B"", Try Again"
End If
TextBox1.Text = ""
End Sub

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 772
Default Help with code

i dont see my answer here so i will post again. You need to make a few
changes that i think will help. It stops the double msgbox issue and lets you
use caps or no caps

Private Sub CommandButton1_Click()
Worksheets("Sheet1").TextBox1.Activate

End Sub
Private Sub TextBox1_Change()
If Len(Worksheets("Sheet1").TextBox1.Text) And
UCase(Left(Worksheets("Sheet1").TextBox1.Text, 1)) < "A" Then
If Len(Worksheets("Sheet1").TextBox1.Text) And
UCase(Left(Worksheets("Sheet1").TextBox1.Text, 1)) < "B" Then
TextBox1.Text = ""
MsgBox "Text Input must begin with either an ""A"" or ""B"", Try Again"
End If
End If
End Sub

--
-John
Please rate when your question is answered to help us and others know what
is helpful.


"JMay" wrote:

When I click my cb1 (below) my cursor jumps inside tb1 - FINE OK

Private Sub CommandButton1_Click()
Worksheets("Sheet1").TextBox1.Activate
End Sub

But If I type Mary -- nothing happens << but should get message


Private Sub TextBox1_Change()
If Not Left(Worksheets("Sheet1").TextBox1.Text, 1) = "A" Or
Left(Worksheets("Sheet1").TextBox1.Text, 1) = "B" Then
MsgBox "Text Input must begin with either an ""A"" or ""B"", Try Again"
End If
TextBox1.Text = ""
End Sub



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Help with code

Hi,

It works partly for me but becaue you are clearing the textbox the change
event will fire twice. Once for incorrect input and once when you clear it.
I'd do it like this

Private Sub TextBox1_Change()
If Left(Worksheets("Sheet1").TextBox1.Text, 1) < "A" And _
Left(Worksheets("Sheet1").TextBox1.Text, 1) < "B" _
And TextBox1.Text < "" Then
TextBox1.Text = ""
MsgBox "Text Input must begin with either an ""A"" or ""B"", Try Again"
End If
End Sub

Mike

"JMay" wrote:

When I click my cb1 (below) my cursor jumps inside tb1 - FINE OK

Private Sub CommandButton1_Click()
Worksheets("Sheet1").TextBox1.Activate
End Sub

But If I type Mary -- nothing happens << but should get message


Private Sub TextBox1_Change()
If Not Left(Worksheets("Sheet1").TextBox1.Text, 1) = "A" Or
Left(Worksheets("Sheet1").TextBox1.Text, 1) = "B" Then
MsgBox "Text Input must begin with either an ""A"" or ""B"", Try Again"
End If
TextBox1.Text = ""
End Sub

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 468
Default Help with code

John I copied your code in exactly, but still have the same problem.

What is condition of the Len() statement(s)? I don't see anything..

Thanks!

"John Bundy" wrote:

i dont see my answer here so i will post again. You need to make a few
changes that i think will help. It stops the double msgbox issue and lets you
use caps or no caps

Private Sub CommandButton1_Click()
Worksheets("Sheet1").TextBox1.Activate

End Sub
Private Sub TextBox1_Change()
If Len(Worksheets("Sheet1").TextBox1.Text) And
UCase(Left(Worksheets("Sheet1").TextBox1.Text, 1)) < "A" Then
If Len(Worksheets("Sheet1").TextBox1.Text) And
UCase(Left(Worksheets("Sheet1").TextBox1.Text, 1)) < "B" Then
TextBox1.Text = ""
MsgBox "Text Input must begin with either an ""A"" or ""B"", Try Again"
End If
End If
End Sub

--
-John
Please rate when your question is answered to help us and others know what
is helpful.


"JMay" wrote:

When I click my cb1 (below) my cursor jumps inside tb1 - FINE OK

Private Sub CommandButton1_Click()
Worksheets("Sheet1").TextBox1.Activate
End Sub

But If I type Mary -- nothing happens << but should get message


Private Sub TextBox1_Change()
If Not Left(Worksheets("Sheet1").TextBox1.Text, 1) = "A" Or
Left(Worksheets("Sheet1").TextBox1.Text, 1) = "B" Then
MsgBox "Text Input must begin with either an ""A"" or ""B"", Try Again"
End If
TextBox1.Text = ""
End Sub

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 772
Default Help with code

If this stuff isn't working i am thinking you have the code in the
ThisWorkbook module, the buttonclick has to be in the sheet module that you
have the button on. Turn on design mode, double click the button and see if
it takes you to the code. The len i accidentally left off =1 you just want to
provide the error if there is 1 letter, not when its blank.
--
-John
Please rate when your question is answered to help us and others know what
is helpful.


"JMay" wrote:

John I copied your code in exactly, but still have the same problem.

What is condition of the Len() statement(s)? I don't see anything..

Thanks!

"John Bundy" wrote:

i dont see my answer here so i will post again. You need to make a few
changes that i think will help. It stops the double msgbox issue and lets you
use caps or no caps

Private Sub CommandButton1_Click()
Worksheets("Sheet1").TextBox1.Activate

End Sub
Private Sub TextBox1_Change()
If Len(Worksheets("Sheet1").TextBox1.Text) And
UCase(Left(Worksheets("Sheet1").TextBox1.Text, 1)) < "A" Then
If Len(Worksheets("Sheet1").TextBox1.Text) And
UCase(Left(Worksheets("Sheet1").TextBox1.Text, 1)) < "B" Then
TextBox1.Text = ""
MsgBox "Text Input must begin with either an ""A"" or ""B"", Try Again"
End If
End If
End Sub

--
-John
Please rate when your question is answered to help us and others know what
is helpful.


"JMay" wrote:

When I click my cb1 (below) my cursor jumps inside tb1 - FINE OK

Private Sub CommandButton1_Click()
Worksheets("Sheet1").TextBox1.Activate
End Sub

But If I type Mary -- nothing happens << but should get message


Private Sub TextBox1_Change()
If Not Left(Worksheets("Sheet1").TextBox1.Text, 1) = "A" Or
Left(Worksheets("Sheet1").TextBox1.Text, 1) = "B" Then
MsgBox "Text Input must begin with either an ""A"" or ""B"", Try Again"
End If
TextBox1.Text = ""
End Sub

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default Help with code

I think Mike is on to it. I got pretty good results with the code below. It
only fires twice if something other that A or B is entered. Note that the Or
was changed to And.

Private Sub TextBox1_Change()
If Not Left(UCase(Worksheets("Sheet1").TextBox1.Text), 1) = "A" And Not
Left(UCase(Worksheets("Sheet1").TextBox1.Text), 1) = "B" Then
TextBox1.Text = ""
MsgBox "Text Input must begin with either an ""A"" or ""B"", Try Again"
End If
End Sub





"Mike H" wrote:

Hi,

It works partly for me but becaue you are clearing the textbox the change
event will fire twice. Once for incorrect input and once when you clear it.
I'd do it like this

Private Sub TextBox1_Change()
If Left(Worksheets("Sheet1").TextBox1.Text, 1) < "A" And _
Left(Worksheets("Sheet1").TextBox1.Text, 1) < "B" _
And TextBox1.Text < "" Then
TextBox1.Text = ""
MsgBox "Text Input must begin with either an ""A"" or ""B"", Try Again"
End If
End Sub

Mike

"JMay" wrote:

When I click my cb1 (below) my cursor jumps inside tb1 - FINE OK

Private Sub CommandButton1_Click()
Worksheets("Sheet1").TextBox1.Activate
End Sub

But If I type Mary -- nothing happens << but should get message


Private Sub TextBox1_Change()
If Not Left(Worksheets("Sheet1").TextBox1.Text, 1) = "A" Or
Left(Worksheets("Sheet1").TextBox1.Text, 1) = "B" Then
MsgBox "Text Input must begin with either an ""A"" or ""B"", Try Again"
End If
TextBox1.Text = ""
End Sub

  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Help with code

I have not looked into the functionality problem yet, so this message does
not address that aspect of the thread. All I am doing here is offering a
simplification for the If-Then statements being used so far. Instead
statements like this...

If Not Left(UCase(Worksheets("Sheet1").TextBox1.Text), 1) = "A" And Not
Left(UCase(Worksheets("Sheet1").TextBox1.Text), 1) = "B" Then

use this much simpler equivalent statement....

If Worksheets("Sheet1").TextBox1.Text Like "[!ABab]*" Then

The Like operator can be a very powerful tool at times and this is one of
those times.

--
Rick (MVP - Excel)


"JLGWhiz" wrote in message
...
I think Mike is on to it. I got pretty good results with the code below.
It
only fires twice if something other that A or B is entered. Note that the
Or
was changed to And.

Private Sub TextBox1_Change()
If Not Left(UCase(Worksheets("Sheet1").TextBox1.Text), 1) = "A" And Not
Left(UCase(Worksheets("Sheet1").TextBox1.Text), 1) = "B" Then
TextBox1.Text = ""
MsgBox "Text Input must begin with either an ""A"" or ""B"", Try Again"
End If
End Sub





"Mike H" wrote:

Hi,

It works partly for me but becaue you are clearing the textbox the change
event will fire twice. Once for incorrect input and once when you clear
it.
I'd do it like this

Private Sub TextBox1_Change()
If Left(Worksheets("Sheet1").TextBox1.Text, 1) < "A" And _
Left(Worksheets("Sheet1").TextBox1.Text, 1) < "B" _
And TextBox1.Text < "" Then
TextBox1.Text = ""
MsgBox "Text Input must begin with either an ""A"" or ""B"", Try Again"
End If
End Sub

Mike

"JMay" wrote:

When I click my cb1 (below) my cursor jumps inside tb1 - FINE OK

Private Sub CommandButton1_Click()
Worksheets("Sheet1").TextBox1.Activate
End Sub

But If I type Mary -- nothing happens << but should get message


Private Sub TextBox1_Change()
If Not Left(Worksheets("Sheet1").TextBox1.Text, 1) = "A" Or
Left(Worksheets("Sheet1").TextBox1.Text, 1) = "B" Then
MsgBox "Text Input must begin with either an ""A"" or ""B"", Try Again"
End If
TextBox1.Text = ""
End Sub




  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Help with code

You've used application.enableevents in worksheet code to stop a procedure (say
the _change event) that changes something from calling itself, right?

Well, you can do the same thing with these kinds of controls--but you have to
take care of them yourself:

Option Explicit
Dim BlkProc As Boolean
Private Sub CommandButton1_Click()
Me.TextBox1.Activate
End Sub
Private Sub TextBox1_Change()
If BlkProc = True Then Exit Sub

If Left(Me.TextBox1.Text, 1) = "A" _
Or Left(Me.TextBox1.Text, 1) = "B" Then
'ok
Else
MsgBox "Text Input must begin with either an ""A"" or ""B"", Try Again"
BlkProc = True
TextBox1.Text = ""
BlkProc = False
End If
End Sub

ps. Your If statement seemed weird to me, so I changed it to what I thought you
wanted <bg. And your code doesn't accept lower case a or b.



JMay wrote:

When I click my cb1 (below) my cursor jumps inside tb1 - FINE OK

Private Sub CommandButton1_Click()
Worksheets("Sheet1").TextBox1.Activate
End Sub

But If I type Mary -- nothing happens << but should get message

Private Sub TextBox1_Change()
If Not Left(Worksheets("Sheet1").TextBox1.Text, 1) = "A" Or
Left(Worksheets("Sheet1").TextBox1.Text, 1) = "B" Then
MsgBox "Text Input must begin with either an ""A"" or ""B"", Try Again"
End If
TextBox1.Text = ""
End Sub


--

Dave Peterson
  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Help with code

Hi, I use KeyUp event to check if the first character entered is "A" or "B".
I noticed that need to check and ignore keycode=8, otherwise when type
backspace, it will display the message again.


Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If KeyCode = 8 Then Exit Sub
If Left(TextBox1.Text, 1) = "A" Or Left(TextBox1.Text, 1) = "B" Then
'
' OK
'
Else
MsgBox "Text Input must begin with either an ""A"" or ""B"". Try
again."
End If

End Sub

--AC

"Dave Peterson" wrote:

You've used application.enableevents in worksheet code to stop a procedure (say
the _change event) that changes something from calling itself, right?

Well, you can do the same thing with these kinds of controls--but you have to
take care of them yourself:

Option Explicit
Dim BlkProc As Boolean
Private Sub CommandButton1_Click()
Me.TextBox1.Activate
End Sub
Private Sub TextBox1_Change()
If BlkProc = True Then Exit Sub

If Left(Me.TextBox1.Text, 1) = "A" _
Or Left(Me.TextBox1.Text, 1) = "B" Then
'ok
Else
MsgBox "Text Input must begin with either an ""A"" or ""B"", Try Again"
BlkProc = True
TextBox1.Text = ""
BlkProc = False
End If
End Sub

ps. Your If statement seemed weird to me, so I changed it to what I thought you
wanted <bg. And your code doesn't accept lower case a or b.



JMay wrote:

When I click my cb1 (below) my cursor jumps inside tb1 - FINE OK

Private Sub CommandButton1_Click()
Worksheets("Sheet1").TextBox1.Activate
End Sub

But If I type Mary -- nothing happens << but should get message

Private Sub TextBox1_Change()
If Not Left(Worksheets("Sheet1").TextBox1.Text, 1) = "A" Or
Left(Worksheets("Sheet1").TextBox1.Text, 1) = "B" Then
MsgBox "Text Input must begin with either an ""A"" or ""B"", Try Again"
End If
TextBox1.Text = ""
End Sub


--

Dave Peterson

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
split post code (zip code) out of cell that includes full address Concord Excel Discussion (Misc queries) 4 October 15th 09 06:59 PM
Run VBA code only worksheet change, but don't trigger worksheet_change event based on what the code does ker_01 Excel Programming 6 October 3rd 08 09:45 PM
Shorten code to apply to all sheets except a few, instead of individually naming them, and later adding to code. Corey Excel Programming 3 December 11th 06 05:14 AM
Protect Sheet with code, but then code will not Paste error. How do i get around this. Please read for explainations.... Corey Excel Programming 4 November 25th 06 04:57 AM
Excel code convert to Access code - Concat & eliminate duplicates italia Excel Programming 1 September 12th 06 12:14 AM


All times are GMT +1. The time now is 08:30 AM.

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"