宏在重复的工作表中不起作用
早上好。我有一个 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”是正确的名字。我已在每个工作表中多次提醒该名称,索引和名称都是正确的。
所以我希望你们能帮助我。感谢您的耐心等待,如果我的英语不够清晰,我们深表歉意。欢迎提问。
Good morning. I have a Microsoft Excel Macro Enabled Workbook with two worksheets: let's say Sheet1 and Sheet2. In Sheet2 I have a combo box (form control) that works as a table sorter. This table will be also in Sheet2. The combo uses the following code:
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
This code works like charm in this worksheet.
I'm using Aspose Cells for Java to use this Excel Workbook as a template to generate new workbooks with multiple datasheets based in Sheet2 copies (which contains my combo) but the problem is that when I do this, the combo doesn't work anymore as it did in the template.
In this line:
comboValue = ActiveSheet.Shapes("Drop Down 4").ControlFormat.List(ActiveSheet.Shapes("Drop Down 4").ControlFormat.ListIndex)
I get this error:
Run-time error '438' Object doesn't support this property or method
It looks like ControlFormat is not being recognized as a valid method for the combo shape.
This happens whether you use the combo name or the combo index (in this case is always 6).
"Drop Down 4" is the right name. I have alerted the name several times in each worksheet and both the index and the name are right.
So I hope you guys can help me out. Thank you for your patience and sorry if my English is not clear enough. Feel free to ask questions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我自己发现,硬编码组合名称(在本例中为“Drop Down 4”)是一种糟糕的方法,因为每次添加 Sheet2 的副本时 Excel 都会分配新名称。尽管 Excel 是这样做的,但组合名称始终以单词“Drop”(来自 Drop Down)开头。
我修改了一些代码并使其工作:
I figured it out by myself that hard coding the combo name (in this case "Drop Down 4") is a terrible way to do this since Excel assigns new names every time a copy of Sheet2 is added. Although Excel does this, combo names always starts with the word "Drop" (from Drop Down).
I modified a little bit the code and made it work:
您的代码位于哪里?
这对我有用(在通用模块中)...
复制工作表并没有破坏它。
Where is your code located?
This worked for me (in a general module)...
Copying the sheet did not break it.