Adding Excel ActiveX Control using VBA - excel

Using Excel 2010 VBA, Win 7 64bit
I'm using the following to add an ActiveX Label but get the error "Can't enter break mode at this time". Any insight as to what I'm doing wrong or a source to read about how to do this is appreciated.
Sub tester()
Dim oLABEL As OLEObject
Set oLABEL = ActiveSheet.OLEObjects.Add(classtype:="Forms.Label.1")
End Sub

It turns out that VBA gives that error when you try to step through adding an ActiveX control. If you just run it it works fine. When you step through and get that error you have the option to Continue but unfortunately it runs the rest of the code, i.e. not in step mode.

Related

"ActiveX component can't create object" Error in Excel

When I run the code below, I get the error in my title at the line indicated. I've looked around StackOverflow and elsewhere and can't find any help that works. Any pointers?
This code was working fine 2 days ago in both Windows 10 and Mac OSX, and then I had another (non-VBA) problem and the Microsoft Tech support representative reinstalled Office 365 to try to fix it. Now, I get this same Excel dialog: "ActiveX component can't create object" whenever I try to run this simple sub, both on Windows 10 and Mac OSX.
Sub changeSheetsZoomLevel()
Dim n As Integer, sh As Worksheet
n = 0
For Each sh In ThisWorkbook.Worksheets ' **<-- error occurs here**
With sh
.Activate
ActiveWindow.Zoom = 90
.Columns("A:A").ColumnWidth = 100
.Columns("B:B").ColumnWidth = 70
.Cells.EntireRow.AutoFit
n = n + 1
End With
Next sh
MsgBox n & " sheets reformatted"
End Sub
I'm only showing this one subroutine, but there are many others, including some that manipulate Word documents.
When I click the "Help" button in these dialogs, the next dialog that appears is different on the 2 operating systems, as shown below.
I even tried Microsoft Support again, and they had me install the 64-bit of Office into Windows instead of the 32-bit version. But that didn't help.
Although I looked into the solution suggested in the comment by #MBB70, I found that was not the problem. Instead, here is the solution that worked for me:
1) Make a new Excel workbook and
2) Transfer all vba code into it from my original workbook
3) Transfer all sheets into it from my original workbook
4) Add the needed reference in the VBA Editor using Tools-->References... as shown below:
The new workbook now works properly in both Mac OSX and Windows 10.

UserForm error: Method 'Properties' of object '_VBComponent' failed

I want to edit my UserForm object in VBE with code.
When I use the following, I get an error as in post title:
Sub myTest()
ThisWorkbook.VBProject.VBComponents("UserForm1").Properties("Caption") = "myCaption"
End Sub
I tried changing "Caption" to "caption", the same error. Also this error happens when I try to set Width or Height properties, not always at first time, but when I run the code again to resize.
Edit 1:
"Trust access to the VBA project object model" in macro settings is enabled.
Edit 2:
Error does not happen when new workbook and UserForm are created. It does happen when UserForm is recreated in workbook in which an error already happened.
Edit 3:
Adding Unload UserForm1 or Set UserForm1 = Nothing before doesn't help.
Edit 4:
For new workbook, if I create UserForm and run this code from a module, but after right clicking on UserForm -> View Object, then I get an error: "Can't move focus to the control because it is invisible, not enabled, or of a type that does not accept the focus." When I confirm, on consecutive macro runs I'm getting an error as in post title.
Edit 5:
The same happens on Excel 2010 and 2016.
May simply try this
Sub myTest()
ThisWorkbook.VBProject.VBComponents("UserForm1").Activate
ThisWorkbook.VBProject.VBComponents("UserForm1").Properties("Caption") = "myCaption"
End Sub
It is working in Excel 2007 and expect to work in any version.
May Please refer my Answer to Post After Import of a Userform into VBComponents Properties cannot be read

Textbox "Method or data member not found"

I am writing an aircraft system emulation using Excel (2013) VBA, Windows 10. I have macros which will allow students to operate the system and display it on the screen. Before the xmas break i was doing some development work and it was working fine. However I have come back to it today I am getting a "Method or data member not found" for some of the textboxes, which are still showing the correct names. I would say that in design mode I can't access the textbox properties.
This is the code that was working:
Sub Batt_MasterOFFExample()
' Indications
Sheet2.Ess1Volt.Text = "---V"
With Sheet2.Ess1Volt
.ForeColor = &HFFFFFF
End With
End Sub
Now when I run it it highlights Sheet2.Ess1Volt.Text = "---V" and gives me the error.
use option explicit to avoid such mistakes. ;-)
Sheet2 isn't declared as a worksheet (dim Sheet2 as worksheet // set sheet2= whatever ) , so I assume you meant Worksheets("Sheet2")
The same thing happened to me. After deleting the textbox and recreating it, everything worked just fine.

Excel 2016 VBA - Problems upgrading from Excel 2013

I developed several userforms and macros for a project in Excel 2013, but when I try to test them in Excel 2016, I get multiple errors on basic VBA functions.
For example I get an "Object doesn't support this property or method" error from the following code:
Private Sub ShowImpact()
FormImpact.Show
End Sub
Another example, the following code gives me an error "Can't find project or library" on the [RIMS_tbl] table reference. This is a named table in the document:
With FormImpact.cboIndustry
.ColumnCount = 11
.ColumnWidths = "0;50;0;0;0;0;0;0;0;0;0"
.RowSource = "=RIMS!" & [RIMS_tbl].Address
.BoundColumn = 1
.TextColumn = 2
End With
As another example, I get the same error message on "Format" method in the following code:
Private Sub txtConLand_AfterUpdate()
txtConLand = Format(txtConLand.Value, "#,##0")
End Sub
Lastly, I have a label that I'm using as a background on the form, which works fine in Excel 2013, but for some reason it covers all of the other controls in 2016, and I can't send it to the back.
I'm really hoping that there is some setting in 2016 that will fix all of these problems, because I can't figure out why so many things would break between these two versions. Thanks for your help!
As mentioned in the comments above, the solution to this problem was in eliminating missing references between different version numbers. In the Visual Basic interface, go to Tools->References. Check for missing references in all versions supported. Thanks #TimWilliams and #Stefan for the solution to this problem.
I never did figure out how to fix the problem of the z-order for the background label. Even setting the v-order for that label programmatically didn't work. But I did come up with a workaround. First I made a small white bitmap file. Then under the page settings, I set the background picture to be the bitmap file. Then I set set the "PictureSizeMode" property "fmPictureSizeModeStretch," which stretched the bitmap to fit the entire page. Finally, I added a border around the entire form.
I had the same problem when migrating an VBA powered Excel from Excel 2010 to 2016. Deselecting the ATLEntityPicker 1.0 Type Library from the References list solved it.

1004: Application-Defined or Object defined error after switching from Excel 2013/2016 to 2010

it's a very simple macro and I read tons of threads, none of which resolves my case. I get the .Caption line highlighted. I tried to Dim the form as new form and it does not help as well.I have a form which I want to call from a button which is apparently not done correctly from my side.
Sub BtnAdd_Click()
frmAddCB.Caption = "Add Transaction"
frmAddCB.Show
End Sub
Cheers,
When executing this step by step with F8, just before the line that was highlighted on my standard module when executing with F5, it jumps to the Private Sub UserForm_Initialize() sub which is in the Form code itself. In that Initialize sub I had this line of code which was causing the error in Excel 2010:
TransTypeComboBox.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names("TransType").RefersToRange))
After I changed this to the old-school way with a loop, it started working properly. Probably something about my dynamic range or something about the RefersToRange is not working the same way after Excel 2010.
For Each TT In Range("TransType")
With Me.TransTypeComboBox
.AddItem TT.Value
End With
Next TT

Resources