Excel 2007 Combo Box - Developer Ribbon vs. VBA Module - excel

Could someone please explain to me the difference between the combo box that's available via the Developer Ribbon in Excel 2007 vs. the Combo Box control that's in the VBA editor? I cannot get this simple line of code to work using the Developer combo box:
MsgBox Combo1.Value
I've tied it to the change event and it seems to be syntactically correct (I'm not a VBA coder by any stretch).
Is the Developer Ribbon version some bastardized craptastic Microsoft shortcut?
What I'm trying to do is populate a second combo box based on the selection of the first combo box. I'd rather not build a case statement for every possible selection. Is this possible using the Developer ribbon version?

You are talking about the Insert button on the Developer tab correct? From that button you can add an ActiveX control or Form control. You're better off using the form controls if your new to programming as they will behave more in line with any Excel VBA reading you've done and the help file. With the Form controls you can right click and choose 'View Code' and/or 'Rename Control and Code'. Renaming the control allows you to address it in VBA however you like. e.g. - Combo1.value or myFavoriteCombo.value
That being said, to answer your question directly, be sure you know the controls full name. If you used a form control and it was the first one you put on the sheet it will be named ComboBox1. To get to the combobox's properties you have to walk through it's 'parent' sheet.
i.e.
MsgBox Sheet1.ComboBox1.value (using the sheet's code name)
or
MsgBox Worksheets("SheetName").ComboBox1.value (using the sheets name as it appears on the Excel tab)

Related

Trying to ID a CommandButton on an Excel 2010 VBA application

I've just started a new contract and am working with Office VBA again after about 20 years. There is a spreadsheet I'm working on where command buttons disappear after the document saves. I've been reading other threads that suggest as a fix setting the size of the button after saving. However, I can't figure out how to identify that button in code.
When I click "Format", there is no place where the name of the object appears. There's a macro attached to it, but I see no way to identify the "sender" when the macro fires.
The macro pops up a form, and after the user enters some information and submits, it saves the sheet, and the button goes "poof".
Is there any way to get the name of the button in the macro so I can resize it?
Thanks!
If you right-click the button, its name appears in the upper left hand corner of the spreadsheet in the Developer ribbon, right below the "Visual Basic" and "Macros" menu items. In my case, it was "Button 1".
Accessing it was non-intuitive for a guy used to working with full-blown .NET apps, but the button is a "Shape". So, addressing the button is accomplished with the identifier ActiveSheet.Shapes("Button 1"). With this object, you can access its properties and methods.

Creating a button on Excel Ribbon to open Userform Window

I have developed a VBA userform interface that allows the user to input values, have it calculate results, and then print them out into the worksheet. I am wondering if there is any possible way that I can have a button on the excel ribbon, say under the "Data" tab for instance, that would elicit the userform interface to open.
Currently the only way I can use the interface is by Developer>>Visual Basic>>Clicking on Userform>>F5. This also limits the userform in that it can only be used in one particular workbook as opposed to many. Or if it was somehow possible, to make it an add-in instead.
Any help is greatly appreciated!
The easiest way to add custom tab and/or button to Ribbon is to use Custom UI Editor Tool for MS Office Ribbon. Follow the links provided in comments to your question too. There you'll find many useful information.

how to put the select range dialog box onto an excel vba form as a gui control

Some built in excel formulas and functions require you to select a range and they give you a gui control to do so.
How can incorporate one of these Range GUI controls into a VBA form to allow the user to select a range?
An example of this Select Range GUI control can be found on the Create New Formula Rule (for Conditional Formatting) below.
This is called a RefEdit control.
You may need to enable this by right-clicking the Controls Toolbox and selecting the option for "Additional Controls". Scroll down and check the box for RefEdit.Ctrl. This will add the icon to the Controls Toolbox (it is third from the right, in the second row in my screenshot).
You may also need to enable this from the References in the VBE:
These controls return an address string based on the range selected by the user.
NOTE This control does not seem to play well if the user form is shown vbModeless. If you don't know what that means, then you don't need to worry about it. A form's default behavior is to .Show vbModal, and it should work in that context. See also HERE for other potential pitfalls when using a RefEdit control.

How to add user form as an icon/button to ribbon and show userform but still able to work with Excel File?

When I user the user form, I have to:
1) Alt + F11
2) Choose the Form
3) Run
4) Close the Form
5) Go back to Excel
Excel will not allow me to do anything if the form is not closed. Is there anyway to let me put a little icon on ribbon? And keep the user form appear while I am working with Excel?
You've got two parts to your question:
Adding an icon to the ribbon: Do you want the macro to be available for all spreadsheets?
If so, follow this guide to save your macro as an Excel Add-in, and then attach it to the ribbon.
If you only need it in the current spreadsheet, you could simplify things by adding a button to the spreadsheet which activates the macro (use this guide), or you could use a shortcut key to invoke the macro directly (use this guide, Assigning a shortcut to an existing macro section)
Keeping the dialog open: One of the properties of the UserForm is ShowModal; you can simply set that to false. Alternatively, as per the other answer, you can open it with MyForm.Show vbModeless.
Note that the properties of the form also allow you to provide a specific screen position too, so that the form isn't in the way while you're working: change StartUpPosition to 0 - Manual, and provide a value for Top and Left.
You don't need to do that :) You can simply launch the form in modeless mode to keep it open and work with the Excel file at the same time
Try this to launch the userform.
Sub Sample()
Userform1.Show vbModeless
End Sub
I think you should have to create another module and call the userform in that module. After that just put that macro on the ribbon.It may help you.....

How do I add a form in an Excel sheet?

How can I add an input form in an Excel sheet. I want to insert values into an Excel cell using the form.
ALT+F11 starts the VBA editor in Excel. In there you can create UserForms (Insert | UserForm) which you can bind into their own modules or attach to worksheets.
I suggest you look at the Excel help or MSDN for details of what you can do with UserForms. There are also a lot of VBA tutorials on the web for exactly this sort of thing. Google is your friend.
HTH
There is a quick and dirty method of creating simple forms in Excel. No VBA is required.
First, create a table with the basic format that you want.
E.g.
A B C
1 Name Age Favourite Animal
2 Jane 11 Horse
Select the range, and then select Data | Form. (This is in Excel 2003.)
It will open up a simple form that allows navigation, creation, deletion, and searching.
#Oddthinking,
Cool trick. Never knew it existed. I had to do a little looking, but you can do it in Excel 2007 as well.
Click the Office button, select Excel Options, go to the Customize tab
Select 'Commands Not in the Ribbon' from the 'Choose commands from' drop down
Find 'Form' in the list and click Add, then click OK
At this point, the Forms command will show up on the Quick Access toolbar, next to the Save and Undo/Redo icons.
You have to activate the form toolbar.
Of course, IIRC, the form must be handled by VBA code.

Resources