VBA四舍五入到最接近的1E-n

时间:2018-03-13 10:47:59

标签: excel vba rounding

我在excel中使用VBA创建了一个散点图。 Y轴具有对数缩放,因为我的数据范围中的值(300个数据范围,每个数据点有几十万个数据点)从1到1E-10不等。

有没有办法自动缩放Y轴?由于图表与图表的最大值可能在1到1E-5之间变化。

如果没有,是否有办法向最接近的1E-n舍入?所以我可以使用下面的代码扩展我的图表。

MyChart.Axes(xlValue).MaximumScale = "round up max value from my data range"

提前致谢

查理

For cond = 2 To wb.Worksheets.Count
                    Set ws = wb.Sheets(cond)
                    wsn = ws.Name

                    With ws
                        'Includes "Title" cell
                        Set ttl = .Cells(.Columns(1).Find(what:="Title", after:=Cells(1, 1)).Row, 1)
                        Set ttl2 = .Cells(ttl.Row, .UsedRange.Columns.Count)
                        Set rng1 = .Range(ttl, ttl2)
                        Set Data = .Cells(.Columns(1).Find(what:="*Pa)*", after:=Cells(1, 1)).Row + 2, 1)
                        Set Data2 = .Cells(.UsedRange.Rows.Count, .UsedRange.Columns.Count)
                        Set rng2 = .Range(Data, Data2)
                        myrng = Union(rng1, rng2).Address
                    End With

                    ws.Shapes.AddChart.Name = (wsn)
                    ws.Shapes(wsn).Chart.ChartType = xlXYScatterLinesNoMarkers
                    Set MyChart = ws.Shapes(wsn).Chart
                    MyChart.SetSourceData Source:=ws.Range(myrng), PlotBy:=xlColumns

                    MyChart.ApplyLayout (1)
                    MyChart.ChartTitle.Text = Title & " " & wsn
                    MyChart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Pressure "
                    MyChart.Axes(xlCategory, xlPrimary).AxisTitle.Text = "Output”

                    MyChart.Axes(xlValue).ScaleType = xlLogarithmic
                    MyChart.Axes(xlValue).TickLabels.NumberFormat = "0.0E+00"
                    MyChart.Axes(xlValue).CrossesAt = 0.000000001
                    MyChart.Axes(xlValue).MaximumScale = “needs automating”

                    MyChart.Axes(xlCategory).ScaleType = xlLogarithmic
                    MyChart.Axes(xlCategory).MaximumScale = 10000
                    ws.ChartObjects(wsn).Left = ws.Range("A1").Left
                    ws.ChartObjects(wsn).Top = ws.Range("A1").Top

                    ws.ChartObjects(wsn).Height = 400
                    ws.ChartObjects(wsn).Width = 1200

                    MyChart.Axes(xlCategory, xlPrimary).HasMajorGridlines = True
                    MyChart.Axes(xlCategory, xlPrimary).HasMinorGridlines = True

                    ws.ChartObjects(wsn).Chart.Legend.Left = 1000
                    ws.ChartObjects(wsn).Chart.Legend.Width = 190
                    ws.ChartObjects(wsn).Chart.Legend.Top = 17.5
                    ws.ChartObjects(wsn).Chart.Legend.Height = 360
                    ws.ChartObjects(wsn).Chart.PlotArea.Width = 975

                Next cond

1 个答案:

答案 0 :(得分:0)

我还没试过这个,但是怎么样:

With MyChart.Axes(xlValue) 
 .MinimumScaleIsAuto = True 
 .MaximumScaleIsAuto = True 
End With