macros不能在重复的工作表中工作

早上好。 我有两个工作表的Microsoft Excelmacros启用工作簿:让我们说Sheet1和Sheet2。 在Sheet2中,我有一个combobox(表单控件),可以作为表格分类器。 该表格也将在Sheet2中。 组合使用以下代码:

Option Explicit Sub DropDown4_Change() Dim comboValue As String Dim Key1ColumnIndex As Integer Dim Key2ColumnIndex As Integer 'You can get the name by doing something like this in the immediate window: "? ActiveSheet.Shapes(1).OLEFormat.Object.Name" comboValue = ActiveSheet.Shapes("Drop Down 4").ControlFormat.List(ActiveSheet.Shapes("Drop Down 4").ControlFormat.ListIndex) Select Case comboValue Case "By Keyphrase" Key1ColumnIndex = 18 Key2ColumnIndex = 19 Case "By Region" Key1ColumnIndex = 19 Key2ColumnIndex = 18 Case "Default" Key1ColumnIndex = 1 Key2ColumnIndex = 1 End Select Range("DataValues").sort Key1:=Range("DataValues").Cells(1, Key1ColumnIndex), _ Order1:=xlDescending, Header:=xlNo, DataOption1:=xlSortNormal, _ Key2:=Range("DataValues").Cells(1, Key2ColumnIndex), order2:=xlDescending End Sub 

这个代码在这个工作表中像魅力一样。

我使用Aspose Cells for Java来使用此Excel工作簿作为模板来生成基于Sheet2副本(包含我的组合)的多个数据表的新工作簿,但问题是,当我这样做时,组合不再工作了就像它在模板中所做的一样。

在这一行中:

 comboValue = ActiveSheet.Shapes("Drop Down 4").ControlFormat.List(ActiveSheet.Shapes("Drop Down 4").ControlFormat.ListIndex) 

我得到这个错误:

 Run-time error '438' Object doesn't support this property or method 

它看起来像ControlFormat不被识别为组合形状的有效方法。 无论使用组合名称还是组合索引(在这种情况下始终为6),都会发生这种情况。 “下拉4”是正确的名称。 我已经在每个工作表中多次提醒这个名字,索引和名字都是正确的。

所以我希望你们能帮助我。 感谢您的耐心和歉意,如果我的英语不够清楚。 随意问的问题。

我自己想出了,编写组合名称(在本例中为“Drop Down 4”)是一种可怕的方法,因为每次添加Sheet2的副本时,Excel都会分配新名称。 尽pipeExcel会这样做,但组合名称总是以“Drop”(从下拉菜单)开始。 我修改了一些代码,并使其工作:

 Option Explicit Sub DropDown4_Change() Dim comboValue As String Dim Key1ColumnIndex As Integer Dim Key2ColumnIndex As Integer Dim Index As Integer Dim comboName As String Dim comboName2 As String Dim comboID As Integer 'You can get the name by doing something like this in the immediate window: "? Sheet1.Shapes(1).OLEFormat.Object.Name" For Index = 1 To ActiveSheet.Shapes.Count comboName = ActiveSheet.Shapes(Index).OLEFormat.Object.Name If InStr(comboName, "Drop") > 0 Then 'MsgBox InStr(comboName, "Drop") comboName2 = comboName comboID = Index End If Next comboValue = ActiveSheet.Shapes(comboID).ControlFormat.List(ActiveSheet.Shapes(comboID).ControlFormat.ListIndex) Select Case comboValue Case "By Keyphrase" Key1ColumnIndex = 18 Key2ColumnIndex = 19 Case "By Region" Key1ColumnIndex = 19 Key2ColumnIndex = 18 Case "Default" Key1ColumnIndex = 1 Key2ColumnIndex = 1 End Select Range("DataValues").sort Key1:=Range("DataValues").Cells(1, Key1ColumnIndex), _ Order1:=xlAscending, Header:=xlNo, DataOption1:=xlSortNormal, _ Key2:=Range("DataValues").Cells(1, Key2ColumnIndex), order2:=xlAscending End Sub 

你的代码在哪里?

这对我来说(在一个通用的模块)…

 Sub DoSorting() Dim dd, val Set dd = ActiveSheet.Shapes(Application.Caller) val = dd.ControlFormat.List(dd.ControlFormat.ListIndex) MsgBox val End Sub 

复制工作表并没有破坏它。