How to select an ActiveX Option/Radio Buttons in Form Controls - vba

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

Related

Get value from command button VBA

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

Creation of Excel Form using VBA and save it

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.

Running a VBA script from a button after selecting a chart

I’m running Excel 2010 on Windows 7.
I have four charts on a worksheet. What I want to do is select one of the charts and then click a button to open the ‘Format Axis’ dialogue box. I found the following code online to open the dialogue box. If I select a chart and then run the code from the toolbar (Developer tab, Macros, select macro, press ‘Run’), it works well.
Sub formatXAxis1()
ActiveChart.Axes(xlCategory).Select
Application.CommandBars.ExecuteMso "ChartFormatSelection"
End Sub
The trouble I have is when the VBA script is assigned to a button. If I use a shape as the button, I get “Run-time error ‘91’: Object variable or With block variable not set”. I get the same error if I use an ActiveX Command Button. However, with a Form Control button it works as expected. The only problem with this solution is that it is not possible to change the background colour of the button so it looks out of place against the other macro calling buttons on the worksheet.
I'm guessing that in the first two cases (shape and ActiveX buttons), VBA drops or loses the chart selection when I press the button – even though the chart still appears selected on screen. Is there a fix for this, or am I doing something wrong?
For an ActiveX button, in the Properties set the property
TakeFocusOnClick
to False. That will cause Selection to keep on the chart rather than switch to the button and your code should work. The color can also be changed from the properties box, though you probably already know that.
You need to reference the chart by name. So either have 4 buttons (one for each chart), or ask the user to input the name of the chart, then:
Dim co as ChartObject
Dim c as Chart
Dim NameOfChart as String
NameOfChart = Inputbox("Enter name of chart")
Set co = ActiveSheet.ChartObjects(NameOfChart)
Set c = co.chart
c.Axes(xlCategory).Select
Application.CommandBars.ExecuteMso "ChartFormatSelection"

Select linked cell, cell next to checkbox, or get the checkbox name

I have a document full of Checkboxes and I dont want to write specific VBA code for each checkbox because the file size needs to stay relatively small. What I am trying to do with my code is when the checkbox is checked, it automatically selects the cells next to it(not hard coded in using "Range") and then perform the rest of the programed VBA function.
How do I either get the name of the checkbox, select the linked cell, or select the cell next to the checkbox using some kind of "offset" property? I am completely stumped!
Thanks for your help in advance.
Use Form Controls instead of ActiveX Controls for Check Box.
Following code will not be work with check box from ActiveX Controls. Also, you need to assign macro to the checkbox, simply trying to run this code from VBEditor will give error.
Assuming all the checkboxes are on same sheet, select all your checkboxes and assign them same macro, something like this
Sub checkBoxHandler()
Dim shp As Shape
Set shp = ActiveSheet.Shapes(Application.Caller)
MsgBox shp.Name 'Name
MsgBox shp.TopLeftCell.Offset(1).Address ' 1 Rows below checkbox
ActiveSheet.Range(shp.ControlFormat.LinkedCell).Select ' Select linked cell.
Set shp = Nothing
End Sub
here Application.Caller helps VBA to identify which checkbox is being clicked.

My ComboBox doesn't display the values I've added in VBA

I'm trying to add options to a combo box in a userform. When I run the code, Excel doesn't give any errors, however when the userform shows up it doesn't display the entities I have added to the combobox previously. That is, when I click on the combobox, it doesn't show any options, only one blank row, as if no items were added to it.
Here is the code I'm using:
Private Sub UserForm_Initialize()
ComboBox1.AddItem "xxx"
ComboBox1.AddItem "yyy"
ComboBox1.AddItem "zzz"
End Sub
I am using the following code to call the user form within a macro:
UserForm.Show
The code given in the question works perfectly well. In my case the code didn't work because I manually entered this part of the code into VBA:
Private Sub UserForm_Initialize()
If you make Excel create this module for you instead of writing it on your own, your code should work perfectly. Excel did not have "Initialize" as a default form so I tried "Activate" and it worked.
To create this module you have to do the following steps:
Right click on user form
Click on view code
On top you will see two categories you can pick, you should pick "Userform" and "Activate", after completing this step excel must add a new module to your code.
In this module you may code everything you want about the content of the combobox.
You should also be careful with the spelling of your combobox, if you spell it incorrectly, you may be unable to see the contents of the combobox.
Ensure that the code segment you have posted is in the userform.
Right click on the user form in the VBA view and choose "View Code". Is this where the code is?
Are you sure that the User Form is called 'UserForm' and not 'UserForm1'? 'UserForm1' is the default, similar to 'ComboBox1'.
The below works for me.
'in the UserForm1 code
Private Sub UserForm_Initialize()
ComboBox1.AddItem "xxx"
ComboBox1.AddItem "yyy"
ComboBox1.AddItem "zzz"
End Sub
The below will display the form.
UserForm1.Show
Is this the only form in the workbook? Create a new one and see if it does the same thing.
Same problem here, but I solved setting ColumnWidths property to -1 (it was set to 0).