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
Related
I want whenever I click on my ActiveX command button on Sheet1 it open VBA Editor and jump directly to the code I wrote for a button on my userform. I know the way to jump directly to code for a module but I couldn't find a property for userform button control (something like ThisWorkbook.VBProject.VBComponents("UserForm1").Controls("MyButton"))
Following is my code for jumping right into my Madule1 code:
Private Sub CommandButton1_Click()
Dim Wb As Workbook: Set Wb = ThisWorkbook
'---Open Module1
Application.VBE.MainWindow.Visible = True
Wb.VBProject.VBComponents("UserForm1").Activate
End Sub
I thaught these pictures would help to better understand my goal:
1-I have an ActiveX command button on sheet1
2-I have userform with a commandbutton on it
3-This commandbuttun has it's own code which I want to jump to it's code
If you want to jump to that specific sub directly, try this:
Private Sub CommandButton1_Click()
Application.VBE.ActiveVBProject.VBComponents("UserForm1").CodeModule.CodePane.Show
With Application.VBE.ActiveCodePane.CodeModule
lStartLine = .ProcStartLine("CommandButton1_Click", 0) '"CommandButton1_Click" being the name of the sub you want.
.CodePane.SetSelection lStartLine, 1, lStartLine, 1
End With
End Sub
First line should show you the code panel for the userform, and the second part selects the row down by the sub you are looking for by name.
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
my question is in what event the submit button enable should be written if text boxes are filled, for VBA
for example im talking about the Private sub userform initialize()
i had created a userform where i used 2 frames. while the macro runs it initialize the first frame and user logins using that form to go the next form which is in the other frame.
in this second frame form i have 3 text box fields. only if the user inputs all the three text box then the command button should be enabled. im now stuck with in which event this code should be written.
Thank you
assuming the button is named after "CommandButton1" and the three textboxes are name after "TextBox1", "TextBox1" and "TextBox3", then in the Userform code pane add the following:
In UserForm_Initialize place:
Me.CommandButton1.Enabled = False
add Change event handler for all those three textboxes
Private Sub TextBox1_Change()
checkEnableButton
End Sub
Private Sub TextBox2_Change()
checkEnableButton
End Sub
Private Sub TextBox3_Change()
checkEnableButton
End Sub
finally add the following:
Sub checkEnableButton()
Me.CommandButton1.Enabled = Me.TextBox1.Value <> "" And Me.TextBox2.Value <> "" And Me.TextBox3.Value <> ""
End Sub
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
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"