Hi
I have received considerable help from you guys in the last while getting this macro up and running (Jon Peltier in particular) and it was working fine until I noticed this glitch:
Basically the section of code pasted below is supposed to:
1. Find a range of data on the worksheet
2. Make a chart from it
3. Find a range on the worksheet where I would like the chart to go
4. Place the chart in the range
5. Repeat steps 1-4 another 2 times with different data ranges.
Here's what I can't understand - If I close the workbook [don't save changes] then open it and run the code, it creates 6 charts (instead of 3): 3 charts with meaningful data, stacked one on top of each other in the middle of the worksheet, and 3 charts with nonsense data - in the 'right places' on the worksheet.
If I then delete all of these charts, and without closing the workbook, run the code again, it only creates 3 charts - all with meaningful data, and all in the right place.
I can't understand why it doesn't do this the first time?
Any suggestions?
Thanks
I have received considerable help from you guys in the last while getting this macro up and running (Jon Peltier in particular) and it was working fine until I noticed this glitch:
Basically the section of code pasted below is supposed to:
1. Find a range of data on the worksheet
2. Make a chart from it
3. Find a range on the worksheet where I would like the chart to go
4. Place the chart in the range
5. Repeat steps 1-4 another 2 times with different data ranges.
Here's what I can't understand - If I close the workbook [don't save changes] then open it and run the code, it creates 6 charts (instead of 3): 3 charts with meaningful data, stacked one on top of each other in the middle of the worksheet, and 3 charts with nonsense data - in the 'right places' on the worksheet.
If I then delete all of these charts, and without closing the workbook, run the code again, it only creates 3 charts - all with meaningful data, and all in the right place.
I can't understand why it doesn't do this the first time?
Any suggestions?
Thanks
Code:
Sub MakeCharts()
'
'
Dim MyChart As Object
Sheets("pivot cash").Select
Range("A1").Select
Cells.Find(What:="Cht1", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate
Range(Selection, Selection.End(xlToRight)(1, 0)).Select
Range(Selection, Selection.End(xlDown)(0)).Select
Charts.Add
ActiveChart.ChartType = xlColumnClustered
Selection.AutoScaleFont = False
Set MyChart = ActiveChart.Location(Where:=xlLocationAsObject, Name:="pivot cash")
ActiveWindow.Visible = False
With MyChart
.Parent.Name = "no1Chart"
.HasTitle = False
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = False
End With
Dim RngToCover As Range
Dim ChtOb As ChartObject
Range("A1").Select
Cells.Find(What:="Cht1", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate
Range(Selection, Selection.End(xlToRight)(1, 4)).Select
Range(Selection, Selection.End(xlDown)(2)).Select
Selection.Offset(-1, 6).Select
Set RngToCover = Selection
ActiveSheet.ChartObjects("no1Chart").Activate
Set ChtOb = ActiveChart.Parent
ChtOb.Height = RngToCover.Height ' resize
ChtOb.Width = RngToCover.Width ' resize
ChtOb.Top = RngToCover.Top ' reposition
ChtOb.Left = RngToCover.Left ' reposition
Range("A1").Select
Cells.Find(What:="Cht2", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate
Range(Selection, Selection.End(xlToRight)(1, 0)).Select
Range(Selection, Selection.End(xlDown)(0)).Select
Charts.Add
ActiveChart.ChartType = xlColumnClustered
Selection.AutoScaleFont = False
Set MyChart = ActiveChart.Location(Where:=xlLocationAsObject, Name:="pivot cash")
ActiveWindow.Visible = False
With MyChart
.Parent.Name = "no2Chart"
.HasTitle = False
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = False
End With
Range("A1").Select
Cells.Find(What:="Cht2", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate
Range(Selection, Selection.End(xlToRight)(1, 4)).Select
Range(Selection, Selection.End(xlDown)(2)).Select
Selection.Offset(-1, 6).Select
Set RngToCover = Selection
ActiveSheet.ChartObjects("no2Chart").Activate
Set ChtOb = ActiveChart.Parent
ChtOb.Height = RngToCover.Height ' resize
ChtOb.Width = RngToCover.Width ' resize
ChtOb.Top = RngToCover.Top ' reposition
ChtOb.Left = RngToCover.Left ' reposition
Range("A1").Select
Cells.Find(What:="Cht3", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate
Range(Selection, Selection.End(xlToRight)(1, 0)).Select
Range(Selection, Selection.End(xlDown)(0)).Select
Charts.Add
ActiveChart.ChartType = xlColumnClustered
Selection.AutoScaleFont = False
Set MyChart = ActiveChart.Location(Where:=xlLocationAsObject, Name:="pivot cash")
ActiveWindow.Visible = False
With MyChart
.Parent.Name = "no3Chart"
.HasTitle = False
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = False
End With
Range("A1").Select
Cells.Find(What:="Cht3", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate
Range(Selection, Selection.End(xlToRight)(1, 5)).Select
Range(Selection, Selection.End(xlDown)(2)).Select
Selection.Offset(-1, 6).Select
Set RngToCover = Selection
ActiveSheet.ChartObjects("no3Chart").Activate
Set ChtOb = ActiveChart.Parent
ChtOb.Height = RngToCover.Height ' resize
ChtOb.Width = RngToCover.Width ' resize
ChtOb.Top = RngToCover.Top ' reposition
ChtOb.Left = RngToCover.Left ' reposition