Excel 2010:隐藏列组的macros

VBA不是我特别的力量,但是我们在这里:

一旦一组列被隐藏或显示,我想触发一个macros。 我怎样才能存档这个?


我以前的研究成果

关于这个我唯一的好消息就是MSDN上的讨论。 在这里,一个解决scheme是使用以下方式起草的:

从xlsx文件的根目录创build一个带有内容的customUI\customUI.xml文件

 <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" > <commands > <command idMso="ColumnsHide" onAction="ColumnHide_onAction"/> <command idMso="ColumnsUnhide" onAction="ColumnUnhide_onAction"/> </commands > </customUI > 

并添加

 <Relationship Id="edTAB" Type="http://schemas.microsoft.com/office/2006/relationships/ui/extensibility" Target="customUI/customUI.xml" /> 

_rels\_rels.xml 。 (所有这些使用Visual Studio都可能更容易一些,但是我没有办法在微软的世界中使用这样复杂的工具…)现在,可以通过以下方式使用macros:

 Public Sub ColumnHide_onAction(control As IRibbonControl, ByRef cancelDefault) ' ' Code for onAction callback. Ribbon control command ' MsgBox "Ribbon Column Hide" cancelDefault = False End Sub Public Sub ColumnUnhide_onAction(control As IRibbonControl, ByRef cancelDefault) ' ' Code for onAction callback. Ribbon control command ' MsgBox "Ribbon Column Unhide" cancelDefault = False End Sub 

这种方法完美地捕捉到隐藏和取消隐藏栏目,而不是隐藏和取消隐藏群组。 所以,closures,但不是那里。

从这里下载可能的idMso值,我注意到了GroupViewShowHide控件。 ColumnsHide使用与ColumnsHideColumnsUnhide相同的方式不会将所需的结果存档。

有任何想法吗?

为了隐藏一组列,我注意到你没有在你的代码示例中使用.Hidden属性。 这可能是非常有用的,尤其是在数组中定义一组列时。 例如:
For byti = 0 To UBound(cols())
If Hide Then
ActiveSheet.columns(cols(byti)).EntireColumn.Hidden = True
For byti = 0 To UBound(cols())
If Hide Then
ActiveSheet.columns(cols(byti)).EntireColumn.Hidden = True
…等等。


检查一组列是否已经被隐藏,你也可以使用一个数组。 将您的列分配给一个数组,将您的列分组,然后将您的工作表的当前状态(哪些列是隐藏的,哪些是不隐藏的)与该数组进行比较。

下面的代码是一个启动的build议,你可以适应你的项目。 它不需要您在问题中提到的customUI.xml文件。

关键部分是用于检查列是否隐藏的MyColumnCheck变体以及.Match方法。 这是Match电子表格function的VBA等价物。

使用这段代码教会了我很多关于如何在数组中search以及使用Match与其他方法(比如Find和仅循环数组)的起伏! 这已经在几个post中讨论过了,请看这个例子。 我select了Match而不是For...Next循环,尽pipe可以很容易地包含一个For..Next循环来检查一个隐藏的列是否在你指定的组中。

如果你想知道IfError声明:
Application.IfError(Application.Match(MyColumnCheck.Column, MyColumnArray, 0),... …这是因为在VBA代码中使用Match通常有点棘手,正如在这里提到的那样,而@Lori_m写在这里 ,“使用应用程序没有.WorksheetFunction返回一个variables,允许在参数和结果中的数组。

另外,我select将组数组的值更改为-1,因此当过程完成时,一个简单的math运算将显示数组引用的所有列是否隐藏。 负数是更好的这个检查,因为我假设你会参考一个只有正数的实际列。


因此,总结一下, .Match可以有效地用来检查工作表上的隐藏列是否与数组定义的一组列相匹配。

 'the column group you're looking for, dim as a dynamic array of column numbers Dim MyColumnArray(1) As Long 'MyColumnArray(0) is column 2 or "B", MyColumnArray(1) is column 3 or "C", etc MyColumnArray(0) = 2 MyColumnArray(1) = 3 Dim MyColumnCheck As Variant 'column check For Each MyColumnCheck In Worksheets("Sheet1").columns 'if the column is hidden and exists in MyColumnArray array... If columns(MyColumnCheck.Column).EntireColumn.Hidden = True And _ Application.IfError(Application.Match(MyColumnCheck.Column, MyColumnArray, 0), 0) > 0 _ Then MyColumnArray(Application.Match(MyColumnCheck.Column, MyColumnArray, 0) - 1) = -1 '... set that element of the MyColumnArray array to -1. End If Next If WorksheetFunction.Sum(MyColumnArray) = 0 - (UBound(MyColumnArray) + 1) Then Debug.Print "group MyColumnArray is hidden" 'execute code here for when the group is hidden Else Debug.Print "group MyColumnArray is visible" 'execute code here for when the group is visible End If