Code from 4 command buttons onto 1

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
Can you advise please if & how i can put the code from 4 command buttons onto 1 command button.

Thanks

Rich (BB code):
Private Sub CommandButton1_Click()
Dim specialWord As String
specialWord = "MsgBox (""?""),"
TextBox3.TEXT = Replace(specialWord, "?", TextBox2.TEXT)
End Sub

Private Sub CommandButton2_Click()
Me.TextBox3.Value = Me.TextBox3.Value & " " & Me.TextBox4.Value
End Sub

Private Sub CommandButton3_Click()
Me.TextBox3.Value = Me.TextBox3.Value & " " & Me.TextBox5.Value
End Sub

Private Sub CommandButton4_Click()
Dim specialWord As String
specialWord = " ""?"""
TextBox3.TEXT = TextBox3.TEXT & Replace(specialWord, "?", TextBox1.TEXT)
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
You need to explain what you want this one button to do. You have four completely different actions here. You are populating TextBox3 based on text from four other text boxes. It doesn't make sense to do them all at once.
 
Upvote 0
Ah ok I just thought as opposed to doing one at a time we could just run then all at once or at least the code run the first part then second etc etc

Values are copied one at a time & put in text box3


Would this approach work.
Run the first code then Call Code 2 at the end of this then put Call Code 3 at the end of that put Call Code 4

Code 2,3 & 4 would be in modules
 
Upvote 0
I'm still not following what you want to do without an example. If you combine all four subs into this

VBA Code:
Dim specialWord As String
specialWord = "MsgBox (""?""),"
TextBox3.TEXT = Replace(specialWord, "?", TextBox2.TEXT)
Me.TextBox3.Value = Me.TextBox3.Value & " " & Me.TextBox4.Value
Me.TextBox3.Value = Me.TextBox3.Value & " " & Me.TextBox5.Value
specialWord = " ""?"""
TextBox3.TEXT = TextBox3.TEXT & Replace(specialWord, "?", TextBox1.TEXT)

it's just the same as doing the following. The last assignment is the only one that matters.

VBA Code:
specialWord = " ""?"""
TextBox3.TEXT = TextBox3.TEXT & Replace(specialWord, "?", TextBox1.TEXT)
 
Upvote 0
I’m confused by your reply

Each command button collects a value from each text box & places it in textbox3
Once all 4 values have been collected
I then copy the line of code from textbox3

Example
Textbox1 value = Have
Textbox2 value = A
Textbox4 value = Merry
Textbox5 value = Christmas

So the line of code in textbox3 is HaveAMerryChristmas

The above is done by the user pressing command button1 then 2 then 3 then 4

Just trying to make is less work hence pressing 1 button to do all the work
 
Upvote 0
Oh, I see. Well then, the code I posted will do that. If it is run by clicking button 1 then
VBA Code:
Private Sub CommandButton1_Click()

   Dim specialWord As String

   specialWord = "MsgBox (""?""),"
   TextBox3.TEXT = Replace(specialWord, "?", TextBox2.TEXT)
   Me.TextBox3.Value = Me.TextBox3.Value & " " & Me.TextBox4.Value
   Me.TextBox3.Value = Me.TextBox3.Value & " " & Me.TextBox5.Value
   specialWord = " ""?"""
   TextBox3.TEXT = TextBox3.TEXT & Replace(specialWord, "?", TextBox1.TEXT)

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,076
Messages
6,122,984
Members
449,092
Latest member
Mr Hughes

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top