Hello All,
Been struggling trying to adapt my working VBA to a user friendly one. I have data in the form:
| X. |. Y. |
A. |. 1. |. 2. |
B. |. 2. |. 3. |
C. |. 3. |. 3. |
With A, B, and C with different conditional formatting colour.
I am developing a scatter graph with automatic labelling for each point and automatic marker colouring for each point using the cell offset to the left of the data.
The labelling code I have works perfectly but I've had difficulty developing my colouring code. It works but I have to select the range each time. If anybody can help me adapt it it would be much appreciated.
Cheers. (Apologies for poor formatting, having to complete this on my phone whilst on the move).
Been struggling trying to adapt my working VBA to a user friendly one. I have data in the form:
| X. |. Y. |
A. |. 1. |. 2. |
B. |. 2. |. 3. |
C. |. 3. |. 3. |
With A, B, and C with different conditional formatting colour.
I am developing a scatter graph with automatic labelling for each point and automatic marker colouring for each point using the cell offset to the left of the data.
The labelling code I have works perfectly but I've had difficulty developing my colouring code. It works but I have to select the range each time. If anybody can help me adapt it it would be much appreciated.
Code:
Sub AttachLabelsToPoints()
'Dimension variables.
Dim Counter As Integer, ChartName As String, xVals As String
' Disable screen updating while the subroutine is run.
Application.ScreenUpdating = False
'Store the formula for the first series in "xVals".
xVals = ActiveChart.SeriesCollection(1).Formula
'Extract the range for the data from xVals.
xVals = Mid(xVals, InStr(InStr(xVals, ","), xVals, _
Mid(Left(xVals, InStr(xVals, "!") - 1), 9)))
xVals = Left(xVals, InStr(InStr(xVals, "!"), xVals, ",") - 1)
Do While Left(xVals, 1) = ","
xVals = Mid(xVals, 2)
Loop
'Attach a label to each data point in the chart.
For Counter = 1 To Range(xVals).Cells.Count
ActiveChart.SeriesCollection(1).Points(Counter).HasDataLabel = _
True
ActiveChart.SeriesCollection(1).Points(Counter).DataLabel.Text = _
Range(xVals).Cells(Counter, 1).Offset(0, -1).Value
Next Counter
End Sub
Sub ColorPoints()
Dim cht As Chart
Dim ser As Series
Dim pnt As Point
Dim i As Long, j As Long
Dim rng As Range
Set cht = ActiveChart
Set ser = cht.SeriesCollection(1)
Set rng = ActiveSheet.Range("AH16:AH22") ' Each of these cells has a different color
j = 0
For i = 1 To ser.Points.Count
j = j + 1
Set pnt = ser.Points(i)
pnt.MarkerBackgroundColor = rng(j).DisplayFormat.Interior.Color ' Cycle through available colors
If (j > rng.Count) Then j = 0
Next i
End Sub
Cheers. (Apologies for poor formatting, having to complete this on my phone whilst on the move).