ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   TEXTBOX COPY AND FORMAT !! (https://www.excelbanter.com/excel-programming/427530-textbox-copy-format.html)

Jay Dean

TEXTBOX COPY AND FORMAT !!
 

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 ***

Chip Pearson

TEXTBOX COPY AND FORMAT !!
 


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)


On Sun, 26 Apr 2009 11:56:50 -0700, jay dean
wrote:


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 ***


Jay Dean

TEXTBOX COPY AND FORMAT !!
 
Thanks, Chip ! In the second solutions, you have 2 functions? Do I just
assign them to textbox2 or they have to be assigned to a command button?


Thanks
Jay



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

Jay Dean

TEXTBOX COPY AND FORMAT !!
 

Please ignore my post on how to use 2nd solutions...I figured it out.
Thanks for all the help!

Jay


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


All times are GMT +1. The time now is 02:45 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com