Chart formatting macro with spontaneous behavior

seraslibre

New Member
Joined
Sep 1, 2009
Messages
9
I have created a function to generate an x-y chart that I call on to generate about 40 charts (hence the number of inputs to the function). However, every time I open excel to run the macro, the field sizes are different, and rarely correspond to what I have set in the macro. i.e. The chart title field is set to 14pt size, but that is rarely the case in the end product. All the other fields, including axis name size, scale text size, and legend size, are all strange sizes. I would say 50% of the time the fiels are the correct size, with the exception of the title field which is usually larger 80% of the time.

Any ideas?

Code:
Function PlotRamps(Worksheet As String, firstcol As Integer, StartRow As Integer, chartheight As Single, nextcharth As Single, chartwidth As Single, nextchartw As Single)
Dim dataLength As Integer
dataLength = Range("A11", Range("A11").End(xlDown)).Rows.Count + 100
Dim Datasets As Integer
Datasets = Range("A11", Range("A11").End(xlToRight)).Cells.Count
Dim myChtObj As ChartObject
Dim rngChtYval As Range
Dim rngChtXval As Range
Dim Title As String
Dim RampType As String
Dim startcol As Integer
startcol = firstcol
If StartRow = 11 Then
    RampType = "Engagement"
    Else
    RampType = "Disengagement"
End If
Title = RampType & " Ramp p=" & Cells(5, startcol) & "; T=" & Cells(6, startcol) & "; n= 0-300-0 rpm"
 
    With Sheets(Worksheet)
          Set myChtObj = Sheets(Worksheet).ChartObjects.Add(Left:=nextchartw, Width:=chartwidth, Top:=nextcharth, Height:=chartheight)
            With myChtObj.Chart
                .ChartType = xlXYScatterLinesNoMarkers
 
                Do Until .SeriesCollection.Count = 0
                    .SeriesCollection(1).delete
                Loop
 
                Do While startcol <= Datasets
                    Set rngChtXval = Range(Cells(StartRow, startcol), Cells(StartRow + dataLength, startcol))
                    Set rngChtYval = Range(Cells(StartRow, startcol + 1), Cells(StartRow + dataLength, startcol + 1))
 
                    With .SeriesCollection.NewSeries
                        .Values = rngChtYval
                        .XValues = rngChtXval
                        .Name = Cells(7, startcol)
                    End With
                startcol = startcol + 18
 
                Loop
                .HasTitle = True
                .ChartTitle.text = Title
                .ChartTitle.Font.Size = 14
                .Legend.Font.Size = 10
                .Legend.Height = 200
 
                With .Axes(xlCategory, xlPrimary)
                    .HasTitle = True
                    .AxisTitle.Characters.text = "Sliding Speed in rpm"
                    .AxisTitle.Font.Size = 12
                End With
                With .Axes(xlValue, xlPrimary)
                    .HasTitle = True
                    .AxisTitle.Characters.text = "Mu"
                    .AxisTitle.Font.Size = 12
                End With
                With .Axes(xlCategory)
                    .MaximumScale = 300
                    .MinimumScale = 0
                    .MajorUnit = 60
                    .AxisTitle.Font.Size = 10
                    .AxisTitle.Font.Bold = False
                    .TickLabels.Font.Size = 10
                    If StartRow <> 11 Then
                        .ReversePlotOrder = True
                    End If
                End With
                With .Axes(xlValue)
                    .MaximumScale = 0.2
                    .MinimumScale = 0
                    .MajorUnit = 0.02
                    .AxisTitle.Font.Size = 10
                    .AxisTitle.Font.Bold = False
                    .TickLabels.Font.Size = 10
                End With
 
            End With
    End With
End Function
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Forum statistics

Threads
1,223,912
Messages
6,175,340
Members
452,638
Latest member
Oluwabukunmi

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