VBA:隐藏单元格更改列

时间:2018-07-29 00:32:41

标签: excel vba

我是vba的新手,我想在发生单元格更改时在工作簿中隐藏一组列。我从Google的研究中整理了一份代码清单,但没有用。请检查我的代码:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Keycells As Range
    Set Keycells = Worksheet("Sheet1").Range("B4")

    If Not Application.Intersect(Keycells, Range(Target.Address)) Is Nothing Then
        Call HideColumn
    End If

End Sub
Sub HideColumn()
    If Worksheet("Sheet1").Range("B4").Value = 0 Then
        Worksheet("Sheet3").Columns("V:Z").EntireColumn.Hidden = True
        Worksheet("Sheet4").Columns("V:Z").EntireColumn.Hidden = True
    Else
        Worksheet("Sheet3").Columns("V:Z").EntireColumn.Hidden = False
        Worksheet("Sheet4").Columns("V:Z").EntireColumn.Hidden = False
    End If
End Sub

2 个答案:

答案 0 :(得分:0)

我对此进行了测试,并且效果很好。我换了一些东西,看起来有点干净。

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Keycells As Range
    Dim ws2 As Worksheet

    'sets a reference to Sheet2
    Set ws2 = Excel.Application.ThisWorkbook.Sheets("Sheet2")

    'I redefined Keycells to be a bit easier to read and use
    Set Keycells = ws2.Cells(4, 2)

    'The previous trigger you had wasn't entirely intuitive to me, so I remade it. 
    'The IsEmpty() function is a built-in function you can call to 
    'determine if a cell is empty.
    If Not IsEmpty(Application.Intersect(Keycells, Target)) Then
        Call HideColumn
    End If

End Sub

Sub HideColumn()
    Dim ws1 As Worksheet
    Dim ws3 As Worksheet
    Dim ws4 As Worksheet

    'sets a reference to Sheet1
    Set ws1 = Excel.Application.ThisWorkbook.Sheets("Sheet1")
    'sets a reference to Sheet3
    Set ws3 = Excel.Application.ThisWorkbook.Sheets("Sheet3")
    'sets a reference to Sheet4
    Set ws4 = Excel.Application.ThisWorkbook.Sheets("Sheet4")

    'I cleaned-up your previous statement to be a bit easier to read
    If ws1.Cells(4, 2).Value = 0 Then
        'hides columns V through Z on Sheet3
        ws3.Columns("V:Z").Hidden = True
        'hides columns V through Z on Sheet4
        ws4.Columns("V:Z").Hidden = True
    Else
        ws3.Columns("V:Z").Hidden = False
        ws4.Columns("V:Z").Hidden = False
    End If
End Sub

要回答有关将Sub放置在何处的问题,可以将HideColumn Sub放置在所需的任何模块或工作表中,假设您已创建具有Public前缀的Sub,如下所示:

Public Sub MySub()
    'do the thing
End Sub

但是,如果在Module1中具有MySub()和Private前缀,则不能从Module2调用它。您可以这样使用Private前缀:

Private Sub MySub()
    'i did my thing
End Sub

我认为Private Worksheet_Change()子项必须位于Sheet2中。在项目资源管理器中,右键单击Sheet2->查看代码->在其中输入Worksheet_Change子项。

答案 1 :(得分:0)

这有效

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    'MsgBox ("inChange")
    If Not Intersect(Target, Range("B4")) Is Nothing Then
        Call HideColumn(Target)
    End If
End Sub

Sub HideColumn(Target As Range)
    'MsgBox ("inHide")
    Dim wb As Workbook
    Set wb = Excel.Application.ThisWorkbook
    If wb.Sheets("Sheet1").Range("B4").Value = 0 Then
        wb.Sheets("Sheet3").Columns("V:Z").EntireColumn.Hidden = True
        wb.Sheets("Sheet4").Columns("V:Z").EntireColumn.Hidden = True
    Else
        wb.Sheets("Sheet3").Columns("V:Z").EntireColumn.Hidden = False
        wb.Sheets("Sheet4").Columns("V:Z").EntireColumn.Hidden = False
    End If
End Sub