2011-09-28 30 views
0

早上好。我有两个工作表的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”是正确的名称。我已在每个工作表中多次提醒该名称,索引和名称都是正确的。

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

回答

1

您的代码位于何处?

这为我工作(在通用模块)...

Sub DoSorting() 
Dim dd, val 

    Set dd = ActiveSheet.Shapes(Application.Caller) 
    val = dd.ControlFormat.List(dd.ControlFormat.ListIndex) 

    MsgBox val 

End Sub 

复制片没有打破它。

2

我想通过自己编写组合名称(在本例中为“Drop Down 4”)是一种可怕的方法,因为每次添加Sheet2的副本时,Excel都会分配新名称。虽然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