ActiveX组件不能创build对象 – Mac的Excel

我试图获得一个包含macros的Excel 2011 32位(for Mac)电子表格。 问题是,这个macros在PC上正常工作,但不是在Mac上。 我试图导入蒂姆·霍尔的Dictionary.cls,但它仍然无法正常工作。 对于KeyValuePair.cls同样的事情。

错误:运行时错误“429”ActiveX组件无法创build对象

我不是程序员,所以问题可能是我,不知道要改变什么才能使事情正常工作。 那些知道自己在做什么的人可能是非常容易的。 任何人都可以花几分钟时间查看这些文件,并告诉我需要更改哪些部分才能运行? [我认为它确实有效…]

FWIW,我试图用“New.Dictionary”replace“Scripting.Dictionary”在两个地方(见下文),但是没有得到它的工作。

Set dAttributes = CreateObject("New.Dictionary") Set dValues = CreateObject("New.Dictionary”) 

RandomiseData文件:

 Option Explicit Sub GenerateResults() Dim LO As ListObject Dim LO2 As ListObject Dim LR As ListRow Dim ws As Worksheet Dim cCount As Integer Dim gCount As Integer Dim dAttributes As Object Dim dValues As Object Dim dKey As Variant Dim c As Range Dim v As Variant Dim i As Integer Dim InsertCount As Integer Set LO = ActiveSheet.ListObjects("Data") If LO Is Nothing Then MsgBox "Please select the table and re-run": Exit Sub With Application .EnableEvents = False .DisplayAlerts = False .ScreenUpdating = False End With LO.AutoFilter.ShowAllData Set ws = ActiveWorkbook.Sheets.Add ws.Range("A1:C1").Value = Array("Candidate", "Attribute", "Value") ws.ListObjects.Add xlSrcRange, Range("A1:C1"), , xlYes Set LO2 = ws.Range("A1").ListObject Set dAttributes = CreateObject(“New.Dictionary") For Each c In LO.ListColumns("Attribute").DataBodyRange.Cells If Not dAttributes.Exists(c.Value) Then dAttributes(c.Value) = c.Value Next c For Each dKey In dAttributes.Keys LO.Range.AutoFilter Field:=LO.ListColumns("Attribute").Index, Criteria1:=dKey gCount = Evaluate("SUM(--(FREQUENCY(IF(" & LO.Name & "[Attribute]=""" & dKey & """,MATCH(" & LO.Name & "[Value]," & LO.Name & "[Value],0)),ROW(" & LO.Name & "[Value])-ROW(" & LO.Name & "[[#Headers],[Value]]))>0))") cCount = Evaluate("SUM(--(FREQUENCY(IF(" & LO.Name & "[Attribute]=""" & dKey & """,MATCH(" & LO.Name & "[Candidate]," & LO.Name & "[Candidate],0)),ROW(" & LO.Name & "[Candidate])-ROW(" & LO.Name & "[[#Headers],[Candidate]]))>0))") v = GenerateSplit(cCount, gCount) Set dValues = CreateObject(“New.Dictionary") For Each c In LO.ListColumns("Value").DataBodyRange.SpecialCells(xlCellTypeVisible) If Not dValues.Exists(c.Value) Then dValues(c.Value) = c.Value Next c InsertCount = 0 i = 1 For Each c In LO.ListColumns("Candidate").DataBodyRange.SpecialCells(xlCellTypeVisible) TryAgain: If i <= v(InsertCount, 2) Then Set LR = LO2.ListRows.Add LR.Range.Value = Array(c.Value, dKey, dValues.Items()(InsertCount)) i = i + 1 Else i = 1 InsertCount = InsertCount + 1 GoTo TryAgain End If Next c Next dKey LO.AutoFilter.ShowAllData LO.Range.Worksheet.Select With Application .EnableEvents = True .DisplayAlerts = True .ScreenUpdating = True End With End Sub 

编辑代码

 Option Explicit Sub GenerateResults() Dim LO As ListObject Dim LO2 As ListObject Dim LR As ListRow Dim ws As Worksheet Dim cCount As Integer Dim gCount As Integer Dim dAttributes As Object Dim dValues As Object Dim dKey As Variant Dim c As Range Dim v As Variant Dim i As Integer Dim InsertCount As Integer #If Mac Then Set dAttributes = New Dictionary Set dValues = New Dictionary #Else Set dAttributes = CreateObject("Scripting.Dictionary") Set dValues = CreateObject("Scripting.Dictionary") #End If Set LO = ActiveSheet.ListObjects("Data") If LO Is Nothing Then MsgBox "Please select the table and re-run": Exit Sub With Application .EnableEvents = False .DisplayAlerts = False .ScreenUpdating = False End With LO.AutoFilter.ShowAllData Set ws = ActiveWorkbook.Sheets.Add ws.Range("A1:C1").value = Array("Candidate", "Attribute", "Value") ws.ListObjects.Add xlSrcRange, Range("A1:C1"), , xlYes Set LO2 = ws.Range("A1").ListObject ' Set dAttributes = CreateObject("New Dictionary") For Each c In LO.ListColumns("Attribute").DataBodyRange.Cells If Not dAttributes.Exists(c.value) Then dAttributes(c.value) = c.value Next c For Each dKey In dAttributes.Keys LO.Range.AutoFilter Field:=LO.ListColumns("Attribute").Index, Criteria1:=dKey gCount = Evaluate("SUM(--(FREQUENCY(IF(" & LO.Name & "[Attribute]=""" & dKey & """,MATCH(" & LO.Name & "[Value]," & LO.Name & "[Value],0)),ROW(" & LO.Name & "[Value])-ROW(" & LO.Name & "[[#Headers],[Value]]))>0))") cCount = Evaluate("SUM(--(FREQUENCY(IF(" & LO.Name & "[Attribute]=""" & dKey & """,MATCH(" & LO.Name & "[Candidate]," & LO.Name & "[Candidate],0)),ROW(" & LO.Name & "[Candidate])-ROW(" & LO.Name & "[[#Headers],[Candidate]]))>0))") v = GenerateSplit(cCount, gCount) ' Set dValues = CreateObject("Scripting.Dictionary") For Each c In LO.ListColumns("Value").DataBodyRange.SpecialCells(xlCellTypeVisible) If Not dValues.Exists(c.value) Then dValues(c.value) = c.value Next c InsertCount = 0 i = 1 For Each c In LO.ListColumns("Candidate").DataBodyRange.SpecialCells(xlCellTypeVisible) TryAgain: If i <= v(InsertCount, 2) Then Set LR = LO2.ListRows.Add LR.Range.value = Array(c.value, dKey, dValues.Items()(InsertCount)) i = i + 1 Else i = 1 InsertCount = InsertCount + 1 GoTo TryAgain End If Next c Next dKey LO.AutoFilter.ShowAllData LO.Range.Worksheet.Select With Application .EnableEvents = True .DisplayAlerts = True .ScreenUpdating = True End With End Sub 

New.Dictionary不是一个有效的类名,也会在PC上失败。 通常使用早期绑定的构造是:

 Set obj = New Dictionary 

或使用后期绑定:

 Set obj = CreateObject("Scripting.Dictionary") 

但是, Mac OS没有脚本运行时库 ,所以这些东西都不可用 – Dictionary,FileSystemObject等等。

您需要使用集合或其他数据types来代替字典types,或者您可以借用这个其他答案并实现自定义字典类 。

我试图导入蒂姆·霍尔的Dictionary.cls,但它仍然无法正常工作。 对于KeyValuePair.cls同样的事情。

我怀疑你根本不知道你需要使用条件编译方法来指定Mac OS上的Dictionary类和Windows OS上的Scripting.Dictionary类。

在Mac / Windows上使用条件编译

删除这两行:

 Set dAttributes = CreateObject("New.Dictionary") Set dValues = CreateObject("New.Dictionary") 

正如我在上面所描述的,即使在Windows中,它们也会失败。 同样,如果您想在Win和Mac环境中使用此代码,则无法使用Scripting.Dictionary而无需采取一些额外的预防措施以避免错误。

您将需要使用编译器指令来实现条件编译来识别操作系统。 对于之前做过的任何人来说,这并不太复杂,但大多数初学者甚至不知道如何使用它。

在伪代码中,基本上你是这样做的:

 If the operating system is Mac, then: Do this ElseIf the operating system is Win, then: Do that instead End If 

在你的代码中,做这样的事情

假设您已经将实现Dictionary副本的其他答案中的KeyValuePair.cls Dictionary.cls代码复制到纯文本文件中,并将两个模块都导入到项目的VBE中。

 #IF Mac Then Set dAttributes = New Dictionary Set dValues = New Dictionary #Else Set dAttributes = CreateObject("Scripting.Dictionary") Set dValues = CreateObject("Scripting.Dictionary") #End If 

我会把这个代码放在行的上面:

 Set LO = ActiveSheet.ListObjects("Data") 

实际上,只要您调用dAttributesdValues 之前将该代码放置任何地方,放置它的位置并不重要。

这应该适用于两个操作系统,因为Dictionary.cls模仿了Scripting.Dictionary的方法。

注意:最好将这些对象分配进行分组,而不是在整个过程中随意地对它们进行处理, 尤其是在使用条件编译时,因为它更易读,更易于继续前进。

我看到你说:“我试图导入Tim Hall的Dictionary.cls,但它仍然不起作用,对于KeyValuePair.cls也是如此。

Tim Halls的2016 Dictionary.cls是Scripting.Dictionary的完整替代品,不需要KeyValuePair.cls,它是我在https://sysmod.wordpress.com/2011/11/24/dictionary-vba-class – 更新/为我的2011 Dictionary.cls。 使用他的class级或我的class级,但不是两个。

关于条件编译的build议对于编写在Mac或PC中工作的代码是很好的。 我会build议,如果你有你自己的字典类,你根本不需要Windows Scripting.Dictionary。 我认为最好是有一个阶级在你的控制之下,而不是以一个微妙的方式偏离两个阶级。