Macro to copy text from one text box to another

J

John

Is it possible to create a macro that copies a text value from one text box
on a word form to a second box on a page further down the document when a
command button is clicked.

I have so far tried a simple macro of the type;

Private Sub UserForm_Initialize()
TextBox1.Text = ""
TextBox2.Text = ""

CommandButton1.Caption = "Cut and Paste"
CommandButton1.AutoSize = True
End Sub

Private Sub CommandButton1_Click()

TextBox1.SelStart = 0
TextBox1.SelLength = TextBox1.TextLength
TextBox1.Copy

TextBox2.SetFocus
TextBox2.SelStart = 0

TextBox2.Paste
TextBox1.SelStart = 0
End Sub

Where am I going wrong?
 
C

Charles Kenyon

Do you want this to happen only if a command button is clicked or would you
be satisfied if it happens automatically when the first value is changed and
you move to the next field (calculate on exit)? If the latter you don't need
any macro...

From the FAQ

19. How can I enter something once in a document and have it repeat
elsewhere?
http://gregmaxey.mvps.org/Repeating_Data.htm

--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide


--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
D

Default User

If you would put the line of code by the other respondee in the TextBox1
change event
it all goes live ...

Private Sub TextBox1_Change()
TextBox2.Text = TextBox1.Text
End Sub

Krgrds,
Perry
 
J

John

Charles Kenyon said:
Do you want this to happen only if a command button is clicked or would you
be satisfied if it happens automatically when the first value is changed and
you move to the next field (calculate on exit)? If the latter you don't need
any macro...

From the FAQ

19. How can I enter something once in a document and have it repeat
elsewhere?
http://gregmaxey.mvps.org/Repeating_Data.htm

--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide


--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.



Thanks for that Charles,

I currently have some of the text in the form being copied with the
"calculate on exit" function but now need additional boxes that only copy the
text from one box to another when the command button is clicked.

But thanks for the advice and I will look over the links you suggested as
sure to pick up some other useful tips etc.

John.
 
J

John

Default User said:
If you would put the line of code by the other respondee in the TextBox1
change event
it all goes live ...

Private Sub TextBox1_Change()
TextBox2.Text = TextBox1.Text
End Sub

Krgrds,
Perry



Thanks for the tips, and clarification of the earlier reply.

Will give that a try now - I had a feeling it would be one little line in
the code that I was missing.

Cheers. John
 

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