View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
mate[_2_] mate[_2_] is offline
external usenet poster
 
Posts: 4
Default Help Creating Code

Apologies for not replying sooner, but thanks very much for this code
- it was exactly what I needed.

Thanks again.


On 14 Mar, 01:06, "merjet" wrote:
Chart data in Excel must be in contiguous cells. Therefore, you will
need to put the data for each chart in its own set of contiguous cell.
The followingcodedoes that and then creates the chart for each name
you have in your data.

Sub MakeCharts()
Dim ws As Worksheet
Dim iRow as Integer
Dim aName as String

Set ws = Sheets("Sheet1")
On Error Resume Next
'avoid prompt when deleting chart
Application.DisplayAlerts = False
ws.Columns("E:G").ClearContents
iRow = 3
Do
'create chart data in Cols E-G
ws.Cells(4 * iRow - 11, 5) = ws.Cells(1, 1)
ws.Cells(4 * iRow - 11, 6) = ws.Cells(1, 2)
ws.Cells(4 * iRow - 11, 7) = ws.Cells(1, 3)
ws.Cells(4 * iRow - 10, 5) = ws.Cells(2, 1)
ws.Cells(4 * iRow - 10, 6) = ws.Cells(2, 2)
ws.Cells(4 * iRow - 10, 7) = ws.Cells(2, 3)
ws.Cells(4 * iRow - 9, 5) = ws.Cells(iRow, 1)
ws.Cells(4 * iRow - 9, 6) = ws.Cells(iRow, 2)
ws.Cells(4 * iRow - 9, 7) = ws.Cells(iRow, 3)

'grab name for new chart and title
aName = ws.Cells(iRow, 1)
'delete old chart
Sheets(aName).Delete
'make new chart
Charts.Add
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData _
Source:=ws.Range(ws.Cells(4 * iRow - 11, 5), _
ws.Cells(4 * iRow - 9, 7)), PlotBy:=xlColumns
ActiveChart.Location Whe=xlLocationAsNewSheet, _
Name:=aName
With ActiveChart
.Name = aName
.HasTitle = True
.ChartTitle.Characters.Text = aName
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = False
End With
iRow = iRow + 1
Loop Until ws.Cells(iRow, 1) = ""
Application.DisplayAlerts = True

End Sub

Hth,
Merjet