Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.charting
Xavier
 
Posts: n/a
Default Problem with empty cells when creating chart using vba

Hello,

I am facing difficulties to create a chart from a sheet containing empty
cells (I use Excel 2003 SP2). My sheet looks similar to this:

A B C

1 0.1

2 0.2 25 34

3 0.3 23 27

4 0.4 24 6

5 0.5 25 16

6 0.6 27 21

7 0.7


Column A contains data (some depth in my case). Each cell in column A
contains data. Column B and C contain some measurements made at the depth
indicated in column A. I need to combine two graphics on one chart: the
content of B in function of A and the content of C in function of A.

The following code works fine:


With Graph

.ChartType = xlLine
.SetSourceData Source:=Sheets(MainSheet).Range("B2:C6"),
PlotBy:=xlColumns
.SeriesCollection(1).XValues = Sheets(MainSheet).Range("A2:A6")

(...)


The result of this code is a chart containing two lines, for X values rangin
from 0.2 to 0.6. Now, what I really need is to have the X values ranging
from 0.1 to 0.7 and keep the lines displayed for values 0.2 - 0.6. So I
change my code to:


With Graph

.ChartType = xlLine
.SetSourceData Source:=Sheets(MainSheet).Range("B1:C7"),
PlotBy:=xlColumns
.SeriesCollection(1).XValues = Sheets(MainSheet).Range("A1:A7")

(...)


And in this case, only ONE line is displayed (values of column C) ???

I have been working on this for two nights and this is driving me nuts.

Any help would be appreciated.


Thanks,

Xavier


  #2   Report Post  
Posted to microsoft.public.excel.charting
Andy Pope
 
Posts: n/a
Default Problem with empty cells when creating chart using vba

Hi,

Excel is trying to guess your data layout and empty cells do not help.
Try using the Formula property of a series.

Sub X
Dim Graph As Chart
Dim MainSheet As String
Dim strXValues As String
Dim strValues As String

MainSheet = "Sheet2"
Set Graph = ActiveSheet.ChartObjects(1).Chart

With Graph
.ChartType = xlLine
'.SetSourceData Source:=Sheets(MainSheet).Range("A2:C7"), _
PlotBy:=xlColumns
.SeriesCollection.NewSeries
strXValues = "'" & MainSheet & "'!" & _
Sheets(MainSheet).Range("A2:A7").Address
strValues = "'" & MainSheet & "'!" & _
Sheets(MainSheet).Range("B2:B7").Address
.SeriesCollection(1).Formula = "=SERIES(," & _
strXValues & "," & strValues & ",1)"
.SeriesCollection.NewSeries
strValues = "'" & MainSheet & "'!" & _
Sheets(MainSheet).Range("C2:C7").Address
.SeriesCollection(2).Formula = "=SERIES(," & _
strXValues & "," & strValues & ",2)"
End With
End sub

Cheers
Andy

Xavier wrote:
Hello,

I am facing difficulties to create a chart from a sheet containing empty
cells (I use Excel 2003 SP2). My sheet looks similar to this:

A B C

1 0.1

2 0.2 25 34

3 0.3 23 27

4 0.4 24 6

5 0.5 25 16

6 0.6 27 21

7 0.7


Column A contains data (some depth in my case). Each cell in column A
contains data. Column B and C contain some measurements made at the depth
indicated in column A. I need to combine two graphics on one chart: the
content of B in function of A and the content of C in function of A.

The following code works fine:


With Graph

.ChartType = xlLine
.SetSourceData Source:=Sheets(MainSheet).Range("B2:C6"),
PlotBy:=xlColumns
.SeriesCollection(1).XValues = Sheets(MainSheet).Range("A2:A6")

(...)


The result of this code is a chart containing two lines, for X values rangin
from 0.2 to 0.6. Now, what I really need is to have the X values ranging
from 0.1 to 0.7 and keep the lines displayed for values 0.2 - 0.6. So I
change my code to:


With Graph

.ChartType = xlLine
.SetSourceData Source:=Sheets(MainSheet).Range("B1:C7"),
PlotBy:=xlColumns
.SeriesCollection(1).XValues = Sheets(MainSheet).Range("A1:A7")

(...)


And in this case, only ONE line is displayed (values of column C) ???

I have been working on this for two nights and this is driving me nuts.

Any help would be appreciated.


Thanks,

Xavier



--

Andy Pope, Microsoft MVP - Excel
http://www.andypope.info
  #3   Report Post  
Posted to microsoft.public.excel.charting
Xavier
 
Posts: n/a
Default Problem with empty cells when creating chart using vba

Hi Andy,

Thanks a lot for your input, it works beautifully.
Do you have any pointer describing the technique you suggested (and why) ?

Thanks again,

Xavier


"Andy Pope" wrote in message
...
Hi,

Excel is trying to guess your data layout and empty cells do not help.
Try using the Formula property of a series.

Sub X
Dim Graph As Chart
Dim MainSheet As String
Dim strXValues As String
Dim strValues As String

MainSheet = "Sheet2"
Set Graph = ActiveSheet.ChartObjects(1).Chart

With Graph
.ChartType = xlLine
'.SetSourceData Source:=Sheets(MainSheet).Range("A2:C7"), _
PlotBy:=xlColumns
.SeriesCollection.NewSeries
strXValues = "'" & MainSheet & "'!" & _
Sheets(MainSheet).Range("A2:A7").Address
strValues = "'" & MainSheet & "'!" & _
Sheets(MainSheet).Range("B2:B7").Address
.SeriesCollection(1).Formula = "=SERIES(," & _
strXValues & "," & strValues & ",1)"
.SeriesCollection.NewSeries
strValues = "'" & MainSheet & "'!" & _
Sheets(MainSheet).Range("C2:C7").Address
.SeriesCollection(2).Formula = "=SERIES(," & _
strXValues & "," & strValues & ",2)"
End With
End sub

Cheers
Andy

Xavier wrote:
Hello,

I am facing difficulties to create a chart from a sheet containing empty
cells (I use Excel 2003 SP2). My sheet looks similar to this:

A B C

1 0.1

2 0.2 25 34

3 0.3 23 27

4 0.4 24 6

5 0.5 25 16

6 0.6 27 21

7 0.7


Column A contains data (some depth in my case). Each cell in column A
contains data. Column B and C contain some measurements made at the depth
indicated in column A. I need to combine two graphics on one chart: the
content of B in function of A and the content of C in function of A.

The following code works fine:


With Graph

.ChartType = xlLine
.SetSourceData Source:=Sheets(MainSheet).Range("B2:C6"),
PlotBy:=xlColumns
.SeriesCollection(1).XValues = Sheets(MainSheet).Range("A2:A6")

(...)


The result of this code is a chart containing two lines, for X values
rangin from 0.2 to 0.6. Now, what I really need is to have the X values
ranging from 0.1 to 0.7 and keep the lines displayed for values 0.2 -
0.6. So I change my code to:


With Graph

.ChartType = xlLine
.SetSourceData Source:=Sheets(MainSheet).Range("B1:C7"),
PlotBy:=xlColumns
.SeriesCollection(1).XValues = Sheets(MainSheet).Range("A1:A7")

(...)


And in this case, only ONE line is displayed (values of column C) ???

I have been working on this for two nights and this is driving me nuts.

Any help would be appreciated.


Thanks,

Xavier


--

Andy Pope, Microsoft MVP - Excel
http://www.andypope.info



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
How to make Excel Chart not to display empty cells? Marko Pinteric Excel Discussion (Misc queries) 6 April 5th 06 09:03 PM
Creating a line chart with two X-axes!! JaVaughn Excel Discussion (Misc queries) 1 June 30th 05 01:36 PM
Counting empty cells within a range of cells Rosehill - ExcelForums.com New Users to Excel 2 May 2nd 05 08:53 AM
Counting empty cells within a range of cells Rosehill - ExcelForums.com New Users to Excel 0 April 7th 05 12:47 AM
How to make empty cells as zero in excel add-ins for SQL Server an Microlong Excel Worksheet Functions 0 January 12th 05 06:31 AM


All times are GMT +1. The time now is 04:31 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"