Stumped on this one...

richanor

Active Member
Joined
Apr 8, 2006
Messages
291
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

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
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Forum statistics

Threads
1,215,063
Messages
6,122,935
Members
449,094
Latest member
teemeren

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top