我正在编写一个代码,它使用excel中的宏来开发图表。图表的输入是不同的变量。Excel会自动调整轴,但我想修改代码,例如,如果输入序列大于1000000 (100万),则轴将更改为百万,并显示为“1”而不是1000000
'This is the code i'm using to generate the chart
'counting total rows
lr = ActiveSheet.Cells(Rows.count, 1).End(xlUp).Row
'counting total columns
lc = ActiveSheet.Cells(1, Columns.count).End(xlToLeft).Column
Set chtRng = Range(Cells(1, 1), Cells(lr, LC))
Worksheets("sheet1").Activate
'Deleting the current chart (PS: If you could suggest a way to update the
'input values and refresh the chart rather than deleting the chart and 'adding a new one)
For Each chtObj In ActiveSheet.ChartObjects
chtObj.Delete
Next
'Adding new chart
With ActiveSheet.Shapes
.AddChart2(227, xlLine).Select
End With
With ActiveChart
'.Axes(xlValue).DisplayUnit = xlMillions
.SetElement (msoElementLegendBottom)
With ActiveChart.Parent
.Height = 250
.Width = 450
.Top = 20 ' reposition
.Left = 280 ' reposition
End With
End With
'I tried using the following code but it doesn't work.
If xlValue >= 1000000 Then
With ActiveChart
.Axes(xlValue).DisplayUnit = xlMillions
End With
Else
End If
任何建议都很受欢迎。
发布于 2019-07-23 22:19:05
您可以使用以下命令更改axe格式:
ActiveChart.Axes(xlValue).TickLabels.NumberFormat = "##,###0,,\M"
因此,您可以将代码的末尾替换为:
maxvalue=Application.WorksheetFunction.Max( chtRng)
If maxvalue>= 1000000 Then ActiveChart.Axes(xlValue).TickLabels.NumberFormat ="##,###0,,\M"
有关axis格式的更多信息,请访问此链接:
希望这能有所帮助
https://stackoverflow.com/questions/57164481
复制相似问题