Run-Time Error '-2147417848 (80010108)' when using ComboBox events [duplicate] - excel

I need a workbook to display the Combobox List Dropdown when it opens.
The combobox in the Workbook is a form control, so a shape.
Cant seem to find the associated property.

If you are using ActiveX Controls then see the below, else if you are using Form Controls then replace them with ActiveX Controls if you want the drop down to happen via code. The below code will work for both ActiveX Controls in a Form as well as Worksheet. If the Control is on a worksheet then change ComboBox1.SetFocus to ComboBox1.Activate
Two ways I can think of...
Using a simple command
Tried And Tested
Private Sub CommandButton1_Click()
ComboBox1.DropDown
End Sub
Using Sendkeys. Sendkeys are unreliable if not used properly.
Tried And Tested
Private Sub CommandButton1_Click()
ComboBox1.SetFocus
SendKeys "%{Down}"
End Sub
SCREENSHOTS

I have had plenty of crashes with .dropdown but find some success with
the SendKeys...

I consider best
UserForm combo box is as Above by Siddharth Rout
ComboBox1.SetFocus
SendKeys "%{Down}"
for some Combo boxes on a worksheets
CB.DropDown Is enough
.. Just as well as they have no setfocus or activate

Related

Excel Macros run but don't show in Developer Tab

I created a spreadsheet in the past which contained buttons that would run macros in the workbook. When I open the spreadsheet now and press the button, the macro will run perfectly. However, on the Developer tab, there are no macros listed and no code available. Is there a way to make the macros visible?
I do not understand. Is that your problem?
Option Explicit
Option Base 1
' This Macro is shown.
Public Sub X()
MsgBox ("X")
End Sub
' This Macro is hidden.
Public Sub Y(ByVal N As Long)
MsgBox ("Y")
End Sub

Using the value of a textbox in a userform to activate another textbox on a worksheet

Firstly, I am as beginner as beginner gets.
I am trying to take the value from a text box embedded in a user form and then using it in another line of code to activate another text box on a worksheet.
Private Sub CommandButton1_Click()
UserForm2.Hide
Dim FieldName As String
FieldName = UserForm2.TextBox1.Value
Worksheets("Quote").FieldName.Activate
End Sub
I hope you can see what I'm attempting from this code. The error is "Run-time error '438': Object doesn't support this property or method".
The value obtained from the textbox is identical to the name of the other one and has been tested to see if it can be retrieved or not.
If someone could tell me the correct functions to use, it'd be much obliged.
Concerning the name of your Sub - Private Sub CommandButton1_Click, I assume that the button is in a form and the TextBox is in a worksheet. In general, there are two types of TextBoxes in Excel - ActiveX control or a Form control and they are handled differently:
What is the difference between "Form Controls" and "ActiveX Control" in Excel 2010?
If you are not sure whether the TextBox is an ActiveX control or a ControlElement, then the following would work. Somehow.
Private Sub CommandButton1_Click()
On Error Resume Next
Dim NameOfTextBox As String
NameOfTextBox = UserForm2.TextBox2.Value
Worksheets(1).OLEObjects(NameOfTextBox).Activate
Worksheets(1).Shapes(NameOfTextBox).Select
Unload Me
On Error GoTo 0
End Sub
Caution: This is a good example of a "quick and dirty" code, consider only using it for learning reasons.
In production, it is really a good idea to use the Forms as they are supposed to be used (see https://codereview.stackexchange.com/questions/154401/handling-dialog-closure-in-a-vba-user-form) and try to avoid On Error Resume Next.

How to combine listbox code in Excel VBA

I am trying to do an Excel project for work. It involves a lot of listboxes. I needed the listboxes to have a double click event where the single click selects and a double click deselects. After some research, I was able to find some code that would perform that function perfectly:
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Application.EnableEvents = False
Me.ListBox1.ListIndex = -1
Application.EnableEvents = True
End Sub
However, there is so much code for all of the listboxes that I think it is making the file glitch. Excel has crashed several times. Each listbox just needs the double click event. Is there a way to combine all this into one general command? Please dumb down your responses, as I am not a programmer. I am in way over my head. Thanks!

Sheet select from a Userform button causes data entered to go on previous sheet

I call a macro called SelectSheet1, from a button on a userform, to select Sheet1.
When data is entered afterwards it is put on the previous sheet.
This is happening on Excel 2013. I confirmed it is not a problem in Excel 2007.
Also it is not a problem if the macro is run directly from the developer tab, keyboard shortcut, quick access toolbar or ribbon customization.
The userform command button code:
Private Sub CommandButton1_Click()
Call SelectSheet1
Unload UserForm1
End Sub
The SelectSheet1 macro selects the sheet:
Sub SelectSheet1()
Sheets("Sheet1").Activate
End Sub
Link to xlsm file in dropbox
Link to youtube video if you want to see with your own eyes
It is a strange error and wondering if it a problem with Excel 2013, something changed in the way they do things and possibly there is a workaround.
Make sure there is only a single sheet Select'ed before you Activate a sheet:
Sub SelectSheet1()
Sheets("Sheet1").Select
Sheets("Sheet1").Activate
End Sub
Remember: "Although only a single sheet may be Active, many may be Selected."
I was able to figure out how to get it to work, but not sure why this solves the issue.
Im unloading the Userform before call macro and I tried using select instead of Activate, those did not help. But after I switched so that the UserForm loads with vbModeless then my problem went away, not sure why this fixed it.
For me the key to fixing this issue was vbModeless, but would love if somebody who understood more could explain why.
Private Sub CommandButton1_Click()
Unload UserForm1
Call SelectSheet1
End Sub
Sub ShowMainMenu()
UserForm1.Show vbModeless
End Sub
Sub SelectSheet1()
Sheets("Sheet1").Select
End Sub

Excel VBA - call same macro from both Ribbon and AutoOpen

Having just upgraded to Excel 2013, I've moved my macros from a legacy custom toolbar to a custom ribbon menu. All works well, except for one thing. I used to have a macro that ran on AutoOpen, but could also be called manually via a button on the toolbar.
I call my macro from the ribbon using Sub myMacro(control As IRibbonControl) which works. But if I Call myMacro(control As IRibbonControl) in AutoOpen I get an "expected list separator" error. Conversely if I just Call myMacro() in AutoOpen I obviously get a "argument not optional" error. Bit of a Catch 22!
I know that I could just move my code to a third sub-routine, called by two separate macros in the ribbon and in AutoOpen, but before I admit defeat and do this I wonder if there is a way around this.
I have searched the web for a solution to this, but couldn't find anything that answered my particular query.
Thanks
Rob
A simple code as this will help?
Option Explicit
Sub AutoOpen()
Dim ctl As IRibbonControl
myMacro ctl
End Sub
Sub myMacro(control As IRibbonControl)
MsgBox "Hello World"
End Sub

Resources