首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果序列中的值大于1000000,则使用宏调整图表的y轴

如果序列中的值大于1000000,则使用宏调整图表的y轴
EN

Stack Overflow用户
提问于 2019-07-23 20:44:18
回答 1查看 31关注 0票数 0

我正在编写一个代码,它使用excel中的宏来开发图表。图表的输入是不同的变量。Excel会自动调整轴,但我想修改代码,例如,如果输入序列大于1000000 (100万),则轴将更改为百万,并显示为“1”而不是1000000

代码语言:javascript
运行
复制
'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

任何建议都很受欢迎。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-23 22:19:05

您可以使用以下命令更改axe格式:

代码语言:javascript
运行
复制
ActiveChart.Axes(xlValue).TickLabels.NumberFormat = "##,###0,,\M"

因此,您可以将代码的末尾替换为:

代码语言:javascript
运行
复制
maxvalue=Application.WorksheetFunction.Max( chtRng) 

If maxvalue>= 1000000 Then  ActiveChart.Axes(xlValue).TickLabels.NumberFormat ="##,###0,,\M"

有关axis格式的更多信息,请访问此链接:

https://www.excelanytime.com/excel/index.php?option=com_content&view=article&id=121:custom-number-formats-date-a-time-formats-in-excel-a-vba-numberformat-property&catid=79&Itemid=475

希望这能有所帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57164481

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档