Running loop in excel vba 2016 for changing series in charts - vba

I have the following macro and I wish to loop the following program for 500 charts starting from 1.
Sub Arrow()
'
' Arrow Macro
'
' Keyboard Shortcut: Ctrl+q
'
ActiveSheet.ChartObjects("Chart 459").Activate
ActiveChart.FullSeriesCollection(1).Select
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(192, 0, 0)
.Transparency = 0
End With
With Selection.Format.Line
.Visible = msoTrue
.Weight = 2.5
End With
Selection.Format.Line.EndArrowheadStyle = msoArrowheadTriangle
With Selection.Format.Line
.EndArrowheadLength = msoArrowheadLengthMedium
.EndArrowheadWidth = msoArrowheadWide
End With
ActiveChart.FullSeriesCollection(2).Select
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent5
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = -0.5
.Transparency = 0
End With
With Selection.Format.Line
.Visible = msoTrue
.Weight = 2.5
End With
Selection.Format.Line.EndArrowheadStyle = msoArrowheadTriangle
With Selection.Format.Line
.EndArrowheadLength = msoArrowheadLengthMedium
.EndArrowheadWidth = msoArrowheadWide
End With
End Sub

I agree with #Jeeped. What you want is not difficult. However, moving from Select etc to Index based takes some learning.
the code below should do what you want. It worked for me in Office 2010, which uses SeriesCollection(1) instead of FullSeriesCollection(1)
Sub Arrow() ' ' Arrow Macro ' ' Keyboard Shortcut: Ctrl+q ' ActiveSheet.ChartObjects("Chart 459").Activate
Dim i As Long
Dim cht As Chart
For i = 1 To ActiveWorkbook.Charts.Count
Set cht = ActiveWorkbook.Charts(i)
With cht.FullSeriesCollection(1).Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(192, 0, 0)
.Transparency = 0
.Weight = 2.5
.EndArrowheadStyle = msoArrowheadTriangle
.EndArrowheadLength = msoArrowheadLengthMedium
.EndArrowheadWidth = msoArrowheadWide
End With
With cht.FullSeriesCollection(2).Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent5
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = -0.5
.Transparency = 0
.Visible = msoTrue
.Weight = 2.5
.EndArrowheadStyle = msoArrowheadTriangle
.EndArrowheadLength = msoArrowheadLengthMedium
.EndArrowheadWidth = msoArrowheadWide
End With
Next i
End Sub
Now you know how to use a For Loop and Index based references.

Related

Insert watermark in Word Documents

I am seeking a way to insert a watermark into Word documents. Here is the code I get by recording Macros,
Sub add_watermark()
'
' Macro2 Macro
'
'
ActiveDocument.Sections(1).Range.Select
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.HeaderFooter.Shapes.AddTextEffect( _
PowerPlusWaterMarkObject354239640, "PAID", "arial", 1, False, False, 0, 0 _
).Select
Selection.ShapeRange.Name = "PowerPlusWaterMarkObject354239640"
Selection.ShapeRange.TextEffect.NormalizedHeight = False
Selection.ShapeRange.Line.Visible = False
Selection.ShapeRange.Fill.Visible = True
Selection.ShapeRange.Fill.Solid
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(192, 192, 192)
Selection.ShapeRange.Fill.Transparency = 0
Selection.ShapeRange.Rotation = 315
Selection.ShapeRange.LockAspectRatio = True
Selection.ShapeRange.Height = CentimetersToPoints(9.31)
Selection.ShapeRange.Width = CentimetersToPoints(13.96)
Selection.ShapeRange.WrapFormat.AllowOverlap = True
Selection.ShapeRange.WrapFormat.Side = wdWrapNone
Selection.ShapeRange.WrapFormat.Type = 3
Selection.ShapeRange.RelativeHorizontalPosition = _
wdRelativeVerticalPositionMargin
Selection.ShapeRange.RelativeVerticalPosition = _
wdRelativeVerticalPositionMargin
Selection.ShapeRange.Left = wdShapeCenter
Selection.ShapeRange.Top = wdShapeCenter
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub
But I have an "out of range" error after running the Macro in another document. When I debug it, this line
"Selection.ShapeRange.Name = "PowerPlusWaterMarkObject354239640" is highlighted.
Does anyone know how to tackle it?
Thanks,
Try something based on:
Sub AddPaidWatermark()
Application.ScreenUpdating = False
Dim sWdth As Single, Shp As Shape
With ActiveDocument.Sections(1)
With .PageSetup
sWdth = .PageWidth - .LeftMargin - .RightMargin - .Gutter
End With
With .Headers(wdHeaderFooterPrimary)
If .Range.Characters.First.Information(wdWithInTable) = True Then
With .Range.Tables(1)
.Rows.Add .Rows(1)
.Split .Rows(2)
End With
.Range.Tables(1).Delete
.Range.Paragraphs(1).Range.Font.Hidden = True
End If
Set Shp = .Shapes.AddTextEffect(msoTextEffect1, "PAID", "Arial", 1, False, False, 0, 0)
End With
With Shp
.WrapFormat.Type = wdWrapBehind
.ZOrder msoBringToFront
.Height = sWdth / 2 ^ 0.5
.Width = .Height
.Rotation = 315
.RelativeHorizontalPosition = wdRelativeVerticalPositionMargin
.RelativeVerticalPosition = wdRelativeVerticalPositionMargin
.Left = wdShapeCenter
.Top = wdShapeCenter
With .Fill
.Visible = True
.Solid
.ForeColor.RGB = RGB(192, 192, 192)
End With
End With
End With
Application.ScreenUpdating = True
End Sub

How do I create a Macro that adds shapes in a particular colour and groups them?

Please can you assist? I'm trying to create a button on Excel that once clicked opens UserForm1 (via a macro that has the command UserForm1.Show).
Thereafter, I want the userform to present options. These options will then (when selected create 2 rectangular shapes and group them. Take a look at some screenshots below along with macro recording code(too specific).
Ultimate objective: I want to create stickies on Excel. I've created 2 blocks and want to group them together everytime. So whenever I click on a button it creates a sticky for me :)
The error I get is
The item with specified name was not found
Code for the 2 options on the form:
Private Sub OptionButton1_Click()
ActiveSheet.Shapes.AddShape(msoShapeRectangle, 285, 74.25, 112.5, 108.75). _
Select
Selection.ShapeRange.Line.Visible = msoFalse
With Selection.ShapeRange.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent6
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0.6000000238
.Transparency = 0
.Solid
End With
ActiveSheet.Shapes.AddShape(msoShapeRectangle, 285.75, 74.25, 111.75, 21.75). _
Select
With Selection.ShapeRange.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent6
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0.400000006
.Transparency = 0
.Solid
End With
Selection.ShapeRange.Line.Visible = msoFalse
Range("J11").Select
ActiveSheet.Shapes.Range(Array("Rectangle 23")).Select
ActiveSheet.Shapes.Range(Array("Rectangle 23", "Rectangle 24")).Select
Selection.ShapeRange.Group.Select
End Sub
Private Sub OptionButton2_Click()
ActiveSheet.Shapes.AddShape(msoShapeRectangle, 286.5, 74.25, 111, 108.75). _
Select
Selection.ShapeRange.Line.Visible = msoFalse
With Selection.ShapeRange.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent5
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0.400000006
.Transparency = 0
.Solid
End With
ActiveSheet.Shapes.AddShape(msoShapeRectangle, 285.75, 74.25, 111.75, 18.75). _
Select
With Selection.ShapeRange.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent5
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = -0.25
.Transparency = 0
.Solid
End With
ActiveSheet.Shapes.Range(Array("Rectangle21")).Select
ActiveSheet.Shapes.Range(Array("Rectangle21", "Rectangle22")).Select
Selection.ShapeRange.Group.Select
End Sub
code as image
User Form and end product:
You should use variables Shape1 and Shape2 to remember the new added shapes. Note that you have to use .OLEFormat.Object and cannot directly access the item like you can with Selection (which is a bit odd by Excel).
This way you can get independent from the hard coded shape names.
Option Explicit
Private Sub OptionButton1_Click()
Dim sht As Worksheet
Set sht = ThisWorkbook.ActiveSheet ' instead I recommend to reference a worksheet
' by name: ThisWorkbook.Worksheets("SheetName")
Dim Shape1 As Shape
Set Shape1 = sht.Shapes.AddShape(msoShapeRectangle, 285, 74.25, 112.5, 108.75)
Shape1.OLEFormat.Object.ShapeRange.Line.Visible = msoFalse
With Shape1.OLEFormat.Object.ShapeRange.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent6
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0.6000000238
.Transparency = 0
.Solid
End With
Dim Shape2 As Shape
Set Shape2 = sht.Shapes.AddShape(msoShapeRectangle, 285.75, 74.25, 111.75, 21.75)
Shape2.OLEFormat.Object.ShapeRange.Line.Visible = msoFalse
With Shape2.OLEFormat.Object.ShapeRange.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent6
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0.400000006
.Transparency = 0
.Solid
End With
sht.Shapes.Range(Array(Shape1.Name, Shape2.Name)).Group
End Sub
Thanks for all the help, I tweaked my code a bit and solved it myself. Essentially, I had to make sure that I could grab the name of the objects correctly to group them. Take a look at the code below:
Private Sub OptionButton1_Click()
Dim James1 As Shape
Dim James2 As Shape
Set James1= ActiveSheet.Shapes.AddShape(msoShapeRectangle, 285, 74.25, 112.5, 108.75)
James1.Select
Selection.ShapeRange.Line.Visible = msoFalse
With Selection.ShapeRange.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent6
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0.6000000238
.Transparency = 0
.Solid
End With
Set James2= ActiveSheet.Shapes.AddShape(msoShapeRectangle, 285.75, 74.25, 111.75, 21.75)
James2.Select
With Selection.ShapeRange.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent6
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0.400000006
.Transparency = 0
.Solid
End With
Selection.ShapeRange.Line.Visible = msoFalse
Dim S1 As String
Dim S2 As String
S1 = James1.Name
S2 = James2.Name
ActiveSheet.Shapes.Range(Array(S1, S2)).Select
Selection.ShapeRange.Group.Select

Get chart name with VBA

I am setting up a macro to generate a chart. I have recorded a macro while I was generating the sample chart, but now I need to have the macro working independently from the name of the chart (Chart 9 in this case)
Sheets("statistics").Select
Sheets("statistics").Range("A101:C106").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlColumnStacked
ActiveChart.SetSourceData Source:=Range("statistics!$A$101:$C$106")
ActiveChart.ChartArea.Select
ActiveSheet.Shapes("Chart 9").Name = "waterfall"
ActiveChart.Location Where:=xlLocationAsObject, Name:="summary"
ActiveSheet.ChartObjects("waterfall").Activate
ActiveSheet.Shapes("waterfall").IncrementLeft 80
ActiveSheet.Shapes("waterfall").IncrementTop -2200
ActiveSheet.ChartObjects("waterfall").Activate
ActiveSheet.Shapes("waterfall").ScaleWidth 1.6025463692, msoFalse, msoScaleFromTopLeft
ActiveSheet.Shapes("waterfall").ScaleHeight 1.6084106153, msoFalse, msoScaleFromTopLeft
ActiveSheet.ChartObjects("waterfall").Activate
ActiveChart.Legend.Select
Selection.Delete
ActiveSheet.ChartObjects("waterfall").Activate
ActiveChart.SeriesCollection(1).Select
Selection.Format.Fill.Visible = msoFalse
ActiveChart.SeriesCollection(2).Select
ActiveChart.SeriesCollection(2).Points(6).Select
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent3
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Solid
End With
ActiveChart.SeriesCollection(2).Points(1).Select
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent3
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Solid
End With
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
.Solid
End With
ActiveChart.SeriesCollection(2).Points(5).Select
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Solid
End With
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.RGB = RGB(255, 0, 0)
.Transparency = 0
.Solid
End With
ActiveChart.SetElement (msoElementDataLabelCenter)
ActiveChart.SeriesCollection(2).Points(1).Select
ActiveChart.ChartArea.Select
ActiveChart.ChartArea.Select
ActiveChart.SeriesCollection(2).Points(1).Select
ActiveChart.PlotArea.Select
ActiveChart.SeriesCollection(2).Select
ActiveChart.SetElement (msoElementDataLabelCenter)
ActiveChart.SetElement (msoElementPrimaryValueAxisTitleHorizontal)
Selection.Caption = "hrs"
ActiveChart.Axes(xlValue).AxisTitle.Select
Selection.Left = 7
Selection.Top = 13.028
I have tried
Sheets("statistics").Range("A101:C106").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlColumnStacked
ActiveChart.SetSourceData Source:=Range("statistics!$A$101:$C$106")
ActiveChart.ChartArea.Select
Set ThisChart = ActiveChart
ActiveSheet.Shapes(ThisChart).Name = "waterfall"
but it is not working
Try the code below, it will loop through all existing ChartObjects in "statistics" worksheet, and if it finds a chartobject with a name of "Chart 9" it will rename it to "waterfall".
Note: you could use a similar approach to create the chart, without the need to use Select, ActiveSheet and ActiveChart.
Code
Option Explicit
Sub RenameExistingChart()
Dim ChtObj As ChartObject
For Each ChtObj In Worksheets("statistics").ChartObjects
If ChtObj.Name = "Chart 9" Then
ChtObj.Name = "waterfall"
End If
Next ChtObj
End Sub
Edit 1: create the chart with ChtObj:
Set ChtObj = Worksheets("statistics").ChartObjects.Add(Left:=100, Top:=100, _
Width:=100, Height:=100) ' <-- just default settings , modify later
With ChtObj
.Chart.ChartType = xlColumnStacked
.Chart.SetSourceData Source:=range("statistics!$A$101:$C$106")
.Name = "waterfall"
With .Chart.SeriesCollection(2).Format.Fill ' modify fill for series (2)
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent3
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Solid
End With
.Chart.SeriesCollection(1).ApplyDataLabels ' add data lables to series (1)
End With
You could use something like this:
Sub ChartStuff()
Dim cht As Shape
Range("A101:A106").Select
ActiveSheet.Shapes.AddChart.Select
Set cht = ActiveSheet.Shapes(1)
cht.Name = "waterfall"
End Sub
Hope this helps!
Dealing with charts in VBA is a little complicated.
When you use Addchart, Selection will be a ChartArea
A ChartArea is part of a Chart which is part of a ChartObject
The name of a chart you see is in fact the name of the ChartObject
You can do something like this:
Range("A101:A106").Select
ActiveSheet.Shapes.AddChart.Select
Dim ca As ChartArea, ch As Chart, co As ChartObject
Set ca = Selection
Set ch = ca.Parent
ch.ChartType = xl3DColumn
Set co = ch.Parent
co.Name = "waterfall"
Debug.Print ca.Name, ch.Name, co.Name
Create a function that you call from within the sub, that sends the name of the active chart, for example:
Function actchart(ActiveChart As String) As String
actchart = ActiveChart
End Function
And then from within your sub, you can replace, as an example, where you have:
ActiveSheet.Shapes("Chart 9").Name = "waterfall"
with
ActiveSheet.Shapes(actchart(ActiveChart.Parent.Name)).Name = "waterfall"
This worked for me with the same issue! Hope it helps.

VBA: Formatting Multiple Selected Charts (Chart, Plot, Legend, etc.)

I am looking to format multiple selected charts on Excel 2010 using VBA. I want the code to work whether I choose one or multiple charts. The code below works when only one chart is selected but when multiple charts are selected, I get a "run-time error '91' Object variable or With Block variable not set". Any idea how to run the macro for number of selected charts?
Sub ChartFormat5_Click()
''Adjust chart area
ActiveChart.ChartArea.Select
'Size
Selection.Width = 631.9
Selection.Height = 290.1
'Border
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorText1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
.Weight = 1
.DashStyle = msoLineSolid
End With
'Font
With Selection.Format.TextFrame2.TextRange.Font
.Name = "Calibri"
.Size = 10
.Fill.Visible = msoTrue
.Fill.ForeColor.ObjectThemeColor = msoThemeColorText1
.Fill.ForeColor.TintAndShade = 0
.Fill.ForeColor.Brightness = 0
.Fill.Transparency = 0
.Fill.Solid
End With
''Adjust axis alignment and format
ActiveChart.Axes(xlCategory).Select
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorText1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
End With
ActiveChart.Axes(xlCategory).TickLabelSpacing = 1
ActiveChart.Axes(xlCategory).TickLabels.Orientation = 45
ActiveChart.Axes(xlValue).Select
Selection.TickLabels.NumberFormat = "#,##0_);(#,##0)"
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorText1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
End With
ActiveChart.Axes(xlValue).AxisTitle.Select
Selection.Left = 1.5
Selection.Format.Line.Visible = msoFalse
''Adjust legend box
ActiveChart.Legend.Select
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.RGB = RGB(255, 255, 255)
.Transparency = 0
.Solid
End With
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorBackground1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = -0.5
.Transparency = 0
End With
Selection.Left = 124
Selection.Top = 67
''Adjust plot area size and format
ActiveChart.PlotArea.Select
'Borders
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorText1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
.Weight = 0.75
.DashStyle = msoLineSolid
End With
'Size
Selection.Width = ActiveChart.ChartArea.Width - 30.4
Selection.Height = ActiveChart.ChartArea.Height - 8.5
Selection.Top = 4
Selection.Left = 20
'Gridlines
ActiveChart.Axes(xlValue).MajorGridlines.Select
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorText1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
End With
With Selection.Format.Line
.Visible = msoTrue
.DashStyle = msoLineDash
End With
End Sub
This will process the active chart or all selected charts. The first routine determines what to process (active chart or selected charts) and the second processes each.
Sub FormatCharts()
Dim obj As Object
If Not ActiveChart Is Nothing Then
FormatOneChart ActiveChart
Else
For Each obj In Selection
If TypeName(obj) = "ChartObject" Then
FormatOneChart obj.Chart
End If
Next
End If
End Sub
Sub FormatOneChart(cht As Chart)
' do all your formatting here, based on cht not on ActiveChart
End Sub
Don't select parts of the chart, just fully reference them. Instead of
ActiveChart.ChartArea.Select
With Selection.Format.Line
use this
With cht.ChartArea.Format.Line
etc.
Just started answering questions on stackoverflow, so I hope this will help you out.
Since you selected multiple charts at once, you should scrap ActiveChart.ChartArea.Select
Just loop through each ChartObject in your current selection as follows:
Sub ChartFormat5_Click()
Dim cObject As ChartObject
For Each cObject In Selection
With cObject
'Do all your stuff here... E.g.
.Chart.PlotArea.Width = 631.9
End With
Next cObject
End Sub

VBA to copy chart line color to many charts in different worksheets

At work I have 72 Excel 2010 workbooks in total, each with 12 sheets, with a chart on each sheet (I think this means the charts are not embedded?). I am a basic programmer having only covered VB at A-Level.
I need all charts (on the 12 seperate sheets) in a workbook to have the same coloured data lines as the first chart in that workbook.
My initial thoughts were to record a macro of me manually changing the line colours, thicknesses and so on then view the code for this macro and put some sort of loop around it.
After many hours trying different suggestions and many google searches I can't get it to work.
The code I have so far is as follows:
Sub Macro1()
Dim i As Integer
Dim sht As Worksheet
For i = 1 To ActiveWorkbook.Worksheets.Count
Set sht = ActiveWorkbook.Sheets(i)
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.Legend.Select
ActiveChart.Legend.LegendEntries(1).Select
ActiveChart.SeriesCollection(1).Select
With Selection
.MarkerStyle = 2
.MarkerSize = 7
End With
Selection.MarkerStyle = -4168
Selection.Format.Fill.Visible = msoFalse
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
End With
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(255, 0, 0)
.Transparency = 0
End With
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(255, 0, 0)
End With
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(255, 0, 0)
.Transparency = 0
End With
With Selection.Format.Line
.Visible = msoTrue
.Weight = 1.25
End With
ActiveChart.Legend.Select
ActiveChart.Legend.LegendEntries(2).Select
ActiveChart.SeriesCollection(2).Select
With Selection
.MarkerStyle = 1
.MarkerSize = 7
End With
Selection.MarkerStyle = -4168
Selection.Format.Fill.Visible = msoFalse
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(255, 0, 0)
End With
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 112, 192)
.Transparency = 0
End With
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 112, 192)
End With
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 112, 192)
.Transparency = 0
End With
With Selection.Format.Line
.Visible = msoTrue
.Weight = 1.25
End With
ActiveChart.Legend.Select
ActiveChart.Legend.LegendEntries(3).Select
ActiveChart.SeriesCollection(3).Select
With Selection
.MarkerStyle = 3
.MarkerSize = 7
End With
Selection.MarkerStyle = -4168
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent1
.ForeColor.TintAndShade = 0
' .ForeColor.Brightness = 0
.Solid
End With
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 176, 80)
.Transparency = 0
.Solid
End With
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 112, 192)
End With
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 176, 80)
.Transparency = 0
End With
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 176, 80)
End With
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 176, 80)
.Transparency = 0
End With
With Selection.Format.Line
.Visible = msoTrue
.Weight = 1.25
End With
Selection.Format.Fill.Visible = msoFalse
ActiveChart.Legend.Select
ActiveChart.Legend.LegendEntries(4).Select
ActiveChart.SeriesCollection(4).Select
With Selection
.MarkerStyle = -4168
.MarkerSize = 7
End With
Selection.Format.Fill.Visible = msoFalse
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 176, 80)
End With
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(112, 48, 160)
.Transparency = 0
End With
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(112, 48, 160)
End With
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(112, 48, 160)
.Transparency = 0
End With
With Selection.Format.Line
.Visible = msoTrue
.Weight = 1.25
End With
Next i
End Sub
This code runs and does what I want but only on the worksheet you actually have open in excel, it will not run through and run the macro on each worksheet in the workbook. Any ideas?
Thanks in advance
You could call your Sub Macro1 from a loop that loops through all Worksheets.
For example:
Sub WorksheetLoop()
' Declare Current as a worksheet object variable.
Dim Current As Worksheet
' Loop through all of the worksheets in the active workbook.
For Each Current In Worksheets
' Insert your code here.
' This line displays the worksheet name in a message box.
MsgBox Current.Name
Next
End Sub
Then you can pass the Current Worksheet to your Function and run your code on that sheet.
For more info see: Macro to Loop Through All Worksheets in a Workbook
In this case you would change your code like this:
Sub Macro1(Byval Current As Worksheet)
Dim i As Integer
Dim sht As Worksheet
For i = 1 To ActiveWorkbook.Worksheets.Count
Set sht = Current
sht.ChartObjects("Chart 1").Activate
.....
End Sub
And create a loop like this:
Sub WorksheetLoop()
Dim Current As Worksheet
For Each Current In Worksheets
Call Macro1(Current)
Next
End Sub