Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 51
Default problem with VBA to draw vertical lines

I have an X-Y scatter plot as an embedded chart. I have a list of X & Y
values that I would like to use to draw vertical lines on the chart. The
range for x-series is AB10:AC33, and I would like to use the same values to
define the y-values (AD10:AE10). I started by recording a macro for adding
one series to the chart, then tried to generalize using a for...next loop for
adding all 24. This of course did not work. Here is my code:
Private Sub combobox_2_click()

Dim i As Integer
Dim num As Integer
Dim myRange As Range

If Range("focus_choice") = "All" Then

Application.ScreenUpdating = False
Worksheets(2).Activate
Set myRange = Range("chr_start")
num = ActiveSheet.ChartObjects(1).Chart.SeriesCollection .count


For i = 1 To 24
With ActiveSheet
..ChartObjects(1).Chart.SeriesCollection.NewSeries
..ChartObjects(1).Chart.SeriesCollection(1).Series (num +
1).XValues = myRange.Offset(i, 0)
..ChartObjects(1).Chart.SeriesCollection(1).Series (num +
1).Values = Worksheets(2).Range(Cells(30, 10), Cells(31, 10))
End With

With
ActiveSheet.ChartObjects(1).Chart.SeriesCollection (1).Series(num + 1).Border
..ColorIndex = vbWhite
..LineStyle = xlContinuous
End With

With
ActiveSheet.ChartObjects(1).Chart.SeriesCollection (1).Series(num + 1)
..MarkerStyle = xlNone
End With

Next i

End If

Application.ScreenUpdating = True

End Sub


This does not work because I get an error message that says the "object
doesn't support this method or property" for the second assignment after the
for i = 1 to 24 (.ChartObjects(1).Chart.SeriesCollection(1).Series (num +
1).XValues = myRange.Offset(i, 0)). I know this can be done, but I need
help, so ... please help.

Thanks in advance,

Warren
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 747
Default problem with VBA to draw vertical lines

The best way IMO to add vertical lines to a chart is to add a series where
the Y-values are all set to the desired maximum value and error bars are set
to 100% in the Y-direction.

Alternatively, each vertical line can be a separate series having two points
each where the X-values of each series (i.e. each bar) are the same and the
Y-values are respectively 0 and the desired max value. The X-values must of
course be different between series. Examples:
Series 1, Point 1: X = 5, Y = 0
Series 1, Point 2: X = 5, Y = 100
Series 2, Point 1: X = 7, Y = 0
Series 2, Point 2: X = 7, Y = 100
etc...

The following demos the first option. Required is that the X- and Y-Value
ranges be named "XVals" and "YVals" respectively.

Sub MakeVertLines()
Dim ws As Worksheet
Dim rng As Range, rng2 As Range
Dim s As Series

Set ws = Worksheets(2)
Set rng = ws.Range("XVals")
Set rng2 = ws.Range("YVals")
With ws.ChartObjects(1).Chart
Set s = .SeriesCollection.NewSeries
End With
s.XValues = rng
s.Values = rng2
s.MarkerStyle = xlNone
s.ErrorBar Direction:=xlY, Include:=xlMinusValues, _
Type:=xlPercent, Amount:=100
s.ErrorBars.Border.ColorIndex = 3

End Sub

Regards,
Greg


"Wazooli" wrote:

I have an X-Y scatter plot as an embedded chart. I have a list of X & Y
values that I would like to use to draw vertical lines on the chart. The
range for x-series is AB10:AC33, and I would like to use the same values to
define the y-values (AD10:AE10). I started by recording a macro for adding
one series to the chart, then tried to generalize using a for...next loop for
adding all 24. This of course did not work. Here is my code:
Private Sub combobox_2_click()

Dim i As Integer
Dim num As Integer
Dim myRange As Range

If Range("focus_choice") = "All" Then

Application.ScreenUpdating = False
Worksheets(2).Activate
Set myRange = Range("chr_start")
num = ActiveSheet.ChartObjects(1).Chart.SeriesCollection .count


For i = 1 To 24
With ActiveSheet
.ChartObjects(1).Chart.SeriesCollection.NewSeries
.ChartObjects(1).Chart.SeriesCollection(1).Series( num +
1).XValues = myRange.Offset(i, 0)
.ChartObjects(1).Chart.SeriesCollection(1).Series( num +
1).Values = Worksheets(2).Range(Cells(30, 10), Cells(31, 10))
End With

With
ActiveSheet.ChartObjects(1).Chart.SeriesCollection (1).Series(num + 1).Border
.ColorIndex = vbWhite
.LineStyle = xlContinuous
End With

With
ActiveSheet.ChartObjects(1).Chart.SeriesCollection (1).Series(num + 1)
.MarkerStyle = xlNone
End With

Next i

End If

Application.ScreenUpdating = True

End Sub


This does not work because I get an error message that says the "object
doesn't support this method or property" for the second assignment after the
for i = 1 to 24 (.ChartObjects(1).Chart.SeriesCollection(1).Series (num +
1).XValues = myRange.Offset(i, 0)). I know this can be done, but I need
help, so ... please help.

Thanks in advance,

Warren

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
draw vertical line to mark today in diagram Florian Demmer Excel Discussion (Misc queries) 1 January 22nd 08 01:31 PM
VBA to draw lines of SPC Daniel Charts and Charting in Excel 4 July 31st 07 08:44 PM
Vertical Lines on the Lines on 2 Axes Chart [email protected] Charts and Charting in Excel 3 March 3rd 06 04:14 AM
Draw lines in a userform gbottesi Excel Programming 3 July 8th 04 05:20 PM
How do I draw lines..?? MIKEB Excel Programming 4 October 24th 03 12:22 AM


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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"