Making a number appear with 3 or 4 decimal places Below is the Sub code for crea
ID: 3560941 • Letter: M
Question
Making a number appear with 3 or 4 decimal places
Below is the Sub code for creating Scatter line charts within added worksheets in my excel workbook. The problem I have is that when the height appears on the chart (once made) the height is only shown as a round up of the actual number. I need the height to appear with at least three decimal places because this is more accurate for my work. Can someone suggest what to add and where in the code below???
Sub deviationcharts(increment As Integer, Company As String, StartDate As String, TankNum As String, sheetname As String, height As Integer)
ActiveSheet.Range("C1:C" & increment).Select
Charts.Add
ActiveChart.ChartType = xlXYScatterLines
ActiveChart.SetSourceData Source:=Sheets(sheetname).Range("C1:D" & (increment + 1)), _
PlotBy:=xlColumns
ActiveChart.SeriesCollection(1).XValues = "=" & sheetname & "!C1:C" & (increment + 1)
ActiveChart.Location Where:=xlLocationAsObject, Name:=sheetname
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "Plate deviation at height " & _
(Int(height)) & "m" & Chr(10) & "Tank number " & (Int(TankNum))
.ChartTitle.Characters.Font.Size = 12
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Angle (degrees)"
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Font.Size = 8
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Deviation (mm)"
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Font.Size = 8
End With
With ActiveChart
.HasAxis(xlCategory, xlPrimary) = True
.HasAxis(xlValue, xlPrimary) = True
End With
ActiveChart.ChartArea.Select
ActiveSheet.Shapes("Chart 1").ScaleHeight 1.7, msoFalse, msoScaleFromBottomRight
ActiveSheet.Shapes("Chart 1").ScaleWidth 1.83, msoFalse, msoScaleFromBottomRight
ActiveChart.Axes(xlCategory, xlPrimary).CategoryType = xlAutomatic
With ActiveChart.Axes(xlCategory)
.HasMajorGridlines = True
.HasMinorGridlines = True
End With
With ActiveChart.Axes(xlValue)
.HasMajorGridlines = True
.HasMinorGridlines = True
End With
ActiveChart.HasLegend = False
ActiveChart.PlotArea.Select
With Selection.Border
.ColorIndex = 16
.Weight = xlThin
.LineStyle = xlContinuous
End With
Selection.Interior.ColorIndex = xlNone
ActiveChart.Axes(xlCategory).Select
Selection.TickLabels.NumberFormat = "0"
ActiveChart.Axes(xlValue).Select
With Selection.Border
.Weight = xlHairline
.LineStyle = xlAutomatic
End With
With Selection
.MajorTickMark = xlOutside
.MinorTickMark = xlOutside
.TickLabelPosition = xlNextToAxis
End With
ActiveChart.Axes(xlValue).Select
With ActiveChart.Axes(xlValue)
.MinimumScale = -50
.MaximumScale = 50
.MinorUnit = 1
.MajorUnit = 5
.Crosses = xlAutomatic
.ReversePlotOrder = False
.ScaleType = xlLinear
End With
ActiveChart.Axes(xlCategory).Select
With ActiveChart.Axes(xlCategory)
.MinimumScale = 0
.MaximumScale = 360
.MinorUnit = 10
.MajorUnit = 90
.Crosses = xlAutomatic
.ReversePlotOrder = False
.ScaleType = xlLinear
End With
ActiveChart.Axes(xlValue).MinorGridlines.Select
With Selection.Border
.ColorIndex = 57
.Weight = xlHairline
.LineStyle = xlDot
End With
ActiveChart.SeriesCollection(1).Select
With Selection.Border
.ColorIndex = 3
.Weight = xlThin
.LineStyle = xlContinuous
End With
With Selection
.MarkerBackgroundColorIndex = 3
.MarkerForegroundColorIndex = 3
.MarkerStyle = xlDiamond
.Smooth = False
.MarkerSize = 5
.Shadow = False
End With
With ActiveChart.PageSetup
.LeftHeader = "&" & Company
.CenterHeader = ""
.LeftFooter = "&" & Title
.CenterFooter = ""
.RightFooter = "Date: " & StartDate
.LeftMargin = Application.InchesToPoints(1.25)
.RightMargin = Application.InchesToPoints(1.25)
.TopMargin = Application.InchesToPoints(1)
.BottomMargin = Application.InchesToPoints(1)
.HeaderMargin = Application.InchesToPoints(0.5)
.FooterMargin = Application.InchesToPoints(0.5)
.ChartSize = xlScreenSize
.CenterHorizontally = True
.CenterVertically = True
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperA4
.FirstPageNumber = xlAutomatic
.BlackAndWhite = False
.Zoom = 100
End With
ActiveChart.ChartArea.Select
End Sub
Explanation / Answer
*In the chart title? If so, make these changes (before and after bolded))_&%
Sub deviationcharts(increment As Integer, Company As String, StartDate As String, TankNum As String, sheetname As String, height As Integer)
to
Sub deviationcharts(increment As Integer, Company As String, StartDate As String, TankNum As String, sheetname As String, height As Double)
And
.ChartTitle.Characters.Text = "Plate deviation at height " & _
(Int(height)) & "m" & Chr(10) & "Tank number " & (Int(TankNum))
To
.ChartTitle.Characters.Text = "Plate deviation at height " & _
Format(height,"0.000") & "m" & Chr(10) & "Tank number " & (Int(TankNum))
But, you may also need to go back and make sure height is actually not an integer to start with, in whatever macro calls the sub.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.