Text in Textbox to be added to stored code on command button

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,689
Office Version
  1. 2007
Platform
  1. Windows
Morning,
On my command button I’m looking for it to copy a Textbox value on my userform & insert it into the stored code of which is then placed into another textbox.

Example,
Textbox1 has the value NEW

The command button will have a default string of HAPPY?YEAR

Basically the ? Is where the textbox value should be placed.
The code will then be placed into Textbox2

I plan to use this code on other command buttons BUT some will have characters as the stored code.

Example of stored code.
ABC@&?

Textbox4 has the value Z46

So the text Z46 is added to the stored code so when placed in the Textbox it will be ABC@&Z46
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
@Flashbond

Hi,
You helped me above with the code which wors fine for 1 line of text for my MsgBox
Here is the code in use.
It soutput is
MsgBox ("FIRST LINE OF CODE"), vbOKCancel + vbInformation, "SINGLE LINE OF CODE"

Rich (BB code):
Private Sub GenerateCode_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


Now i wish to be able to allow two lines of code but not sure the correct way to do it.
The output should be
MsgBox("FIRST LINE OF TEXT HERE)" & vbNewLine & vbNewLine & "SECOND LINE OF TEXT HERE", vbOKCancel + vbInformation, "TWO LINES OF CODE"

I have added 2 option buttons for single line or 2 two lines of code which the user must select one of them

OptionButton 8 will copy ONLY TextBox 8 value
OptionButton 9 will copy TextBox8 value & also TextBox 6 value

So basically the special word for 2 lines will be & vbNewLine & vbNewLine &

Rich (BB code):
Private Sub GenerateCode_Click()
   Dim specialWord As String
   
   If OptionButton8 = True Then
   specialWord = "MsgBox (""?""),"
   TextBox3.TEXT = Replace(specialWord, "?", TextBox2.TEXT)
   End If
   
   If OptionButton9 = True Then
   specialWord = "MsgBox (""?""),"
   TextBox3.TEXT = Replace(specialWord, "?", TextBox2.TEXT)
   
   specialWord = "& vbNewLine & vbNewLine & (""?""),"
   TextBox3.TEXT = TextBox3.TEXT & Replace(specialWord, "?", TextBox6.TEXT)
   End If
   
   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

Out put is
MsgBox ("FIRST LINE OF TEXT HERE"),& vbNewLine & vbNewLine & ("SECOND LINE OF TEXT HERE"), vbOKCancel + vbInformation, "TWO LINES OF CODE"

But the syntax is wrong
 
Upvote 0
My code is currently this & Blue code output is correct where Red code output is wrong
Do we need to allow for two special words ?

Rich (BB code):
Private Sub GenerateCode_Click()
   Dim specialWord As String

   If OptionButton8 = True Then
   specialWord = "MsgBox (""?""),"
   TextBox3.TEXT = Replace(specialWord, "?", TextBox2.TEXT)
   End If
   
   If OptionButton9 = True Then
   specialWord = "MsgBox (""?""),"
   TextBox3.TEXT = TextBox3.TEXT & Replace(specialWord, "?", TextBox6.TEXT)
   End If
   
   Me.TextBox3.Value = Me.TextBox3.Value & " " & Me.TextBox8.Value
   
   specialWord = "MsgBox (""?"")"
   Me.TextBox3.Value = Me.TextBox3.Value & " " & Me.TextBox7.Value
   
   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

Im nearly there but when i see the output code its missing some characters
Example
With the code above output is MERRY CHRISTMAS

I am trying to see it as "MERRY CHRISTMAS"
 
Upvote 0
specialWord = "MsgBox (""?"")" Me.TextBox3.Value = Me.TextBox3.Value & " " & Me.TextBox7.Value
I don't understand your project completely. Maybe you should try like this:

VBA Code:
   specialWord = "MsgBox (""?"")"
   Me.TextBox3.Value = Me.TextBox3.Value & "?" & Me.TextBox7.Value
 
Upvote 0
Hi

When i type ABC in Textbox 7 i need the value to then be "ABC" using my special word

Rich (BB code):
   specialWord = "MsgBox (""?"")"
   Me.TextBox3.Value = Me.TextBox3.Value & "?" & Me.TextBox7.Value
 
Upvote 0

Forum statistics

Threads
1,223,157
Messages
6,170,419
Members
452,325
Latest member
BlahQz

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