早上好。我有一个带有两个工作表的Microsoft Excel宏启用工作簿:让我们说Sheet1和Sheet2。在Sheet2中,我有一个组合框(表单控件),可用作表格分类器。该表也将在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),都会发生这种情况。 “Drop Down 4”是正确的名称。我在每个工作表中多次提醒过该名称,索引和名称都是正确的。
所以我希望你们能帮助我。感谢您的耐心等待,如果我的英语不够清楚,请对不起。随意提问。
答案 0 :(得分:2)
我自己发现,对组合名称进行硬编码(在本例中为“Drop Down 4”)是一种可怕的方法,因为Excel每次添加Sheet2的副本时都会分配新名称。尽管Excel执行此操作,但组合名称始终以单词“Drop”(从Drop Down开始)开头。 我修改了一些代码并使其工作:
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
答案 1 :(得分:1)
您的代码在哪里?
这对我有用(在一般模块中)......
Sub DoSorting()
Dim dd, val
Set dd = ActiveSheet.Shapes(Application.Caller)
val = dd.ControlFormat.List(dd.ControlFormat.ListIndex)
MsgBox val
End Sub
复制表单并没有破坏它。