简体   繁体   中英

“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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM