TEXTBOX COPY AND FORMAT !!

J

jay dean

Hello -

On my userform, I have 2 questions:

1. How do I use vba to copy the contents of a textbox to the clipboard?

2. How do I format any 10-digit number I enter into a textbox to be of
the form ###-###-####. Example: entering 7891452236 in the textbox
should show as 789-145-2236.

Any help would be appreciated!
Thanks
Jay


*** Sent via Developersdex http://www.developersdex.com ***
 
C

Chip Pearson

1. How do I use vba to copy the contents of a textbox to the clipboard?

Dim DataObj As New MSForms.DataObject
DataObj.SetText Me.TextBox1.Text
DataObj.PutInClipboard
2. How do I format any 10-digit number I enter into a textbox to be of
the form ###-###-####.

Allow only numeric values:

Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
KeyAscii = 0
End If
End Sub

Format string:
Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim S As String
Dim T As String
Dim N As Long

With Me.TextBox2
For N = 1 To Len(.Text)
Select Case Mid(.Text, N, 1)
Case "0" To "9"
T = T & Mid(.Text, N, 1)
Case Else
End Select
Next N
If Len(T) <> 10 Then
Cancel = True
.SelStart = 0
.SelLength = Len(.Text)
.SetFocus
MsgBox "Invalid data"
Exit Sub
End If
S = Left(T, 3) & "-" & Mid(T, 4, 3) & "-" & Right(T, 4)
.Text = S
End With
End Sub


Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top