HELP ME! VBA to automatically format charts

roomgrey

New Member
Joined
Sep 22, 2017
Messages
1
Hello guys,

I'm a total noob in VBA. Please help me to merge this two solution into one below. The 1st is a loop that colors the chart's lines based on the 1st cells color. The 2nd can change the line style based on the text written in a cell (currently D4). What I'd like to have is a macro that clones the color of the 1st cell in the series for the chart line color and clones the style of the same series based on a cell text at the beginnig or at the end of the series.
Here are the macros:

1st:
File-Copy-icon.png

<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">Sub CellColorsToChart()
Dim oChart As ChartObject
Dim MySeries As Series
Dim FormulaSplit As Variant
Dim SourceRange As Range
Dim SourceRangeColor As Long

'Loop through all charts in the active sheet
For Each oChart In ActiveSheet.ChartObjects

'Loop through all series in the target chart
For Each MySeries In oChart.chart.SeriesCollection

'Get Source Data Range for the target series
FormulaSplit = Split(MySeries.Formula, ",")

'Capture the first cell in the source range then trap the color
Set SourceRange = Range(FormulaSplit(2)).Item(1)
SourceRangeColor = SourceRange.Interior.Color

On Error Resume Next
'Coloring for Excel 2003
MySeries.Interior.Color = SourceRangeColor
MySeries.Border.Color = SourceRangeColor

'Coloring for Excel 2007 and 2010
MySeries.Format.Line.ForeColor.RGB = SourceRangeColor
MySeries.Format.Line.BackColor.RGB = SourceRangeColor
MySeries.Format.Fill.ForeColor.RGB = SourceRangeColor
MySeries.Format.Line.ForeColor.RGB = SourceRangeColor

Next MySeries
Next oChart

End Sub</code>

2nd:
File-Copy-icon.png

<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">Private Sub Worksheet_Change(ByVal Target As Range)

Dim dash_style As String
Set Target = Range("d1")
dash_style = Target.Value

With ActiveSheet.ChartObjects("Wykres 1").Chart.SeriesCollection(1).Format.Line
Select Case dash_style
Case "LineDash": .DashStyle = msoLineDash
Case "LineDashDot": .DashStyle = msoLineDashDot
Case "LineDashDotDot": .DashStyle = msoLineDashDotDot
Case "LineLongDash": .DashStyle = msoLineLongDash
Case "LineLongDashDot": .DashStyle = msoLineLongDashDot
Case "LineRoundDot": .DashStyle = msoLineRoundDot
Case "LineSolid": .DashStyle = msoLineSolid
Case "LineSquareDot": .DashStyle = msoLineSquareDot
Case Else:
End Select
End With
Set Target = Nothing
End Sub</code>

thanks a lot,
Norbert
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top