How to clear Excel ActiveX ComboBox list with VBA. I expected this code would work:
Sheet1.OLEObjects(1).Clear
but it raises
runtime error object doesn't support this property or method.
I am puzzled because this works:
Sheet1.OLEObjects(1).Name
returning the name TempCombo.
I still fail when I try this:
Sheet1.TempCombo.Clear
It returns error:
Runtime error Unspecified error.
What else should I check?
If you want to clear the ActiveX ComboBox list, you may try something like this...
Sheet1.OLEObjects(1).ListFillRange = ""
Or more precisely this...
If TypeName(Sheet1.OLEObjects(1).Object) = "ComboBox" Then
Sheet1.OLEObjects(1).ListFillRange = ""
End If
If the combobox has a name, you can just refer to the name. Like
With mySheet
.cbMyComboBox.ListFillRange = vbNullString
End with
Related
I have just written this easy macro in Excel VBA for merging a group of selected cells:
Sub Macro_Merge()
Dim Temp As String
Dim S As Variant
Temp = ""
For Each S In Selection
If Temp = "" Then
Temp = CStr(S.Value)
Else:
Temp = Temp + "," + CStr(S.Value)
End If
Next
Selection.Merge
Selection.Value = Temp
Selection.VerticalAlignment = xlTop
End Sub
This works fine, but I always see that annoying dialog box, warning me about loosing data while merging (which is exactly what I'm trying to avoid in my macro).
I can get rid of that dialog box, configuration the Application's DisplayAlerts property:
Application.DisplayAlerts = False
Selection.Merge
Selection.Value = Temp
Application.DisplayAlerts = True
This is working fine.
So, as Application is the default object, I tried to clean up my code, as follows:
DisplayAlerts = False
Selection.Merge
Selection.Value = Temp
DisplayAlerts = True
As you see, I simply omit mentioning the Application object. This is something which is allowed and I've done in the past. (If not in VBA, then Delphi, maybe?)
... but to my surprise, the dialog box appears again (although pressing F1 brings me to the official "Application.DisplayAlerts" documentation).
This leaves me with a simple question:
If a simple DisplayAlerts = ... does not equal Application.DisplayAlerts = ... anymore, what does it mean and how can I use it?
For your information, I'm working with Excel-365.
DisplayAlerts is an undeclared variable.
Certain Application properties and methods can (effectively) have the Application omitted:
ActiveCell, ActiveSheet, ActiveWorkbook, ActiveWindow, Addins, Charts, Selection, etc.
Calculate, Evaluate, Intersect, Run, Union, etc.
(but see this answer why/how this works):
A boolean property such as DisplayAlerts (EnableEvents, ScreenUpdating, etc) doesn't fall into the above category.
A golden rule in order not to fall into such a trap is the usage of Option Explicit while writing macros.
Just to add some information to the answer of #BigBen. If you write something like Workbooks or ActiveSheet in your code, VBA is not looking into the Application-object - it is looking into a (rather well hidden) object named Global.
The global object is exposing some (but not all) properties and methods of the Application-object, so ActiveSheet is referring to Application.ActiveSheet - but not because the Application has a member with this name but because the Global object defines that ActiveSheet means Application.ActiveSheet. In fact even the Application-object is accessed via the Global object.
There is hardly any information about this Global object or its concept. I found a page from Microsoft describing the Global object of MS Word, but the only explanation there is "Contains top-level properties and methods that don't need to be preceded by the Application property.". For Excel, I found this page on O'Reilly.
From time to time you get strange error messages like "Excel VBA Method 'Range' of object'_global' failed" - this is a pointer to the Global object. I would be glad to learn more about the concepts and mechanics of this object, but I am afraid that there are only very few people around that know more (except of course Mathieu Guindon AKA Mr. Rubberduck...). In daily life, we take it for granted that things like ActiveSheet simply works.
I'm creating small project in Excel, and because I'm a VBA newbie I do encounter a lot of problems that I'm trying to resolve on my own. However i can't cope with this:
I created Sub that accepts two objects: FormName and ControlName.
What i want it to do, is to loop through every Control in specific UserForm and populate every ListBox it encounters, from another ListBox.
I created this funny string comparison, because I need to operate on objects in order to execute the line with AddItem. This comparison actually works well, no matter how ridiculous it is. However when I launch the program, I got
Type Mismatch error
and to my surprise "Next" is being highlighted. I have no idea how to fix this, nor what is wrong.
Public Sub deploy(ByRef FormName As Object, ByRef ControlName As Object)
Dim i As Integer
Dim O As msforms.ListBox
i = 0
For Each O In FormName.Controls
If Left(FormName.Name & O.Name, 16) = Left(FormName.Name & ControlName.Name, 16) Then
O.AddItem (FormName.PodglÄ…d.List(i))
i = i + 1
End If
Next
End Sub
I call this sub using:
Call deploy(UserForm1, UserForm1.ListBox3)
Above, I use Listbox3 because otherwise i got error saying that variable is not defined. However in my comparison I kinda override this.
If someone can explain in simple words, how to fix this type mismatch issue or how to write it in more elegant way
I have this function called when the user clicks a button. It is supposed to open up a new sheet (if it doesn't already exist), add a listbox, and add values to it. It does a lot more but I removed a lot of code for the sake of posting on here, but made sure it throws the same error. If you see some minor error it is only because I truncated it incorrectly or changed variable names.
The problem is that is throws the error "Object does not support this property or method error" at the line:
ThisWorkbook.Worksheets("FFL").xxx.AddItem "hi"
I have tried calling it in another function from this function and I still get the same error. When I copy and paste this same line into a separate function and run it separately after the sheet is created it works just fine.
I have tried variations like fffl.xxx.AddItem Item:="hi", list.AddItem "hi"
I've tried everything
Public Sub Test()
If sheetExists("FFL") Then
MsgBox ("Only one FFL session can be open at a time.")
Else
pmsID = Application.Caller
Set sheet = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets("FFL Calendar"))
sheet.Name = "FFL"
Set fffl = ThisWorkbook.Worksheets("FFL")
Dim list As Object
Set t = fffl.Range("B7")
Set list = fffl.OLEObjects.Add(ClassType:="Forms.ListBox.1", Left:=t.Left, Top:=t.Top, Width:=t.Width * 2, Height:=t.Height * 5)
list.Name = "xxx"
ThisWorkbook.Worksheets("FFL").xxx.AddItem "hi"
Try replacing the line ThisWorkbook.Worksheets("FFL").xxx.AddItem "hi" with
list.Object.AddItem "hi"
The .OLEObjects.Add method returns an OLEObject object, which doesn't have an AddItem method. However, its .Object property returns the actual underlying object, which is a ListBox in this case: Polymorphism at its best and most confusing :)
I'm trying to use the code in this link, but I keep getting the runtime error 438 "Object doesn't support this property or method" for the line
ActiveSheet.PivotItems(1).Visible = True
Does anyone know what's going on?
Full Code:
Sub DeleteAllFields()
Dim i As Long
ActiveSheet.PivotItems(1).Visible = True
For i = 2 To ActiveSheet.PivotItems.Count
ActiveSheet.PivotItems(i).Visible = False
Next
End Sub
Full disclosure - I am a novice VBA user.
Thank you!
The error relates to you using .PivotItems() with the object ActiveSheet.
The PivotItems() property is part of the PivotFields() object so you would need something like:
ActiveSheet.PivotTables("PivotTable1").PivotFields("Example Field").PivotItems(1).Visible = True
I have a VBA procedure (in Excel 2007) where I aspire to set the ListFillRange property of a combobox styled as a list using an array.
I know this works if I right click the combobox and write "Sheet1!$F2:$F17" next to the "ListFillRange" property. I can also do this in code. However, I am interested in dynamically setting the value of this property by assigning it an array.
I know for sure the array works as I tested it; there is probably a syntax error here:
ThisWorkbook.Worksheets("Sheet1").OLEObjects("cmbS").ListFillRange = ar
when I do this I get:
"Type mismatch" error.
The result of this action should be that the component is populated with the array elements, from element(0) ... to the last element (n-1) of the array. Any pointers, thank you very much!
I also tried:
ThisWorkbook.Worksheets("Sheet1").cmbS.list = ar
and it says "permission denied"
Here are the combobox properties in case it helps:
After testing and trying, I found this works:
ThisWorkbook.Worksheets("Sheet1").cmbS.ListFillRange = ""
Dim i As Integer
For i = LBound(ar) To UBound(ar)
ThisWorkbook.Worksheets("Sheet1").cmbS.AddItem (ar(i))
Next
However, I am interested in populating with all values at once for faster effect, not just adding element by element.
I know its late but maybe it is going to help someone else. At least the following code works (much faster than element for element) for me.
dim arr() as variant
arr = Worksheets("Total").Range("C2:"&lrow).Value
Worksheets("Menu").ComboBox2.List = arr
The only way you can populate a combobox with the content of an array is by doing it element by element. I find it hard to believe that it would be a notably slow process no matter how large your array is.