Vba - Selecting Cells Around A Selected ChartObject - Stack Overflow
Vba - Selecting Cells Around A Selected ChartObject - Stack Overflow
I am totally new to VBA so sorry if this is a simple question. I have this problem at the
moment: I have a worksheet with about 30 graphs in it. The problem is that the headers and
1 sources at the bottom are in cells, not part of the graph. The company has accounted for this
when formatting the "graph" region (there is a company wide standard format macro). I have
tried the snippet below but as expected that only selects the actual graph regions. Is there a
way to select the two rows above and below each of the graphs and iterate through that?
1
Thanks
Sub SelectAll()
ActiveSheet.ChartObjects.Select
End Sub
Edit: This is what I'm currently working with, the chart in question is in Column T and Row 9 so
going negative shouldn't be a worry.
Sub SelectAll()
Worksheets("Real Estate").Activate
MsgBox ActiveSheet.ChartObjects("Chart 1").TopLeftCell.Address
With ActiveSheet
.Range(.ChartObjects("Chart 1").TopLeftCell.Offset(-2, 0), _
.ChartObjects("Chart 1").BottomLeftCell.Offset(4, 0)).Select
End With
End Sub
vba excel
You can return the TopLeftCell and BottomRightCell of the chart object and OFFSET from
those cells.
0
For example:
ThisWorkbook.Worksheets("Sheet1").ChartObjects("Chart 1").BottomRightCell
ThisWorkbook.Worksheets("Sheet1").ChartObjects("Chart 1").TopLeftCell
https://stackoverflow.com/questions/51018394/selecting-cells-around-a-selected-chartobject 1/2
10/7/2020 vba - Selecting Cells Around a Selected ChartObject - Stack Overflow
These two cells can then be used to return the range surrounding the chart:
Sub Test()
With ThisWorkbook.Worksheets("Sheet1")
.Range(.ChartObjects("Chart 1").TopLeftCell.Offset(-2, -2), _
.ChartObjects("Chart 1").BottomRightCell.Offset(2, 2)).Select
End With
End Sub
I can't quite seem to get it working, this makes logical sense to me but I keep getting 'Object doesn't
support this property or method' I assume I just am not putting in my slightly different names for things
correctly. My sheet is named "Real Estate" and the chart is "Chart 1" – Chris Novak Jun 25 '18 at 9:01
I fixed the earlier issue, although now I'm getting Method 'Offset' of object 'Range' failed. –
Chris Novak Jun 25 '18 at 9:35
@ChrisNovak - Sorry, wasn't getting notifications that you'd replied, add my name with the @ so I get a
notification to any comments. To find the chart name select the chart and look on the Layout ribbon
under Properties. If your chart is in column A or B, or in rows 1 or 2 you'll get problems as you can't
offset by -2 cells which would take you off the sheet. Could you edit your question with your full code
please - I get an Application-defined or object-defined error when trying to offset off the sheet,
so not sure how you're coding it. – Darren Bartrup-Cook Jun 25 '18 at 9:49
@DarrenBartrupCook Apologies, must've not done that @ correctly before – Chris Novak Jun 25 '18
at 10:06
No problem - shouldn't have to do it if you commenting on my post, so not sure what happened. Your
code says BottomLeftCell it should be BottomRightCell . – Darren Bartrup-Cook Jun 25 '18 at
10:16
I changed that, same error code though "Method 'Offset' of object 'Range' failed – Chris Novak Jun 26
'18 at 1:01
I'm not sure then - your SelectAll code works fine for me as long as I use
.Range(.ChartObjects("Chart 1").TopLeftCell.Offset(-2, 0), ("Chart
1").BottomRightCell.Offset(4, 0)).Select and have the chart below row 2. Does the message box
return an address? If you change BottomRightCell to TopLeftCell does it select a range - the
wrong range, but a range? – Darren Bartrup-Cook Jun 26 '18 at 8:07
https://stackoverflow.com/questions/51018394/selecting-cells-around-a-selected-chartobject 2/2