excel 如何自动隐藏其他工作表中的列

p5cysglq  于 2023-06-25  发布在  其他
关注(0)|答案(3)|浏览(167)

我想写一个VBA函数,这样当一个列在“主”工作表是隐藏的,同一列(相同的名称)在所有其他工作表也隐藏。下面是我的“主”表代码:

Private Sub Worksheet_Change(ByVal Target As Range)    
    Dim i As Integer, ws As Worksheet

    If IsEntireColumn(Target) = True Then    
        If Target.Hidden = True Then

            For i = 1 To Target.Columns.Count
                For Each ws In ThisWorkbook.Worksheets
                    If ws.Name = "Master" Or ws.Name = "Affiliate Codes" Then
                    Else
                        ws.Cells(1, ColumnIndexReturn(ws.Name, Target.Cells(1, i), 3)).EntireColumn.Hidden = True
                    End If
                Next ws
            Next i 

        End If    
    End If    

End Sub

对于上下文,IsEntireColumn是一个布尔函数,如果所选范围确实是整个列,则返回True,而ColumnIndexReturn是另一个函数,它根据列的名称返回列索引。
由于这是一个Worksheet_Change子表,我希望每次隐藏一列时,其他工作表中相应的列也会自动隐藏。然而,到目前为止,这还没有发生,其他工作表中的列并没有自动隐藏自己。
我能知道这里有什么问题吗?如果需要任何进一步的信息,请随时告诉我。任何帮助将不胜感激!感谢您的评分

lsmd5eda

lsmd5eda1#

你的要求有点棘手。
你的代码没有做任何事情的主要原因是:Change-事件仅在单元格内容发生更改时触发。显示/隐藏单元格(或任何其他格式)不会触发更改事件。
一个更好的事件是Selection_Change-事件,每当选择一个单元格时,它就会被触发。然而,这也不起作用:当您选择一个列时会触发此事件,当您右键单击(以选择“隐藏”命令)时会再次触发此事件,但在隐藏该列后不会触发此事件。
我想到的最好的办法是在Selection_Change事件中“记住”一个选定的列。当Selection_Change被调用 * 下一次 * 时,对以前记住的列执行显示/隐藏逻辑-用户迟早会选择另一个单元格。只有一个例外:当用户在隐藏列后立即选择其他工作表时。为了解决这个问题,我们可以使用Worksheet_Deactivate-event。
我得到了以下代码:

Option Explicit
Dim lastSelectedColumn As Range

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not lastSelectedColumn Is Nothing Then
        ShowHideColumns lastSelectedColumn
    End If
    
    If IsEntireColumn(Target) Then
        Set lastSelectedColumn = Target
    Else
        Set lastSelectedColumn = Nothing
    End If
End Sub

Private Sub Worksheet_Deactivate()
    If Not lastSelectedColumn Is Nothing Then
        ShowHideColumns lastSelectedColumn
        Set lastSelectedColumn = Nothing
    End If
End Sub

Sub ShowHideColumns(Target As Range)
    Dim col As Long, ws As Worksheet
    For col = 1 To Target.Columns.Count
        For Each ws In ThisWorkbook.Worksheets
            If ws.Name <> "Master" And ws.Name <> "Affiliate Codes" And ws.Name <> Target.Parent.Name Then
                Dim cell As Range
                Set cell = Target.Cells(1, col)
                ws.Cells(1, cell.Column).EntireColumn.Hidden = cell.EntireColumn.Hidden
            End If
        Next ws
    Next col
End Sub

Function IsEntireColumn(Target As Range) As Boolean
    IsEntireColumn = (Target.Rows.Count = Target.Parent.Rows.Count)
End Function

请注意,代码不仅 * 隐藏 * 列,还将再次 * 显示 * 它们。

gab6jxml

gab6jxml2#

@FunThomas,你快了24分钟
将此代码放在模块“ThisWorkbook”中

Option Explicit

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    Dim i As Integer, c As Integer, ws As Worksheet
    For i = 1 To Worksheets("Master").UsedRange.Columns.Count
        For Each ws In ThisWorkbook.Worksheets
            If ws.Name <> "Master" And ws.Name <> "Affiliate Codes" Then
               c = ColumnIndexReturn(ws.Name, Worksheets("Master").Cells(1, i))
               If c <> 0 Then
                  ws.Cells(1, c).EntireColumn.Hidden = Worksheets("Master").Columns(i).EntireColumn.Hidden
               End If
            End If
        Next
    Next
End Sub
Private Function ColumnIndexReturn(sSheetName As String, sColumnName As String) As Integer
    Dim i As Integer
    For i = 1 To Worksheets(sSheetName).UsedRange.Columns.Count
        If Worksheets(sSheetName).Cells(1, i).Value = sColumnName Then
           ColumnIndexReturn = i
           Exit Function
        End If
    Next
End Function

此代码仅在用户更改工作表时运行,而不是在每个单元格更改时运行,并且当列在Master中更改为可见时,它再次显示列

5n0oy7gb

5n0oy7gb3#

下面是hennep代码的一个更通用的版本。

Sub SyncMasterColumnVisibility(Optional TargetWorksheet As Worksheet)
    Application.ScreenUpdating = False
    Const MasterSheetName As String = "Master"
    Dim Excludes As Variant
    Excludes = Array("Master", "Affiliate Codes")
    
    Dim TargetWorksheets As New Collection
    Dim ws As Worksheet

    If Not TargetWorksheet Is Nothing Then
        TargetWorksheets.Add TargetWorksheet
    Else
        For Each ws In ThisWorkbook.Worksheets
            On Error Resume Next ' In case Match function does not find a match
            If IsError(Application.WorksheetFunction.Match(ws.Name, Excludes, 0)) Then
                TargetWorksheets.Add ws
            End If
            On Error GoTo 0 ' Reset error handling
        Next ws
    End If
    
    Dim MasterSheetColumnVisibility As New Collection
    Dim Cell As Range
    
    For Each Cell In ThisWorkbook.Worksheets(MasterSheetName).UsedRange.Rows(1).Cells
        If Len(Cell.Value) > 0 Then
            On Error Resume Next
            MasterSheetColumnVisibility.Add Cell.EntireColumn.Hidden, CStr(Cell.Value)
            On Error GoTo 0
        End If
    Next
    
    Dim Hidden As Boolean
    Dim tWs As Variant ' Use variant type because the collection may contain different object types
    For Each tWs In TargetWorksheets
        For Each Cell In tWs.UsedRange.Rows(1).Cells
            On Error Resume Next ' In case Match function does not find a match
            Hidden = MasterSheetColumnVisibility(CStr(Cell.Value))
            If Err.Number = 0 Then Cell.EntireColumn.Hidden = Hidden
            On Error GoTo 0 ' Reset error handling
        Next
    Next tWs
End Sub

使用Workbook_SheetActivate事件:

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    SyncMasterColumnVisibility Sh
End Sub

使用Workbook_SheetDeactivate事件:

Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    Const MasterSheetName As String = "Master"
    If Sh.Name = MasterSheetName Then SyncMasterColumnVisibility
End Sub

相关问题