Clear contents of combo box - excel-2007

I have an invoice spreadsheet with comboboxes to select a product which fills out the product number and name in the invoice. Then I have a clear button that clears the information out to do another invoice, but the combo box and the linked cells don't get cleared. I tried adding the code ComboBox2.Clear or ComboBox2.Value="", or DropDown2.Clear, but I keep getting a run-time 424 error object required. What am I doing wrong.
Sub ClearIncoive()
ClearIncoive Macro
'Clear the invoice
Range("G6:G9,F16,G16:H16,F17,G17:H17,F18:H28").Select
Range("F18").Activate
Selection.ClearContents
ComboBox2.Clear
End Sub
Any help?

I played around with a few things and got the same error you did, so I figured I was on the right track. The combobox I created, with Excel 2010, was a Form Control. I then created another combobox using the ActiveX Control and used
Sheets(1).ComboBox2.Value = ""
Note that I set the contents of the combobox to A1:A8, which contained the data for the combobox.

Related

word vba combo box values not displaying although populated

I am trying to populate Mail Merge Data fields from excel database and I am successful in pulling it, but when trying to add it in user form combo box, it is not displayed. Although combo box is populated with 3 blank rows. Screenshot attached.
Below code is written in Module1. Do I need to write anything in Combobox1_change() in userform as well?
For Each aField In ActiveDocument.MailMerge.DataSource.FieldNames
UserForm1.ComboBox1.AddItem
Next aField
Userform screenshot
You haven't told the userform what to add to the combobox. For example:
For Each afield In ActiveDocument.MailMerge.DataSource.FieldNames
UserForm1.ComboBox1.AddItem afield.Name
Next afield

Set focus on ComboBox (ActiveX Control) after code execution

I have an Excel file with a combobox (name = "Combobox1"). After running a script (basically pasting the selected value in "the next row" of a column) I want the focus to be reset on the combobox at the end of the script, so doing allowing me to type the next entry in the Combobox without having to click on the ComboBox text field first.
This does the job in Excel 2013 but I would like to have it working in 2007 as well:
Combobox1.Activate
Anyone any idea?
Or:
I can replace the combobox with an in-cell dropdown list (data validation) and the same data validation as the one I have in the combobox at the moment, but then I have another issue:
For a ComboBox you can choose to have the dropdown list active, but for an in-cell data validation that is not the case, at least not if you want to be able to type in the cell after the list is shown with ALT+UP or
Application.SendKeys "%{UP}"
Any idea here?
If this combobox is on a worksheet and not a userform, then "Combobox1.select" should return the focus to the combobox.

excel spreadsheet referencing an activex control

I am using Excel 2013. I have added an activex control to my spreadsheet. The control is a checkbox which I have named chkAD1. My spreadsheet is called "timeseries_AD".
I am trying to reference the checkbox to check its value however without any joy. I have tried the lines below,
worksheets("timeseries_AD").OleObjects("chkAD1").Value
This results in the error message "unable to get the OLEObjects property of the worksheet class".
I have read that an activex control has two names. One is the name of the shape that contains the control the other is the code name. I'm not sure which one I have changed. I clicked on my control and in the Name Box renamed it to "chkAD1". Is that the shape name or code name I have changed?
UPDATE - Apologies
Sorry the control I added is not an activex control it is actually a form control.
I tried this and it worked for me.
When I check the box I get a messagebox that says TRUE.
And when I uncheck it I get a messagebox that says FALSE
Private Sub CheckBox1_Click()
MsgBox CheckBox1.Value
End Sub

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.

VBA Excel comboBox dropdown is empty after obj.addItem

I have an Excel2010 VBA userform that has one comboBox, from which the user should be able to select a currently-open Excel Workbook. The USERFORM_Initialize subroutine contains (among other things) :
cbWorkbook.Clear
cbWorkbook.Visible = True
For Each wb In Workbooks
cbWorkbook.AddItem wb.name
Next wb
I have set a breakpoint at this code, and am able to step through it; in the present situation there are four open workbooks, and the "for each" is iterated four times, as appropriate. And I can see that wb.name contains the values that I want.
However, when the form displays and the dropbox arrow is clicked, the "list" is empty. It looks like there is room for one item, and that item is blank. (I believe this is typical of an empty dropdown box.)
Select attributes for the combobox are:
Autosize=False; AutoTab=false; Enabled=True; DropButtonStyle=1-fmDropButtonStyleArrow;
Height=18; ListRows=8; ListStyle=0; Locked=False; ShowOptionWhen=2; SpecialEffect=2; Style=0; Visible=True. At the time of execution, cbWorkbook.listCount = 4
This is in development, and it did appear to work as expected yesterday, but now seems to never work. Any ideas where I might be going wrong?
EDIT: I found the solution to this: I had inadvertantly duplicated another combo box over the top of cbWorksheet, effectively hiding it. The control I was seeing was empty, while the control I wanted was overlaid. Deletion of the rogue control box solved the issues.
My apologies; this should have been the first thing I sought.
I found the solution to this: I had inadvertantly duplicated another combo box over the top of cbWorksheet, effectively hiding it. The control I was seeing was empty, while the control I wanted was overlaid. Deletion of the rogue control box solved the issues.
My apologies; this should have been the first thing I sought.
With ComboBox1
.AddItem "This"
.AddItem "Is"
.AddItem "A"
.AddItem "Test"
End With
or if you want to fill it with Range data:
ActiveSheet.Shapes("ComboBox1").Select
Selection.ListFillRange = "k1:k10"
PS - submit your file for review; it should be easier to look at!