ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Simple question on With command (https://www.excelbanter.com/excel-discussion-misc-queries/214673-simple-question-command.html)

teepee[_3_]

Simple question on With command
 
Hello I was experimenting with changing graph data in VBA and could do with
some help.

Easy enough to do by selecting the graph in question as in fist example
below.
Is there a way of doing it with the With command (which i gather is much
more processor efficient - an important factor in this scenario). My
attempts below, the first way works fine but the second way I'm struggling
on syntax

Any advice gratefully received

tp

Sub changegraph()

ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.PlotArea.Select
ActiveChart.SeriesCollection(2).Values = "=WMP!$BH$4:$BH$304"
End Sub


Sub changegraph2()

With Sheets("sheet1").ChartObjects("Chart 1")
.PlotArea.Select '(I tried omitting this line but no good)
.SeriesCollection(2).Values = "=WMP!$BH$4:$BH$304"
End With
End Sub



Jim Cone[_2_]

Simple question on With command
 

The ChartObject is the container for the Chart.
Also, you don't need to select the PlotArea (i couldn't get that to happen anyway).
So this works...
With Sheets("sheet1").ChartObjects("Chart 1").Chart
.PlotArea.Border.LineStyle = xlDashDot
.SeriesCollection(2).Values = "=WMP!$BH$4:$BH$304"
End With

The With statement is only more efficient when it reduces the number of dots required.
In the above, each line uses two less dots for a net gain (loss) of two.
However, it can save a lot of typing and shorten code lines even without limiting dots.
--
Jim Cone
Portland, Oregon USA




"teepee"
wrote in message
Hello I was experimenting with changing graph data in VBA and could do with
some help.
Easy enough to do by selecting the graph in question as in fist example
below.
Is there a way of doing it with the With command (which i gather is much
more processor efficient - an important factor in this scenario). My
attempts below, the first way works fine but the second way I'm struggling
on syntax
Any advice gratefully received
tp

Sub changegraph()
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.PlotArea.Select
ActiveChart.SeriesCollection(2).Values = "=WMP!$BH$4:$BH$304"
End Sub

Sub changegraph2()
With Sheets("sheet1").ChartObjects("Chart 1")
.PlotArea.Select '(I tried omitting this line but no good)
.SeriesCollection(2).Values = "=WMP!$BH$4:$BH$304"
End With
End Sub

teepee[_3_]

Simple question on With command
 

"Jim Cone" wrote

Thaaks very much Jim. Much appreciated




All times are GMT +1. The time now is 07:04 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com