2013-06-27 54 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