使用Checkbox循环

时间:2016-11-03 21:57:16

标签: vba excel-vba checkbox excel

我正在尝试在Excel中生成GUI,根据是否选中该复选框,在单个图表上绘制和取消绘制数据。

vba脚本:

 Private Sub UserForm_Initialize()

    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlXYScatterLinesNoMarkers
    ActiveChart.SeriesCollection.NewSeries

    ActiveChart.SeriesCollection(1).XValues = "=Sheet1!$A$21:$A$23"
    ActiveChart.SeriesCollection(1).Values = "=Sheet1!$B$21:$B$23"
    With ActiveChart.Parent
        .Height = 300 ' resize
        .Width = 600 ' resize
        .Top = 100 ' reposition
        .Left = 100 ' reposition
    End With
End Sub

Private Sub CheckBox1_Click()
    ActiveChart.ChartType = xlXYScatterLinesNoMarkers
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(2).Name = "3bar,pH7,30C"
    ActiveChart.SeriesCollection(2).XValues = "=Sheet1!$D$21:$D$223"
    ActiveChart.SeriesCollection(2).Values = "=Sheet1!$E$21:$E$223"
End Sub

Private Sub CheckBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    ActiveChart.SeriesCollection(2).Delete
End Sub

Private Sub CheckBox2_Click()
    ActiveChart.ChartType = xlXYScatterLinesNoMarkers
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(3).Name = "3bar,pH7,20C"
    ActiveChart.SeriesCollection(3).XValues = "=Sheet1!$G$21:$G$223"
    ActiveChart.SeriesCollection(3).Values = "=Sheet1!$H$21:$H$223"
End Sub

Private Sub CheckBox3_Click()
    ActiveChart.ChartType = xlXYScatterLinesNoMarkers
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(4).Name = "3bar,pH7,10C"
    ActiveChart.SeriesCollection(4).XValues = "=Sheet1!$J$21:$J$223"
    ActiveChart.SeriesCollection(4).Values = "=Sheet1!$K$21:$K$223"
End Sub

Private Sub CheckBox4_Click()
    ActiveChart.ChartType = xlXYScatterLinesNoMarkers
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(5).Name = "3bar,pH5,30C"
    ActiveChart.SeriesCollection(5).XValues = "=Sheet1!$M$21:$M$223"
    ActiveChart.SeriesCollection(5).Values = "=Sheet1!$N$21:$N$223"
End Sub

Private Sub CheckBox5_Click()
    ActiveChart.ChartType = xlXYScatterLinesNoMarkers
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(6).Name = "3bar,pH5,20C"
    ActiveChart.SeriesCollection(6).XValues = "=Sheet1!$P$21:$P$223"
    ActiveChart.SeriesCollection(6).Values = "=Sheet1!$Q$21:$Q$223"
End Sub

Private Sub CheckBox6_Click()
    ActiveChart.ChartType = xlXYScatterLinesNoMarkers
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(7).Name = "3bar,pH5,20C(DI)"
    ActiveChart.SeriesCollection(7).XValues = "=Sheet1!$S$21:$S$223"
    ActiveChart.SeriesCollection(7).Values = "=Sheet1!$T$21:$T$223"
End Sub

Private Sub CheckBox7_Click()
    ActiveChart.ChartType = xlXYScatterLinesNoMarkers
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(8).Name = "3bar,pH5,20C(HNO3)"
    ActiveChart.SeriesCollection(8).XValues = "=Sheet1!$V$21:$V$223"
    ActiveChart.SeriesCollection(8).Values = "=Sheet1!$W$21:$W$223"
End Sub

Private Sub CheckBox8_Click()
    ActiveChart.ChartType = xlXYScatterLinesNoMarkers
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(9).Name = "3bar,pH5,10C"
    ActiveChart.SeriesCollection(9).XValues = "=Sheet1!$Y$21:$Y$223"
    ActiveChart.SeriesCollection(9).Values = "=Sheet1!$Z$21:$Z$223"
End Sub

Private Sub CheckBox9_Click()
    ActiveChart.ChartType = xlXYScatterLinesNoMarkers
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(10).Name = "3bar,pH9,20C"
    ActiveChart.SeriesCollection(10).XValues = "=Sheet1!$AB$21:$AB$223"
    ActiveChart.SeriesCollection(10).Values = "=Sheet1!$AC$21:$AC$223"
End Sub

Private Sub CheckBox10_Click()
    ActiveChart.ChartType = xlXYScatterLinesNoMarkers
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(11).Name = "3bar,pH9,10C"
    ActiveChart.SeriesCollection(11).XValues = "=Sheet1!$AE$21:$AE$223"
    ActiveChart.SeriesCollection(11).Values = "=Sheet1!$AF$21:$AF$223"
End Sub

Private Sub CheckBox11_Click()
    ActiveChart.ChartType = xlXYScatterLinesNoMarkers
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(12).Name = "5bar,pH5,20C"
    ActiveChart.SeriesCollection(12).XValues = "=Sheet1!$AH$21:$AH$223"
    ActiveChart.SeriesCollection(12).Values = "=Sheet1!$AI$21:$AI$223"
End Sub

Private Sub CheckBox12_Click()
    ActiveChart.ChartType = xlXYScatterLinesNoMarkers
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(13).Name = "1bar,pH5,20C"
    ActiveChart.SeriesCollection(13).XValues = "=Sheet1!$AK$21:$AK$223"
    ActiveChart.SeriesCollection(13).Values = "=Sheet1!$AL$21:$AL$223"
End Sub

Private Sub CheckBox13_Click()
    ActiveChart.ChartType = xlXYScatterLinesNoMarkers
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(14).Name = "0bar,pH5,20C"
    ActiveChart.SeriesCollection(14).XValues = "=Sheet1!$AN$21:$AN$223"
    ActiveChart.SeriesCollection(14).Values = "=Sheet1!$AO$21:$AO$223"
End Sub

总共有近60个复选框定义 - 30个用于点击每个复选框定义,另外30个用于取消它们。

以下是我的问题:

(1)不是对每30个复选框进行硬编码,是否可以使用循环来定义它们?我不确定如何将复选框名称(CheckBox1,Checkbox2等)定义为变量以及如何为命令创建通用表单

(2)删除数据集的尝试似乎不起作用。例如:如果我删除了SeriesCollection(2),则vba会重新编号使其ID号无法恢复的所有其他数据集。

我真的很感激任何关于同样的指导。

1 个答案:

答案 0 :(得分:2)

  • 创建一个自定义类,为所有人处理一个组事件 组合框。
  • 将类级别集合添加到userform以保持对自定义类的引用。
  • 创建一个表以根据Checkboxes索引
  • 查找设置

Lookup Table Download

enter image description here

enter image description here

ControlWrapper类代码

Public WithEvents ChartCheckbox As MSForms.CheckBox
Public index As Long

Private Sub ChartCheckbox_Click()
    MsgBox index
    With ActiveChart
        .ChartType = xlXYScatterLinesNoMarkers
        .SeriesCollection.NewSeries
        .SeriesCollection(index).Name = Application.VLookup(index, Range("ChartSettings"), 3, False)
        .SeriesCollection(index).XValues = "=" & Application.VLookup(index, Range("ChartSettings"), 4, False)
        .SeriesCollection(index).Values = "=" & Application.VLookup(index, Range("ChartSettings"), 5, False)
    End With
End Sub

用户形式代码

Private ControlsCollection As Collection

Private Sub UserForm_Initialize()

    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlXYScatterLinesNoMarkers
    ActiveChart.SeriesCollection.NewSeries

    ActiveChart.SeriesCollection(1).XValues = "=Sheet1!$A$21:$A$23"
    ActiveChart.SeriesCollection(1).Values = "=Sheet1!$B$21:$B$23"
    With ActiveChart.Parent
        .Height = 300    ' resize
        .Width = 600    ' resize
        .Top = 100    ' reposition
        .Left = 100    ' reposition
    End With

    ' Initiate the Controls Collection

    Dim ctrl
    Dim wrapper As ControlWrapper
    Set ControlsCollection = New Collection

    For Each ctrl In Me.Controls
        If TypeOf ctrl Is MSForms.CheckBox Then
            Set wrapper = New ControlWrapper
            Set wrapper.ChartCheckbox = ctrl
            wrapper.index = Replace(ctrl.Name, "CheckBox", "")
            ControlsCollection.Add wrapper
        End If
    Next
End Sub