Excel VBA + 用户表单复选框 :: Hide/Unhide 列使用表单复选框

Excel VBA + Userform Checkboxes :: Hide/Unhide columns using Form Checkboxes

我希望有人能够帮助我解决我遇到的问题。首先让我说我不是最好的VBA,我是新手。

问题

我正在尝试创建一个带有复选框(13 个复选框)的简单用户表单,这将允许用户 hide/unhide 他们选择的列。我有 70 列从 S 到 BR。我知道,这听起来可能很多,但这是必需的。我有 13 个周期 每个 周期有 4 列 I.E. 第 1 期有列 **S:V.**

我无法做到的是,将用户表单上的复选框设置为 hide/unhide 用户选择的时间段。我试过的代码。我遵循了许多教程,但始终无法正常工作。我不确定这是否有意义,但我已经添加了我希望它能正常工作的所有代码。

我还添加了专栏的图像,以便您可以更好地理解...我希望

我希望有人能在这件事上帮助我,因为这对我来说很重要。非常感谢您。

IMAGE HERE (BEST OPEN IN NEW TAB) These columns go all the way up to PERIOD 13 or WEEK 52

  Sub hideCol(C As Integer)
    If Controls("CheckBox" & C ) = True Then
        Columns(C).Hidden = True
    Else
        Columns(C).Hidden = False
    End If
    ActiveWindow.ScrollColumn = 1
End Sub 

 Private Sub CheckBox1_Clickl()
    Dim C As Integer
    C = (19, 20, 21, 22)
    Call hideCol(C)
End Sub

 Private Sub CheckBox2_Click()
    Dim C As Integer
    C = (23, 24, 25, 26)
    Call hideCol(C)
End Sub

 Private Sub CheckBox3_Click()
    Dim C As Integer
    C = (27, 28, 29, 30)
    Call hideCol(C)
End Sub

Private Sub CheckBox4_Click()
    Dim C As Integer
    C = (31, 32, 33, 34)
    Call hideCol(C)
End Sub

Private Sub CheckBox5_Click()
    Dim C As Integer
    C = (35, 36, 37, 38)
    Call hideCol(C)
End Sub

Private Sub CheckBox6_Click()
    Dim C As Integer
    C = (39, 40, 41, 42)
    Call hideCol(C)
End Sub

Private Sub CheckBox7_Click()
    Dim C As Integer
    C = (43, 44, 45, 46)
    Call hideCol(C)
End Sub

Private Sub CheckBox8_Click()
    Dim C As Integer
    C = (47, 48, 49, 50)
    Call hideCol(C)
End Sub

Private Sub CheckBox9_Click()
    Dim C As Integer
    C = (51, 52, 53, 54)
    Call hideCol(C)
End Sub

 Private Sub CheckBox10_Click()
    Dim C As Integer
    C = (55, 56, 57, 58)
    Call hideCol(C)
End Sub

 Private Sub CheckBox11_Click()
    Dim C As Integer
    C = (59, 60, 61, 62)
    Call hideCol(C)
End Sub

 Private Sub CheckBox12_Click()
    Dim C As Integer
    C = (63, 64, 65, 66)
    Call hideCol(C)
End Sub

 Private Sub CheckBox13_Click()
    Dim C As Integer
    C = (67, 68, 69, 70)
    Call hideCol(C)
End Sub

 Private Sub UserForm_Initialize ()
    Dim i As Integer
    For i = 19 to 70
        Controls("CheckBox" & i).caption = Cells(5, i)
        If Columns(i).Hidden = True Then
            Controls("CheckBox" & i).Value = True
        End If
        Next i
    End Sub 
 'This is supposed to get the name of the column I.e Period 1, period 2, period 3 and title it in the checkboxes accordingly. Not working though :( :( :( :( 

添加

每当我尝试 运行 这个。我得到“运行-Time Error '-2147024808 (80070057)': 找不到指定的对象。


再次感谢您提供的帮助。几天来我一直试图让它工作,但无法弄清楚。如果所有代码都完全错误,我也不会感到惊讶:/.

我可以通过更改代码来实现您的目标。

我没有将列设置为变量 C 然后调用子例程来隐藏设置为 C 的列,而是将 CheckBox_click 事件写入 hide/unhide 列。

例如,我已将此代码分配给 UserForm1 上的 CheckBox1

Private Sub CheckBox1_Click()
    If Me.CheckBox1.Value = True Then
        Sheets(1).Columns("D:G").Hidden = True
    ElseIf Me.CheckBox1.Value = False Then
        Sheets(1).Columns("D:G").Hidden = False
    End If
End Sub

基本上就是说,每次点击UserForm1上的CheckBox1,判断复选框的值现在是True还是False。如果 True,隐藏列 D、E、F 和 G,否则如果 False,取消隐藏列。

如果这是您要查找的内容,只需将代码添加到每个 CheckBox_click 事件并更改您的控件名称以适合,例如CheckBox2CheckBox3 等)。

记住您在用户表单中的选择

当工作簿关闭或 UserForm 从内存中卸载时,您的选择是 'forgotten'。 但是CheckBox 被点击时,你的列只有 hide/unhide。这意味着当随后打开工作簿时 and/or 加载用户窗体,所有复选框将在初始化时显示它们的值(默认情况下为 False)。

Note: When the userform is unloaded from memory, all variables are forgotten so we can't use VBA to remember our selections for next time.

因此,我们可以 'remember' 选择的一种方法有以下 3 个步骤:

  1. 在我们的 Spreadsheet 上放置一个 ActiveX CheckBox(我们将使用 sheet1 作为示例)。
  2. CheckBox_click 代码中,在我们的代码中添加一个新行,以将匹配值从我们的用户表单复选框分配给我们的工作sheet CheckBox.
  3. UserForm_Initialization代码中,将我们的WorksheetCheckBox的值设置为我们的UserFormCheckBox.

    步骤 1

    在您的 WorkBook 中,选择相关 sheet 转到 'Developer Tab',单击 'Insert' 并选择 CheckBox。

然后 'draw' 将 CheckBox 放到你的 sheet 上,无论你想把它放在哪里(就像你在 VBE 中为你 UserForm.

第 2 步

将以下代码行添加到 CheckBox_click 代码中:

Private Sub CheckBox1_Click()
    If Me.CheckBox1.Value = True Then
        Sheets(1).Columns("D:G").Hidden = True
        Sheet1.CheckBox1.Value = True '<~~ HERE
    ElseIf Me.CheckBox1.Value = False Then
        Sheets(1).Columns("D:G").Hidden = False
        Sheet1.CheckBox1.Value = False '<~~ AND HERE
    End If
End Sub

步骤 3

在我们的 Userform1 代码中,输入以下内容:

Private Sub UserForm_Initialize()
    Me.CheckBox1.Value = Sheet1.CheckBox1.Value
End Sub