Trying to Position the Graph in a spreadsheet

Eric Penfold

Active Member
Joined
Nov 19, 2021
Messages
431
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
The code paste the graph but won`t position the Graph in the range specified. Any help much appreciated thanks!

VBA Code:
Option Explicit

Sub DailyMail_Chart_Update()

    Dim wb     As Workbook
    Dim ws     As Worksheet
    Dim dwb    As Workbook
    Dim dws    As Worksheet
    Dim LRow   As Long
    Dim cht1   As ChartObject, cht2 As ChartObject
    Dim Month  As Date
    Dim shape  As Excel.shape
    Dim MyWidth As Single, MyHeight As Single
    Dim chtRng As Range

    Set wb = Workbooks("MyPersonal.xlsb")
    Set ws = wb.Worksheets("DailyMail")
    Set dwb = Workbooks("DailyMail.xlsx")
    Set dws = dwb.Worksheets("Daily Mail Update")
    
    With dws
        Set cht1 = .ChartObjects("Daily_Mail_Graph")
        cht1.Activate
        ActiveChart.ChartTitle.Select
        Selection.Characters.Text = Format(Date, "mmmm")
        With Selection.Characters(Start:=1, Length:=30).Font
            .Name = "Arial"
            .FontStyle = "Bold"
            .Size = 16
            ActiveChart.ChartTitle.Left = 600
            ActiveChart.ChartTitle.Top = 0
            dws.ChartObjects("Daily_Mail_Graph").Chart.ChartArea.Copy
            ws.Activate
            With ws
                On Error Resume Next
                .ChartObjects.Delete
                .Paste
                Set cht2 = .ChartObjects("Daily_Mail_Graph")
                cht2.Select
                With Selection
                    .Top = .Range("B40")
                    .Left = .Range("B40")
                    .Width = .Range("B40:Q40")
                    .Height = .Range("B40:Q69")
                End With
            End With
        End With
    End With
    
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
How about to modify this part:
VBA Code:
                With Selection
                    .Top = .Range("B1:B40").Height
                    .Left = .Range("A40:B40").Width
                    .Width = .Range("B40:Q40").Width
                    .Height = .Range("B40:Q69").Height
                End With
 
Upvote 0
Ok, I've tested it. This works for me:
VBA Code:
                 With ws
                   Set cht2 = .ChartObjects("Daily_Mail_Graph")
                    cht2.Top = .Range("B1:B40").Height
                    cht2.Left = .Range("A40:B40").Width
                    cht2.Width = .Range("B40:Q40").Width
                    cht2.Height = .Range("B40:Q69").Height
                End With
I think the problem is because there two nested With satements which is not recommended.
 
Upvote 0
Theni full code will be:
VBA Code:
Option Explicit

Sub DailyMail_Chart_Update()

    Dim wb     As Workbook
    Dim ws     As Worksheet
    Dim dwb    As Workbook
    Dim dws    As Worksheet
    Dim LRow   As Long
    Dim cht1   As ChartObject, cht2 As ChartObject
    Dim Month  As Date
    Dim shape  As Excel.shape
    Dim MyWidth As Single, MyHeight As Single
    Dim chtRng As Range

    Set wb = Workbooks("MyPersonal.xlsb")
    Set ws = wb.Worksheets("DailyMail")
    Set dwb = Workbooks("DailyMail.xlsx")
    Set dws = dwb.Worksheets("Daily Mail Update")
 
    With dws
        Set cht1 = .ChartObjects("Daily_Mail_Graph")
        cht1.Activate
        ActiveChart.ChartTitle.Select
        Selection.Characters.Text = Format(Date, "mmmm")
        With Selection.Characters(Start:=1, Length:=30).Font
            .Name = "Arial"
            .FontStyle = "Bold"
            .Size = 16
            ActiveChart.ChartTitle.Left = 600
            ActiveChart.ChartTitle.Top = 0
            dws.ChartObjects("Daily_Mail_Graph").Chart.ChartArea.Copy
            ws.Activate
            With ws
                On Error Resume Next
                .ChartObjects.Delete
                .Paste
             
                 Set cht2 = .ChartObjects("Daily_Mail_Graph")
                 cht2.Top = .Range("B1:B40").Height
                 cht2.Left = .Range("A40:B40").Width
                 cht2.Width = .Range("B40:Q40").Width
                 cht2.Height = .Range("B40:Q69").Height
            End With
        End With
    End With
 
End Sub
 
Upvote 0
Theni full code will be:
VBA Code:
Option Explicit

Sub DailyMail_Chart_Update()

    Dim wb     As Workbook
    Dim ws     As Worksheet
    Dim dwb    As Workbook
    Dim dws    As Worksheet
    Dim LRow   As Long
    Dim cht1   As ChartObject, cht2 As ChartObject
    Dim Month  As Date
    Dim shape  As Excel.shape
    Dim MyWidth As Single, MyHeight As Single
    Dim chtRng As Range

    Set wb = Workbooks("MyPersonal.xlsb")
    Set ws = wb.Worksheets("DailyMail")
    Set dwb = Workbooks("DailyMail.xlsx")
    Set dws = dwb.Worksheets("Daily Mail Update")
 
    With dws
        Set cht1 = .ChartObjects("Daily_Mail_Graph")
        cht1.Activate
        ActiveChart.ChartTitle.Select
        Selection.Characters.Text = Format(Date, "mmmm")
        With Selection.Characters(Start:=1, Length:=30).Font
            .Name = "Arial"
            .FontStyle = "Bold"
            .Size = 16
            ActiveChart.ChartTitle.Left = 600
            ActiveChart.ChartTitle.Top = 0
            dws.ChartObjects("Daily_Mail_Graph").Chart.ChartArea.Copy
            ws.Activate
            With ws
                On Error Resume Next
                .ChartObjects.Delete
                .Paste
             
                 Set cht2 = .ChartObjects("Daily_Mail_Graph")
                 cht2.Top = .Range("B1:B40").Height
                 cht2.Left = .Range("A40:B40").Width
                 cht2.Width = .Range("B40:Q40").Width
                 cht2.Height = .Range("B40:Q69").Height]
            End With
        End With
    End With
 
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,893
Messages
6,175,244
Members
452,622
Latest member
Laura_PinksBTHFT

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