Vba 获取图表轴值

Vba 获取图表轴值,vba,excel,Vba,Excel,我意识到,您可以使用VBA设置图形的轴 .MaximumScale = .MinimumScale = 有没有办法获取轴值? 我这样做是因为这样可以更容易地自动化获取图表轴的过程,然后在其中添加一个月(而无需将图形轴设置为自动) 附言 我录制了一个改变轴日期的宏,它将日期值设置为数字,如40148或41609。这是什么?请尝试逐步阅读以下代码片段。它显示了如何查找Y轴值以及如何更改Y轴值。看看里面的一些评论 第一次尝试嵌入工作表中的图表 Sub test_chart() 'get the

我意识到,您可以使用VBA设置图形的轴

.MaximumScale = 
.MinimumScale = 
有没有办法获取轴值?

我这样做是因为这样可以更容易地自动化获取图表轴的过程,然后在其中添加一个月(而无需将图形轴设置为自动)

附言


我录制了一个改变轴日期的宏,它将日期值设置为数字,如40148或41609。这是什么?

请尝试逐步阅读以下代码片段。它显示了如何查找Y轴值以及如何更改Y轴值。看看里面的一些评论

第一次尝试嵌入工作表中的图表

Sub test_chart()

'get the chart for activesheet
    Dim myCHR As Chart
    Set myCHR = ActiveSheet.ChartObjects(1).Chart

'get Y axis of the chart
    Dim myYA As Axis
    Set myYA = myCHR.Axes(XlAxisType.xlValue)

'get the value
    Debug.Print myYA.MaximumScale
    Debug.Print myYA.MinimumScale

'the same in almost one line
    With ActiveSheet.ChartObjects(1).Chart.Axes(xlValue)
        Debug.Print .MaximumScale
        Debug.Print .MinimumScale
        'change the value
        .MaximumScale = 10
    End With
End Sub
Sub test_sheet_chart()

'get the chart for activesheet
    Dim myCHR As Chart
    Set myCHR = Sheets("All SIN 10 Pubs - Unique Users")

'get Y axis of the chart
    Dim myYA As Axis
    Set myYA = myCHR.Axes(XlAxisType.xlValue)

'get the value
    Debug.Print myYA.MaximumScale
    Debug.Print myYA.MinimumScale

'the same in almost one line
    With Sheets("All SIN 10 Pubs - Unique Users").Axes(xlValue)
        Debug.Print .MaximumScale
        Debug.Print .MinimumScale
        'change the value
        .MaximumScale = 10
    End With
End Sub
第二次尝试将图表作为单独的表格

Sub test_chart()

'get the chart for activesheet
    Dim myCHR As Chart
    Set myCHR = ActiveSheet.ChartObjects(1).Chart

'get Y axis of the chart
    Dim myYA As Axis
    Set myYA = myCHR.Axes(XlAxisType.xlValue)

'get the value
    Debug.Print myYA.MaximumScale
    Debug.Print myYA.MinimumScale

'the same in almost one line
    With ActiveSheet.ChartObjects(1).Chart.Axes(xlValue)
        Debug.Print .MaximumScale
        Debug.Print .MinimumScale
        'change the value
        .MaximumScale = 10
    End With
End Sub
Sub test_sheet_chart()

'get the chart for activesheet
    Dim myCHR As Chart
    Set myCHR = Sheets("All SIN 10 Pubs - Unique Users")

'get Y axis of the chart
    Dim myYA As Axis
    Set myYA = myCHR.Axes(XlAxisType.xlValue)

'get the value
    Debug.Print myYA.MaximumScale
    Debug.Print myYA.MinimumScale

'the same in almost one line
    With Sheets("All SIN 10 Pubs - Unique Users").Axes(xlValue)
        Debug.Print .MaximumScale
        Debug.Print .MinimumScale
        'change the value
        .MaximumScale = 10
    End With
End Sub

这对你有用吗?我在“Set myCHR=ActiveSheet.ChartObjects(1.Chart)”上收到一个“应用程序定义的或对象定义的错误”问题-活动表上是否有浮动的图表?或者你有一张图表(图表就是表格本身)?表格就是图表。这是录制的宏在选择图表:工作表(“所有10家酒吧-唯一用户”)时的外观。选择ActiveChart.ChartArea。Select@JakkyD,请参阅我答案中的第二个解决方案。40148和41609是数字格式的日期。这些数字分别对应于2009年12月1日和2013年12月1日。您可以通过更改单元格的格式和到目前为止的数字来发现这一点。