2013-06-27 52 views
2

VBA是不是我的独特优势,但在这里我们去:Excel 2010中:宏隐藏列组

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


我以前的研究结果

唯一的好提示关于这个我能找到在MSDN this讨论。在这里,一个解决方案是使用下面的方式起草单位:

从XLSX文件的根目录创建一个文件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,但我没有获得这种先进的工具在微软的世界......)现在,宏可以采用以下方式:

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 

这种做法完全捕获隐藏和取消隐藏栏目,但不隐藏和取消隐藏群组。所以,关闭,但不是那里。

here下载可能的idMso值,我得到了GroupViewShowHide控件的通知。不过,使用与ColumnsHideColumnsUnhide相同的方式不会归档所需的结果。

任何想法?

+0

if(cell.outlinelevel> 0)then cell.entirerow.showdetail = true/false –

回答

0

对于隐藏组的列,我注意到你没有在你的代码示例中使用.Hidden属性。它可能非常有用,特别是如果您在数组中定义一组列。例如:
For byti = 0 To UBound(cols())
If Hide Then
ActiveSheet.columns(cols(byti)).EntireColumn.Hidden = True
...等等。


检查如果一组列已经隐藏或没有,你也可以使用一个数组。通过将列分配给一个数组来对列进行分组,然后将您的工作表的当前状态(哪些列隐藏,哪些不显示)与该数组进行比较。

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

关键部分是MyColumnCheck变体,它用于检查列是否隐藏,以及.Match方法。这是与Match电子表格功能相同的VBA。

对这段代码的工作教会了我很多关于如何在数组中搜索以及使用Match与使用其他方法(比如Find)并仅循环访问数组的起伏!已经在几个帖子中讨论过这个问题,例如this one就是一个很好的例子。我选择做Match而不是For...Next循环,尽管可以很容易地包含一个For..Next循环,用于检查隐藏列是否在您分配的组中。

如果你想知道关于IfError声明:
Application.IfError(Application.Match(MyColumnCheck.Column, MyColumnArray, 0),... ......这是因为使用Match在VBA代码往往是有点棘手提到here。另外,正如@Lori_m写道here,“使用无.WorksheetFunction的应用程序返回一个允许参数和结果中的数组的变体”。

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


因此,综上所述,.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