I am trying to create a excel form with text and combo boxes using VBA where the data I enter in the form should get saved in a different worksheet. There should be a save button on the form for users to click on it so that the data gets saved. Is that possible. please help
It's certainly possible, can you be more specific about what part you're having a problem with?
The general timeline of events would be this:
Create UserForm and add textboxes, comboboxes and save button.
a. To learn how to populate comboboxes, check out these resources: How to add items to a combobox in a form in excel VBA?
Give each of your textboxes, comboboxes and save button a name in properties, so that they can be referenced in the code.
Add code to the Save Button event. To do this, go to your UserForm and double click on the Save Button, this will bring you to the code that will execute when that button is clicked, you'll be adding something like this:
Sheets("Example").Range("A1").Value = textbox.Value
To answer your comment:
You shouldn't have any problem adding arrays to Comboboxes like this:
Private Sub UserForm_Initialize()
ComboBox1.List = Array("One", "Two", "Three")
ComboBox2.List = Array("Four", "Five", "Six")
ComboBox3.List = Array("Seven", "Eight", "Nine")
End Sub
If you're having issues, make sure your code, in this case "ComboBox1... etc." is referring to Comboboxes that exist.
Related
I have created A userform with few command buttons.
I can't seem to figure out how do I get the information from them.
I want to open this userform from another one and then I want the user to choose an option from one of the buttons which will change a specific cell in the table itself
the userform I created
I did not write anything on this userform therefor there is no code from it to show only the design.
how do get the information from the buttons to be written in A specific cell on a specific worksheet?
double click on one of the buttons, in the code menu a new sub should appear. this looks something like:
Sub CommandButton1_onClick()
End sub
Alongside this event method, it also has a few properties. The one that is usefull here is the CommandButton1.Value, this represents the state of the button (either true or false iirc).
So build for each button (I strongly advice giving them custom names to prevent getting lost in the trouble) as sub like this:
Sub CommandButton1_onClick()
if CommandButton1.Value then
ThisWorkBook.WorkSheets("WorksheetName").Range("YourRange").Value = "Some value"
else
ThisWorkBook.WorkSheets("WorksheetName").Range("YourRange").Value = ""
end if
End sub
I have a Form Control with ActiveX Radio/Option Buttons in it.
The Form Control name is Side and contains Option/Radio Buttons with names xOption, oOption, and randomSide.
How would I be able to create a Macro that would allow me to set the radio buttons to a certain value upon opening the workBook. Recording a Macro of me clicking options results in a blank Macro. I've Already tried:
ActiveSheet.Shapes.Range(Array("Side")).Select
ActiveSheet.Shapes.Range("xOption").OLEFormat.Object.Value = 1
But this gives me error 1004 and other codes give me error 91. I'm really new to VBA so if I look stupid you know why.
Try something like this, using Worksheets instead of ActiveSheet:
Private Sub Workbook_Open()
Worksheets("your sheet name here").OLEObjects("xOption").Object.Value = 1
End Sub
As you want it to be selected after opening the sheet. Place this on ThisWorkbook.
You may try something like this...
ActiveSheet.OLEObjects("xOption").Object.Value = 1
I searched for a way to show a listbox on certain event, here a click, but wasn't able to find it
If MsgBox("Souhaitez vous reprendre un bordereau déjà édité?", vbYesNo, "Edition Bordereau") = vbYes Then
PreCheckPlot
Else
rest of commands
And the sub where I want to show the listbox
Sub PreCheckPlot()
ListBox2.Visible = True
End Sub
This doesn't work, and ListBox2.Show doesn't either, it throws an error.
Is it possible to show a listbox on a click, and if yes, how would I write it?
Thank you in advance.
You need to refer to the Sheet as well.
So if your ListBox2 is in Sheet1 then you need to use:
Sheet1.ListBox2.Visible = True
Does it go into PreCheckPlot when you step through?
What is the error?
If you create a userform, put a listbox on it and a button which when pressed shows your message your code then works fine.
I wonder if you are trying to set ListBox2.Visible from outside the form (where it will not know what the form is)
This assumes it is an ActiveX listbox - or is it a forms listbox?
Longtime viewer, first time question asker.
I'm currently working with UserForms within MS Word and have a particular form that can have up to 20 different labels and accompanying textboxes with varying texts. I have all but the first hidden while not in use, however I would like the next label and text box to become visible following input in the previous textbox. So if you enter data (anything) in the first textbox, the next label and text box will become visible. Does this make sense? I've seen other responses here suggest using AfterUpdate() rather than Change() or Click() but can't figure out how to use any of them. I would share my code but at this point I don't have any code to share, other than my labels and textboxes are lblField1 txtField1, lblField2 txtField2...
Any suggestions?
I would suggest using Change event, when using AfterUpdate you need to leave you TextBox for a while to fire the event. If you have only one TextBox visible there is nothing to move to. If you have more TextBoxes you would need to move back to fire AfterEvent and I don't think this is what you expect.
So, double click wherever on your userform and add the following code in code area:
Private Sub txtField1_Change()
txtField2.Visible = True
lblField2.Visible = True
End Sub
Next, add next portion for next textbox:
Private Sub txtField2_Change()
txtField3.Visible = True
lblField3.Visible = True
End Sub
And so on, if only you have an order in controls name you just need to change numbers in the end of control names.
What I am trying to achieve is for a combo box (Combo_sf) selection to dictate the form in the subform control (sf_record) I have about 10 forms, their names are in the combo box data. I am new to VBA and am not sure if my approach is right:
Private Sub Combo_sf_AfterUpdate()
Dim strLoadTable As String
strLoadTable = "Form." & Me.Combo_sf.Value
MsgBox strLoadTable
Forms![frm_Mnu_Manage Configuration Settings]!sf_record.Form.SourceObject = strLoadTable
End Sub
I have placed this in the combobox's after update event but when I make my selection nothing happens in the form. Am I approaching this right or would another way work better?
Your approach should work. I put a combo box named cbxSubform on my main form and added one line of code to its AfterUpdate() event handler...
Private Sub cbxSubform_AfterUpdate()
Me.mySubform.SourceObject = Me.cbxSubform.Value
End Sub
...and changing the selection in the combo box switches the subforms immediately. Are you sure that the AfterUpdate() code for your combo box is actually firing? (You could add a MsgBox or a Debug.Print to check.)
It could be this line which is tripping you up:
strLoadTable = "Form." & Me.Combo_sf.Value
What is your form object called? If your form is called Form.myTableName it could be the . that is throwing it out, try setting it to a form without a dot in its name.
In this line, it seems the code attempts to change the SourceObject property of a Form object.
Forms![frm_Mnu_Manage Configuration Settings]!sf_record.Form.SourceObject = strLoadTable
However, SourceObject is a property of a subform control, not the form contained in that control. So if the subform control is named sf_record, do it this way.
Forms![frm_Mnu_Manage Configuration Settings]!sf_record.SourceObject = strLoadTable
Also, if the after update procedure runs from [frm_Mnu_Manage Configuration Settings], you can use Me to refer to the form.
Me!sf_record.SourceObject = strLoadTable
Finally, if Me.Combo_sf.Value is the name of a form, you don't need to prefix its name with "Form.". It worked either way in my test, but I would just leave off "Form.".
strLoadTable = Me.Combo_sf.Value