根据工作表中的位置(VBA)选择图表对象

时间:2016-07-22 06:59:47

标签: vba charts

是否有可能通过其确切位置处理Excel工作表中的图表对象?谷歌到目前为止没有任何帮助,所以任何意见都表示赞赏。

祝你好运, 斯蒂芬

1 个答案:

答案 0 :(得分:0)

您可以使用TopLeftCellBottomRightCell查找给定范围之间的图表。

Sub testChart()
 Dim oChart As Chart

 '/ Selecet the chart , if found in Range D4:F10
 Set oChart = findChart(Sheet1.Range("D4:F10"))
 If Not oChart Is Nothing Then
    oChart.Parent.Select
 Else
    MsgBox "No chart found."
 End If

End Sub

Function findChart(ByVal rngChart As Range) As Chart

    Dim oChart      As Chart
    Dim oChartObj   As ChartObject
    Dim wksChart    As Worksheet
    Dim rngCArea    As Range

    Set wksChart = rngChart.Parent

    For Each oChartObj In wksChart.ChartObjects
         Set rngCArea    = Intersect(wksChart.Range(rngChart.Address), wksChart.Range(oChartObj.TopLeftCell, oChartObj.BottomRightCell))
         If Not rngCArea Is Nothing Then
          Set oChart = oChartObj.Chart
         End If
    Next

    Set findChart = oChart

End Function