"ActiveX component can't create object" Error in Excel - 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.

Related

Excel.Application.Visibile not working on Office 2016

I need help reguarding the following code written in Visual Basic 6.0.
Private Sub cmdExcel_Click()
Dim obj As Object
Set obj = CreateObject("Excel.Application")
On Error Resume Next
MkDir "c:\temp"
On Error GoTo 0
MousePointer = vbHourglass
On Error GoTo err
objGrid.m.ExportToXLS "c:\temp\test.xls"
obj.Workbooks.Open "c:\temp\test.xls"
obj.Visible = True
obj.Interactive = True
On Error GoTo 0
err:
MousePointer = vbNormal
End Sub
The enviroment: Windows 10, Office Excel 2016.
The problem: the workbook doesn't show up, but I can still find an Excel instance in Task Manager. If I shut down the instance I can find the Excel in c:\temp and the application starts working again.
The goal: show the Excel just created.
Do you have any solutions?
Thanks in advance.
Sara
I found the solution.
The problem wasn't in the code but in some Excel's settings.
First, I modified the code in order to get a more detailed error. Basically I switched the two instructions which allow Excel App to be visibile and the file to be opened:
obj.Visible = True
obj.Workbooks.Open "c:\temp\test.xls"
In this way, I was able to open Excel App and read the reason why the file could not be opened.
As you can see the file type is Excel 4 Worksheets which is blocked on opening.
I had to go Trust Center and disable it.
Just to be sure, I disabled also Excel 4 Workbook.
After performing the steps above, my app worked.

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.

Adding Excel ActiveX Control using VBA

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.

VBA fails when opening PowerPoint presentation on Windows 7

I wrote a VBA macro in Excel 2007 on Windows XP to copy data from an excel spreadsheet into a powerpoint presentation.
When this macro enabled spreadsheet was run on a fresh install of Windows 7 it fails.
So I pulled out the code that fails to pin point the problem and it seems to fail when trying to open an existing powerpoint file. I have tried running this code in both Office 2010 and Office 2007.
The code I am trying to use it (just the problem parts shown below)
Sub test()
Dim PowerPointApplication As PowerPoint.Application
Dim PowerPointFile As PowerPoint.Presentation
Set PowerPointApplication = CreateObject("PowerPoint.Application")
Set PowerPointFile = PowerPointApplication.Presentations.Open("PATH_TO_FILE\test.pptx")
End Sub
The macro fails on the Presentations.Open line above with the following error
Run-time error '-2147467259 (80004005)':
Method 'Open' of object 'Presentations' failed
I have already enabled the PowerPoint 12.0 Object Library in the references settings in the VBEditor for the spreadsheet. All the other references match exactly with the file that runs without error on my Windows XP box.
I have looked all over the web for an answer and cant find anything. I read something about Windows 7 and offline files, so tried turning that off but it didnt help.
I am logged in as an administrator user as well, and tried moving the pptx that I am opening to other directories as well with no success.
I am running the following version of Windows:
Windows 7 Professional
Service Pack 1
64 Bit
Any help would be appreciated!
Is PATH_TO_FILE a variable (or constant)??
If so shouldn't it be
PowerPointFile = PowerPointApplication.Presentations.Open(PATH_TO_FILE & "\test.pptx")
This does work in office 2016 on Win7 SP1 64bit OS
Should be good for Office 2010.
Could be John's advise on the path to file, also.
Sub test()
Dim PowerPointFile As PowerPoint.Presentation
Dim PPTObj As Object
Set PPTObj = CreateObject("PowerPoint.application")
Set PowerPointFile = PPTObj.Presentations.Open("C:\test.pptx")
End Sub

#NAME? error in Excel for VBA Function

I am making my first VBA program and trying to run the following function. The function checks a specific named range for the first row which does not have a value greater than it's leading value, but less than 1.
Public Function findPurchase()
Dim CRT As Range
Set CRT = Range("CostRateTable")
Dim existsBetter As Boolean
existsBetter = True
Dim r As Integer
r = 2
Dim c As Integer
c = 4
While existsBetter
Dim Found As Boolean
FoundBetter = False
While Not FoundBetter And c <= CRT.Columns.Count
If CRT(r, c) > CRT(r, 2) And CRT(r, c) < 1 Then
FoundBetter = True
Else
c = c + 1
End If
Wend
existsBetter = FoundBetter
If existsBetter Then
r = r + 1
End If
Wend
findPurchase = CRT(r, 3)
'MsgBox(findPurchase)
End Function
I know the function does what it is supposed to because I have both manually checked the table of values, removed the comment ' from the MsgBox, and used the debug tools to step in and out of each of the functions steps as it went through the table. However, when I reference the function in Excel with =findPurchase() I'm given a #NAME? error. The function even shows up in the function auto-complete box when I begin to type its name. When I write other functions, both with and without parameters, I can reference them just fine, for example:
Function addtwo()
addtwo = 1 + 2
End Function
What am I doing wrong with my function which causes it not to work?
You are getting that error because you have a module with the same name as the function.
Change that name to say find_Purchase and everything will be fine :) See the image below...
I had the same issue myself. It turned out that I "Saved As..." another file and macros were not enabled for that file. No banner on the top appeared, but a #NAME? error was generated.
I reopened the file, enabled macros, and the problem was resolved.
Make sure you have placed the function in a Standard Module. The error message means Excel can't find the function.
When Excel opens an unkown workbook containing VBA-Code, it usually asks for macros to be enabled by the user (depending on the application settings).
If the user then enables the macros, all event-driven procedures will be started, such as auto_open or others.
Custom VBA Functions however require for a full recalculation of the workbook. Otherwise the functions return-value still is #NAME, as the calculation is only done directly after opening the workbook.
In order to work directly at the first time opening, one has to add the following line to the workbook_open event
'
' Workbook open event
Private Sub Workbook_Open()
Application.CalculateFullRebuild
End Sub
Check "Trust access to the VBA project object model" in Macro settings from Macros security
One reason for this problem is security restrictions.. I had this problem and I activate "Enable all macros" from security center, and the problem solved
I had a similar persistent problem with one of my functions when everything else seemed fine.
Open the worksheet & go to the Developer Tab. Open VBA, and back on the Developer ribbon select "View Code". See if it opens any similar Code (apart from your Module) specific to that worksheet (eg. Sheet2 (Code). I found that I had duplicated the code on the worksheet in addition to the Module. Delete the "worksheet" code. (You may need to save the workbook & re-open at this stage). When I deleted the worksheet code, the module function then worked.
In addition to checking some of the above mentioned items, you might need to specify the filename where the custom function is actually defined, e.g. cell content
=XLstart.xlsm!myCustomFunc(Arg1,Arg2)
where myCustomFunc is defined in the startup file XLstart.xlsm.
Following the Excel help for "Correct a #NAME? error":
In the formula bar, select the [suspect] function name.
In the Name Box (to the left of the formula bar), click the arrow and then select a [user-defined] function from the list that Excel suggests.
This will add the filename per the above format.
MS 2010, Windows 10.
Here's why I got that error. This answer is not provided so far.
If you have two or more workbooks (spreadsheets) open, then you may have your module under the other workbook - not the only you want to do the calculation on. This may seem impossible but ... as soon as you open the Developer/VBA code editor Excel wants to show you the structure (objects, modules, etc) of every open workbook. It's not what I expect as a developer, but there it is. So like me, you may have pressed 'Add module' and dropped the code in another workbook and worksheet.
If this is your issue, nothing mention above will work. Move your VBA module and code to the correct spreadsheet visible through this VBA code editor.
True,
I had the same (in Excel 2010) and when I migrated to Excel 2016 , the function prototype was shown, but when I completed the function, the #NAME error was shown with a pop-up... so the code was never triggered.
It turned out I had a Macro of the same name as a Sub or UDF function !
I renamed the Macro, and then it worked
Cheers
Another cause I found for the #NAME? error is that the macro workbook with the custom function has a range name the same as the function name. I changed the function name and solved the problem.
This solution applies to users with an Excel installed in another language than "United States English":
I had a similar problem when making a copy of the active workbook to duplicate it and immediately opened the copy afterwards:
Non-working code:
ThisWorkbook.SaveCopyAs NewFileName
Set wb = Workbooks.Open(FileName:=NewFileName)
This always showed me several cells with Error 2029 / "#NAME?". If I opened the Workbook "the official way" via the File-Menu it worked as expected.
I solved the issue by adding the parameter "local:=true" to the open statement:
Working code:
ThisWorkbook.SaveCopyAs NewFileName
Set wb = Workbooks.Open(FileName:=NewFileName, Local:=True)
as VBA expected english function names in my German workbook. With this parameter VBA is told directly to use the local names.
I hope that helps someone not to loose several hours, as I did...
Short answer - if the function was working before, RESTART YOUR COMPUTER.
Long answer - I had this same thing happen to me. The problem is that the function I had created had been working for months. Then one day it just started showing a #NAME error instead of working like it was before. I had tried closing all other excel workbooks and even closing excel all-together and re-opening the sheet. Nothing seemed to work. Then for kicks, I edited the code to where I knew VBA would complain that there is a compile error. Surprisingly, it didn't complain. OK... I saved and closed excel anyways and then restarted my computer.
Once rebooted, I re-opened the excel workbook. Then VBA finally gave me a compile error. So I changed my function back to the original code I had before and now the sheet is running the function like it is supposed to. No more #NAME error.
Not sure all of those steps are necessary, but simply restarting the computer seems to have fixed my issue.

Resources