Hello guys,
I'm having difficulties with this piece of code. I have searched the forum, but no luck till now (maybe im not using the right keywords).Here is my problem and thanks in advance.
I am trying to create a chart with VBA that compares the metric (grade percentage) of different people for different months of the year. The trick here is that the number of people to be compared might be different from one run to another. The max number of people to be compared is set to 10. Also, a target on the graph is set using a fixed range.
I have inserted a combobox that enables the user to pick the number of people to be compared before starting the macro in able for the macro to be able to select the right range for the graph. Below is my code which works prfectly except for one instance; when the user only chooses one person to be inserted in the graph . As per the code below, when the user chooses one person, the two ranges to be used should be "H10:H23" and "H24:H37". The problem occurs when the code attempts to change the target range to a line graph. However, for some reason vba can't find the seriescollection (2). This means that the two ranges are considered as one eventhough they are two seperate ranges. Please guide me where have I might gone wrong. Below is a sample table and my my code so far.
Another question I have. Is it possible to include the combobox inside my code ?(maybe in a msg box)
<TBODY>
</TBODY>
Thanks tons
I'm having difficulties with this piece of code. I have searched the forum, but no luck till now (maybe im not using the right keywords).Here is my problem and thanks in advance.
I am trying to create a chart with VBA that compares the metric (grade percentage) of different people for different months of the year. The trick here is that the number of people to be compared might be different from one run to another. The max number of people to be compared is set to 10. Also, a target on the graph is set using a fixed range.
I have inserted a combobox that enables the user to pick the number of people to be compared before starting the macro in able for the macro to be able to select the right range for the graph. Below is my code which works prfectly except for one instance; when the user only chooses one person to be inserted in the graph . As per the code below, when the user chooses one person, the two ranges to be used should be "H10:H23" and "H24:H37". The problem occurs when the code attempts to change the target range to a line graph. However, for some reason vba can't find the seriescollection (2). This means that the two ranges are considered as one eventhough they are two seperate ranges. Please guide me where have I might gone wrong. Below is a sample table and my my code so far.
Another question I have. Is it possible to include the combobox inside my code ?(maybe in a msg box)
10 | Months | A | B | to.. | H (person1) | I (person2) | J (person3) | to.. | R | S |
11 | Jan | x | x | 90% | 89% | 45% | x | x | ||
12 | feb | x | x | 100% | 95% | 80% | x | x | ||
13 | march | x | x | 99% | 58% | 75% | x | x | ||
14 | apr | x | x | 75% | 65% | 50% | x | x | ||
15 | may | x | x | 80% | 79% | 75% | x | x | ||
... | x | x | ||||||||
to... | x | x | ||||||||
24 | target | x | x | 93% | x | x | ||||
25 | target | x | x | 93% | x | x | ||||
26 | target | x | x | 93% | x | x | ||||
..... | x | x | x | x |
<TBODY>
</TBODY>
Code:
Dim GRPerc1 As Range
Dim GRPerc2 As Range
Dim GRPerc3 As Range
Sheets("sheet1").Select ' sheet with data
x = Range("B42").Value 'combo box is linked to this cell
Range("H10").Activate
Set GRPerc1 = Range("H10:H23", ActiveCell.Offset(0, x - 1)) ' Selects the range of data of the people to be compared based on user selection
Set GRPerc2 = Range("H24:H37") ' Selects the Range of data for the Target
Set GRPerc3 = Union( GRPerc1, GRPerc2)
Sheets("Graph").Select ' Graph will be added on this list
On Error Resume Next
ActiveSheet.ChartObjects.Delete
On Error GoTo 0
Charts.Add.Location Where:=xlLocationAsObject, Name:="Graph"
ActiveChart.ChartType = xlColumnClustered
ActiveChart.ApplyLayout (5)
ActiveChart.SetSourceData Source:=GRPerc3
ActiveChart.SeriesCollection(1).XValues = "=sheet1!$A$11:$A$23" 'X-axis labels i.e months of the year
ActiveChart.SeriesCollection(x + 1).ChartType = xlLine ' change target to line graph instead of bar
ActiveChart.SeriesCollection(x + 1).Select
With Selection.Format.Line
.Visible = msoTrue
.Weight = 5
End With
With ActiveChart
.Axes(xlValue).MaximumScale = 1
.HasTitle = False
.HasTitle = True
.ChartTitle.Characters.Text = ActiveSheet.Name ' Chart title
End With
With ActiveChart.Parent
.Height = 400 ' resize
.Width = 800 ' resize
.Top = 100 ' reposition
.Left = 50 ' reposition
End With
Thanks tons