W
waveracerr
I have the following line of code, most of it simply copied from various
internet souces except for the line of code concerning the legend
(towards the bottom of the code, indicated with a row of dashes above
and below said line of code). The macro works great, however it is
hampered by the apparent need to still create a legend for each chart.
Basically I can physically see the macro creating a chart and adding a
legend. The macro then creates the next chart and adds a legend to the
new chart which includes the previous series. By the time the macro
has reached the last column to be charted the corresponding chart takes
forever as a legend is built to include a reference to each previous
series. Finally the code goes through each chart and removes the
legend and everything comes out fine. Ultimately I am just looking to
speed up the process by having the code never create a legend in the
first place.
Also, Any idea on how to alter the code such that the chart location is
a new worksheet rather than being placed within the current worksheet?
Thanks for any advice!
P.S. For all professors, etc. I promise I have my code properly
indented and with more descriptive comments
Option Explicit
Sub MultiX_OneY_Chart()
Dim rngDataSource As Range
Dim iDataRowsCt As Long
Dim iDataColsCt As Integer
Dim iSrsIx As Integer
Dim chtChart As CHART
Dim srsNew As Series
Dim i As Integer
i = 2
Dim j As Integer
Dim numrow As Integer
numrow = 0
Dim numcol As Integer
numcol = 0
Cells(2, 3).Select
Do While Not IsEmpty(ActiveCell)
numrow = numrow + 1
ActiveCell.Offset(1, 0).Select
Loop
numrow = numrow + 1
Cells(6, 1).Select
Do While Not IsEmpty(ActiveCell)
numcol = numcol + 1
ActiveCell.Offset(0, 1).Select
Loop
For j = 3 To numcol
If Not TypeName(Selection) = "Range" Then
' Doesn't work if no range is selected
MsgBox "Please select a data range and try again.", _
vbExclamation, "No Range Selected"
Else
Set rngDataSource = Range(Cells(1, i), Cells(numrow, j))
'Selection
With rngDataSource
iDataRowsCt = .Rows.Count
iDataColsCt = .Columns.Count
End With
' Create the chart
Set chtChart = ActiveSheet.ChartObjects.Add( _
Left:=ActiveSheet.Columns(ActiveWindow.ScrollColumn).Left + _
ActiveWindow.Width / 4, _
Width:=ActiveWindow.Width / 2, _
Top:=ActiveSheet.Rows(ActiveWindow.ScrollRow).Top + _
ActiveWindow.Height / 4, _
Height:=ActiveWindow.Height / 2).CHART
With chtChart
For iSrsIx = 1 To iDataColsCt - 1
' Add each series
Set srsNew = .SeriesCollection.NewSeries
With srsNew
Name = rngDataSource.Cells(1, iSrsIx)
Values = rngDataSource.Cells(2, iDataColsCt)
_
Resize(iDataRowsCt - 1, 1)
XValues = rngDataSource.Cells(2, iSrsIx) _
Resize(iDataRowsCt - 1, 1)
End With
With chtChart
HasTitle = True
ChartTitle.Characters.Text = Cells(1, j)
Axes(xlCategory, xlPrimary).HasTitle = False
Axes(xlValue, xlPrimary).HasTitle = True
Axes(xlValue,
xlPrimary).AxisTitle.Characters.Text = "ug cm-3"
End With
Next
'---------------------'
With chtChart
HasLegend = False
End With
'---------------------'
End With
End If
Next j
End Sub
internet souces except for the line of code concerning the legend
(towards the bottom of the code, indicated with a row of dashes above
and below said line of code). The macro works great, however it is
hampered by the apparent need to still create a legend for each chart.
Basically I can physically see the macro creating a chart and adding a
legend. The macro then creates the next chart and adds a legend to the
new chart which includes the previous series. By the time the macro
has reached the last column to be charted the corresponding chart takes
forever as a legend is built to include a reference to each previous
series. Finally the code goes through each chart and removes the
legend and everything comes out fine. Ultimately I am just looking to
speed up the process by having the code never create a legend in the
first place.
Also, Any idea on how to alter the code such that the chart location is
a new worksheet rather than being placed within the current worksheet?
Thanks for any advice!
P.S. For all professors, etc. I promise I have my code properly
indented and with more descriptive comments
Option Explicit
Sub MultiX_OneY_Chart()
Dim rngDataSource As Range
Dim iDataRowsCt As Long
Dim iDataColsCt As Integer
Dim iSrsIx As Integer
Dim chtChart As CHART
Dim srsNew As Series
Dim i As Integer
i = 2
Dim j As Integer
Dim numrow As Integer
numrow = 0
Dim numcol As Integer
numcol = 0
Cells(2, 3).Select
Do While Not IsEmpty(ActiveCell)
numrow = numrow + 1
ActiveCell.Offset(1, 0).Select
Loop
numrow = numrow + 1
Cells(6, 1).Select
Do While Not IsEmpty(ActiveCell)
numcol = numcol + 1
ActiveCell.Offset(0, 1).Select
Loop
For j = 3 To numcol
If Not TypeName(Selection) = "Range" Then
' Doesn't work if no range is selected
MsgBox "Please select a data range and try again.", _
vbExclamation, "No Range Selected"
Else
Set rngDataSource = Range(Cells(1, i), Cells(numrow, j))
'Selection
With rngDataSource
iDataRowsCt = .Rows.Count
iDataColsCt = .Columns.Count
End With
' Create the chart
Set chtChart = ActiveSheet.ChartObjects.Add( _
Left:=ActiveSheet.Columns(ActiveWindow.ScrollColumn).Left + _
ActiveWindow.Width / 4, _
Width:=ActiveWindow.Width / 2, _
Top:=ActiveSheet.Rows(ActiveWindow.ScrollRow).Top + _
ActiveWindow.Height / 4, _
Height:=ActiveWindow.Height / 2).CHART
With chtChart
For iSrsIx = 1 To iDataColsCt - 1
' Add each series
Set srsNew = .SeriesCollection.NewSeries
With srsNew
Name = rngDataSource.Cells(1, iSrsIx)
Values = rngDataSource.Cells(2, iDataColsCt)
_
Resize(iDataRowsCt - 1, 1)
XValues = rngDataSource.Cells(2, iSrsIx) _
Resize(iDataRowsCt - 1, 1)
End With
With chtChart
HasTitle = True
ChartTitle.Characters.Text = Cells(1, j)
Axes(xlCategory, xlPrimary).HasTitle = False
Axes(xlValue, xlPrimary).HasTitle = True
Axes(xlValue,
xlPrimary).AxisTitle.Characters.Text = "ug cm-3"
End With
Next
'---------------------'
With chtChart
HasLegend = False
End With
'---------------------'
End With
End If
Next j
End Sub