View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Mike H Mike H is offline
external usenet poster
 
Posts: 11,501
Default SetFocus back to textbox after exiting

Hi,

Right click your sheet tab, view code and paste this in

Sub extractnumbers()
Dim RegExp As Object, Collection As Object, RegMatch As Object
Dim myrange As Range, C As Range, Outstring As String
Set RegExp = CreateObject("vbscript.RegExp")
RegExp.Global = True
For x = 1 To 2
If x = 1 Then
RegExp.Pattern = "\d"
Else
RegExp.Pattern = "\D"
End If
Set myrange = ActiveSheet.Range("a1:a20") 'change to suit
For Each C In myrange
Outstring = ""
Set Collection = RegExp.Execute(C.Value)
For Each RegMatch In Collection
Outstring = Outstring & RegMatch
Next
C.Offset(0, x) = Outstring
Next
Next
End Sub

Mike

"Sam Kuo" wrote:

Hi

I'm trying to set focus back to a textbox after exiting the textbox, if the
condition isn't met.

I found a similar example in an earlier thread by jimec74, titled "Using
SetFocus with Frames", which was said to work. But when I tested it myself in
Excel 2003, the SetFocus doesn't seem to fire (i.e. focus still jumps to the
next textbox). Is this just me?


Sub Txt_StartValue_Exit(ByVal Cancel As MSForms.ReturnBoolean)

Dim Msg, Title, Response

If IsNumeric(Txt_StartValue.Value) = False Then
Msg = "You must enter a number"
Title = "Non-numeric Value"
Response = MsgBox(Msg,16,Title)
Txt_StartValue.SetFocus
End If

End Sub