2011-09-28 137 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 
相關問題