dgregory1979
New Member
- Joined
- Oct 4, 2017
- Messages
- 1
First, I am new to VBA and this is my very first time posting a question. Please let me know if I should provide additional detail, etc. Here goes… I have a worksheet (built with MS Excel 2013) with a table and graph. There are VBA checkboxes built in so the end user can make a selection to add/remove select groups from the graph (e.g., Group1, Group2, …, Group<n>), which is read directly from the table on the same worksheet. The current setup with the code pasted below works as desired.
However, I need to make a change to accommodate multiple selections and I am unsure how to do so. For example, I need to add/accommodate selections for subgroups within each group (e.g., Subgroup1, Subgroup2, Subgroup3). The “Group” values are currently in Column C in the spreadsheet and the new Subgroup values are in Column D. I am unsure how to update the code for the multiple selections. For example, if the user selects a checkbox for Group1 and Subgroup1, a single row of data will be displayed on the chart. If the user selects a checkbox for both Group1 and Group2 and Subgroup1, two rows will be displayed on the chart. If the user selects a checkbox for both Group1 and Group2 and Subgroup1 and Subgroup2, four rows will be displayed on the chart. Each Group and Subgroup correspond to a row of data in the table. I am unsure where to start to get the multiple selections (i.e., Group and subgroup) to work together. I greatly appreciate any advice. Thanks!
Current VBA code to select rows in table to display in a chart (based on check box)
Sub selectesco(Grp As String)
Dim row As Integer
application.ScreenUpdating = False
wrksht = ActiveSheet.Name
Range("C30:C1000").Select
Selection.Find(What:=Grp, After:=ActiveCell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
row = ActiveCell.row + 1
ActiveSheet.ChartObjects("Chart 1").Activate
With ActiveChart.SeriesCollection.NewSeries
.Values = "='" & wrksht & "'!$F$" & row & ":$Y$" & row & ", '" & wrksht & "'!$AA$" & row & ":$AF$" & row
.Name = "='" & wrksht & "'!$C$" & row - 1
.XValues = "=('" & wrksht & "'!F32:Y33, '" & wrksht & "'!AA32:AF33)"
End With
application.ScreenUpdating = True
End Sub
VBA code for check box
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
selectesco ("Group1")
Else: DeleteSeriesWith_Series_InName ("Group1")
End If
End Sub
However, I need to make a change to accommodate multiple selections and I am unsure how to do so. For example, I need to add/accommodate selections for subgroups within each group (e.g., Subgroup1, Subgroup2, Subgroup3). The “Group” values are currently in Column C in the spreadsheet and the new Subgroup values are in Column D. I am unsure how to update the code for the multiple selections. For example, if the user selects a checkbox for Group1 and Subgroup1, a single row of data will be displayed on the chart. If the user selects a checkbox for both Group1 and Group2 and Subgroup1, two rows will be displayed on the chart. If the user selects a checkbox for both Group1 and Group2 and Subgroup1 and Subgroup2, four rows will be displayed on the chart. Each Group and Subgroup correspond to a row of data in the table. I am unsure where to start to get the multiple selections (i.e., Group and subgroup) to work together. I greatly appreciate any advice. Thanks!
Current VBA code to select rows in table to display in a chart (based on check box)
Sub selectesco(Grp As String)
Dim row As Integer
application.ScreenUpdating = False
wrksht = ActiveSheet.Name
Range("C30:C1000").Select
Selection.Find(What:=Grp, After:=ActiveCell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
row = ActiveCell.row + 1
ActiveSheet.ChartObjects("Chart 1").Activate
With ActiveChart.SeriesCollection.NewSeries
.Values = "='" & wrksht & "'!$F$" & row & ":$Y$" & row & ", '" & wrksht & "'!$AA$" & row & ":$AF$" & row
.Name = "='" & wrksht & "'!$C$" & row - 1
.XValues = "=('" & wrksht & "'!F32:Y33, '" & wrksht & "'!AA32:AF33)"
End With
application.ScreenUpdating = True
End Sub
VBA code for check box
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
selectesco ("Group1")
Else: DeleteSeriesWith_Series_InName ("Group1")
End If
End Sub