如何设置图表轴上的标签对齐方式?

发布于 2024-07-11 09:39:06 字数 165 浏览 8 评论 0原文

我必须仅使用 VBA 在 Excel 中制作柱形图(无需用户输入)。

我想格式化 x 轴的标签,以便每个标签的对齐变为 -270 度。 这可以通过更改“格式轴”对话框的“对齐”选项卡中的“自定义角度”属性来手动完成。

我录制了一个宏,但 Excel 似乎没有记录对齐步骤。

I have to make column charts in Excel using VBA only (no user input).

I want to format the labels of the x-axis so that the alignment for every label becomes -270 degrees. This can be done manually by changing the "Custom angle" property in the "Alignment" tab of the "Format Axis" Dialog.

I recorded a macro but Excel does not seem to be recording the alignment step.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

马蹄踏│碎落叶 2024-07-18 09:39:06

如果您使用的是 Excel 2007,请尝试使用早期版本,因为 2007 的宏记录器有点残缺。

这就是我得到的:

ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.Axes(xlCategory).Select
Selection.TickLabels.Orientation = xlUpward

If you are using Excel 2007, try using an earlier version because 2007's macro recorder is a bit crippled.

This is what I got:

ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.Axes(xlCategory).Select
Selection.TickLabels.Orientation = xlUpward
强辩 2024-07-18 09:39:06
ActiveChart.Axes(xlCategory).TickLabels.Orientation = 67
ActiveChart.Axes(xlCategory).TickLabels.Orientation = 67
琉璃繁缕 2024-07-18 09:39:06

关于.Activate

老派的 VBA 宏经常会让人们认为您必须“激活”并反对使其成为“当前”。 就像您单击图形表示一样,您使用鼠标和键盘所做的一切都会被激活的对象接收。 Microsoft Office 产品的“宏生成器”是这种编码风格的罪魁祸首。

然而,在 VBA 编程或使用 C# 或其他流行语言的 Office API 的世界中,这不是应该做的事情。

要修改图表对象、线条等,您可以通过其对象引用对对象本身进行操作。 一旦获得它,您就可以访问所有方法和属性,并且“激活”不是这个概念的一部分。 如果由于某种原因您实际上希望让对象获得一些焦点以便用户看到它 - 视觉对象通常有一个“Activate()”函数可以调用。

在此示例中,图表中多个系列的线宽设置为 1:

Sub Change_all_charts()
    Dim ch As chart
    Dim ws As Worksheet
    Dim ChtObj As ChartObject
    Dim srs As Series

    'Get a reference to the active worksheet
    Set ws = ActiveSheet

    'Loop each chart object in worksheet ws
    For Each ChtObj In ws.ChartObjects

        'Operate on 'ChtObj.chart' object using . notation
        With ChtObj.chart
            Debug.Print "Chart being modified: '" & .Name & "'"
            'Loop through all series in the chart .Series collection
            For Each srs In .SeriesCollection
                Debug.Print "  Series being modified: '" & srs.Name & "'"
                'Set the series line width
                srs.Format.Line.Weight = 1
            Next
        End With
    Next
End Sub

在过去(甚至仍然),财务人员进行 Excel 编程,并使他们的工作表疯狂地闪烁和动画,因为他们认为和对象需要接收“焦点” ” 进行修改。

好吧,现在有人这么说,因为它在过去 40 年里一直“困扰”我。

About .Activate

Old school VBA macros often lead people to think you have to "activate" and object to make it "current". Like you click a graphical representation and everything you do with mouse and keyboard is received by the activated object. The "macro generator" for the Microsoft Office products is to blame for this coding style.

However, in the world of VBA programming or using the Office API;s from C# or other popular languages — that is not how it should be done.

To modify a chart object, line etc, you can operate on the object itself by its object reference. Once you obtained it you can access all the methods and properties and "activation" is not part of the concept. If you by some reason actually want to have the object receive some focus for the user to see it — visual objects normally have an "Activate()" function to call.

In this example the line width of multiple series in a chart is set to 1:

Sub Change_all_charts()
    Dim ch As chart
    Dim ws As Worksheet
    Dim ChtObj As ChartObject
    Dim srs As Series

    'Get a reference to the active worksheet
    Set ws = ActiveSheet

    'Loop each chart object in worksheet ws
    For Each ChtObj In ws.ChartObjects

        'Operate on 'ChtObj.chart' object using . notation
        With ChtObj.chart
            Debug.Print "Chart being modified: '" & .Name & "'"
            'Loop through all series in the chart .Series collection
            For Each srs In .SeriesCollection
                Debug.Print "  Series being modified: '" & srs.Name & "'"
                'Set the series line width
                srs.Format.Line.Weight = 1
            Next
        End With
    Next
End Sub

In the old days (or even still) financial people do Excel programming and make their worksheets flash and animate like crazy because they think and object needs to receive "focus" to be modified.

Well now it has been said as it has been "bothering" me for the last 40 years.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文