I have a button that is on an excel spreadsheet (not in a userform) and I have a userfrom that has a textbox on and would like it to, when I enter a name in the textbox on my userform for it to then set the Caption of my button that is on my excel sheet to what ever is entered in the textbox. I would like to know what code I need to use and where to insert that code?
Hope that make sense, please keep in mind I'm a newbie to this so this is why I am asking where to insert the code
Many thanks in advance
Code in your userform, assuming a textbox named TextBox1, could be like this:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Len(Me.Textbox1.text) > 0 then
ActiveSheet.Buttons("Button 1").Caption = Me.Textbox1.text
End If
End Sub
or if you want the caption to update as you type:
Private Sub TextBox1_Change()
If Len(Me.TextBox1.Text) > 0 Then _
ActiveSheet.Buttons("Button 1").Caption = Me.TextBox1.Text
End Sub
As you have used "CommandButton" (which is an ActiveX control) yet seemingly heading towards a Form control, I have gone with the 2 methods you will need:
ActiveX:
ActiveSheet.Shapes("YourButton").OLEFormat.Object.Object.Caption = "YourText"
Forms:
ActiveSheet.Shapes("YourButton").TextFrame.Characters.Text = "YourText"
Related
Is there a way to make the vba label for a command button from a cell reference. For example if I create a new command button on a userform the label in the sub routine is CommandButton1. Can I make the 1 a cell value like CommandButton & (Sheets("Sheet1").Range("A1").value) .visible = true?
Thanks for the help.
Try this sub.
Private Sub UserForm_Initialize()
Me.CommandButton1.Caption = ThisWorkbook.Sheets("Sheet1").Range("A1")
End Sub
I have a Userform with a multipage object. In one of pages there is a textbox object where I want to add a placeholder when Enter event is not detected. Problem is I'm not figure out how to make reference to textbox from Enter and Exit events due to I have a multipage structure.
I tried to adapt the following approach but it works only for a Userform without Multipage structure.
Got it here: Text box prompt text in Excel VBA
Private Sub TextBox1_Enter()
TB_enter ActiveControl.Name
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TB_exit ActiveControl.Name
End Sub
Sub TB_enter(TB_name)
If Len(Me.Controls(TB_name).Tag) = 0 Then
Me.Controls(TB_name).Tag = Me.Controls(TB_name).Value
Me.Controls(TB_name).Value = vbNullString
End If
End Sub
Sub TB_exit(TB_name)
If Len(Me.Controls(TB_name).Value) = 0 Then
Me.Controls(TB_name).Value = Me.Controls(TB_name).Tag
Me.Controls(TB_name).Tag = vbNullString
End If
End Sub
Here is a video that shows how to do it when the Userform don't have a Multipage structure: https://www.youtube.com/watch?v=yJ4fnw1zmGU
Setting up properties Tag and Text of textbox I get the placeholder if use a properly Forecolor (&H8000000) but I can't make it works dynamically, that means: delete placeholder text when Enter event occurs and if lenght (Len) of the string inside textbox is zero when Exit event occurs must show the placeholder string again.
Any suggestions? Thanks in advance.
I've been searching and found various links, but none seem to address the problem that I have.
I have a userform with a label, and I want the caption of the label to be whatever cell B2 is. This is what I currently have:
Private Sub Label1_Click()
UserForm1.Label1.Caption = Worksheets("Sheet1").Range("B2").Value
End Sub
My problem is that I have a Label1_Click() and the label only appears in my userform when I click. Which do I choose to make the label appear in my userform immediately as it opens?
Either:
Private Sub UserForm_Initialize()
Me.Label1.Visible = True
End Sub
Or:
Change settings of the label if you don't want it hidden at all:
Click label in editor
Under properties change Visible to True
In an User form with the text box, a cancel button, and an Ok button. The ok button should send the text of the text box to the cell i want. The cancel is just to close.
I researched for it and all i got was this:
Private Sub TextBox1_Change()
GetData
End Sub
Which didn't work.
I'm not a good coder but I'm trying to build something to make my work easier, if someone could help i would really appreciate.
You need write the code in that command button click event..
' ok button
Sub Button1_Click()
' replace the sheet name and range A2 or B2 with yours
Sheets("Sheetname").Range("A1").Value = TextBox1.Value
End Sub
' cancel button
Sub Button2_Click()
unloadme
End Sub
im having some weird things happen with some code in excel
Private Sub CommandButton2_Click()
Dim thiswb As Workbook
Set thiswb = ActiveWorkbook
Call EnterDataToSS(thiswb)
Me.Hide
BeamNoFind.Show vbModal
End Sub
the called code basically drops some values into a spreadsheet.
any left over values are populated to LISTBOX1 on BeamNoFind Userform.
Then on the BeamNoFind userform there is a button and the LISTBOX1. when you select and item from listbox1, and click the button, a third userform opens (VBMODELESS) to allow placement of the value.
below is the code of the button to show the third userform.
Private Sub CommandButton2_Click()
Dim Selected_Length As String
Dim Selected_Name As String
Dim Selected_Length_index As Integer
Selected_Length_index = BeamNoFind.ListBox1.ListIndex
With BeamNoFind.ListBox1
If .ListIndex > -1 Then
Selected_Name = .Column(0, .ListIndex)
Selected_Length = .Column(1, .ListIndex)
CellInputForm.beam_length_label.Caption = Selected_Name & " [ " & Selected_Length & " ] "
BeamNoFind.Hide
'ChngDataSrc.Hide
'Unload ChngDataSrc
CellInputForm.Show vbModeless
Else
MsgBox "No selection", vbExclamation, "Oops!"
End If
End With
End Sub
the weird thing is, when i click the button to show my modeless userform, to place the data in a cell, the initial macro is being triggered that drops you into the first userform.
Where i have the commented code 'ChngDataSrc.Hide' and 'Unload ChngDataSrc' are my attempts to stop the userform from displaying when i dont want it to.
When i unload the form, i then get an error instead of the form displaying, the error is with the initial macro:
Sub get_scheduling_data(control As IRibbonControl)
ChngDataSrc.Show
End Sub
It has something to do with vbModeless because if i replace "vbModeless" from "CellInputForm.Show vbModeless" line with "vbModal", it shows correctly, without the unwanted form (ChngDataSrc). but then the function of the form (select cell, press ok button, value placed in selected cell) is gone.
I found a solution, but its not a real solution,
i placed
ChngDataSrc.Hide in the Activate sub of the CellInputForm userform.
So when CellInputForm.show vbModeless is run, and the ChngDataSrc userform pops up unwantedly, it is then hidden again.
Id rather find out why it is being showed in the first place, but this fix seems to work for now.