Button on a userform to add and remove text from left side of textbox?

vbaNumpty

Board Regular
Joined
Apr 20, 2021
Messages
171
Office Version
  1. 365
Platform
  1. Windows
I am wondering how I would go about adding a button to a userform that could add and remove a portion of text to the left side of a textbox.

Simple example would be a button labeled 'ATTENTION' that when clicked would add the following text to the left of a notebox: "**ATTENTION**"

Clicking the button a second time would 'undo' and remove the same text from the left side of the notes box.

When I say the left side, the idea is that there can already be text in the note box and clicking the button acts as a toggle to add and remove the text '**ATTENTION**' to the notesbox without disrupting or impacting existing text. The left side so that it precedes any existing text when viewed later in a spreadsheet.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Give this CommandButton Click event code a try...
VBA Code:
Private Sub CommandButton1_Click()
  Dim Word As String
  Word = "*ATTENTION* "
  If TextBox1.Text Like Replace(Replace(Replace(Word, "*", "[*]"), "?", "[?]"), "#", "[#]") & "*" Then
    TextBox1.Text = Mid(TextBox1.Text, Len(Word) + 1)
  Else
    TextBox1.Text = Word & TextBox1.Text
  End If
End Sub
Note: I generalized it for you so you can assign any text to the Word variable and the CommandButton will perform the toggle effect you asked for.
 
Last edited:
Upvote 0
Solution
Give this CommandButton Click event code a try...
VBA Code:
Private Sub CommandButton1_Click()
  Dim Word As String
  Word = "*ATTENTION* "
  If TextBox1.Text Like Replace(Replace(Replace(Word, "*", "[*]"), "?", "[?]"), "#", "[#]") & "*" Then
    TextBox1.Text = Mid(TextBox1.Text, Len(Word) + 1)
  Else
    TextBox1.Text = Word & TextBox1.Text
  End If
End Sub
Note: I generalized it for you so you can assign any text to the Word variable and the CommandButton will perform the toggle effect you asked for.
Works like a charm! Thank you very much.
 
Upvote 0

Forum statistics

Threads
1,223,630
Messages
6,173,454
Members
452,514
Latest member
cjkelly15

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