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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/412750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:33:01  来源:igfitidea点击:

How to set label alignment on chart axes?

excelvbachartsaxis

提问by Kshitij

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

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

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.

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

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

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

采纳答案by Artelius

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

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

This is what I got:

这是我得到的:

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

回答by Kyle

ActiveChart.Axes(xlCategory).TickLabels.Orientation = 67

回答by flodis

About .Activate

关于.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 Officeproducts is to blame for this coding style.

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

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.

但是,在 VBA 编程的世界中,或者使用来自 C# 或其他流行语言的 Office API,这不是应该的。

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.

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

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

在此示例中,图表中多个系列的线宽设置为 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.

在过去(甚至现在),财务人员进行 Excel 编程并使他们的工作表像疯了一样闪烁和动画,因为他们认为和对象需要获得“焦点”才能进行修改。

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

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