+ Reply to Thread
Results 1 to 5 of 5

change position in VBA of chartobject doesn't work

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-21-2009
    Location
    Belgium - Mechelen
    MS-Off Ver
    Office 365 - version 2310
    Posts
    278

    change position in VBA of chartobject doesn't work

    Hi there ,

    I try to change the top position when adding a chartobject in a specific sheet.
    My purpose is to add several charts with their specific datarange , but each chart should be placed in a different region on the specific sheet.
    Therefor i put new values in the top , width , height and left position like :

    TPos = TPos + 250
    WPos = 385
    HPos = 255
    LPos = LPos + 390
    I use the following code to add the chart , but it stops at the Left:=LPos position in the code

            With Sheets(Charts).ChartObjects.Add _
                    (Left:=LPos, Width:=WPos, Top:=TPos, Height:=HPos)
                .Chart.ChartType = xlLine
                .Chart.SetSourceData Source:=Sheets("DataRange").Range(ChartDataRange)
                .Axes(xlValue).Select
                .Axes(xlValue).MinimumScale = 0
                .Axes(xlValue).MaximumScale = 40
                .Axes(xlValue).MaximumScale = 45
                .Axes(xlValue).MajorUnit = 5
                .Axes(xlValue).MinorUnit = 1
                .Legend.Select
                .Position = xlTop
        
            End With
    How can i use the variable TPos , Wpos , HPos and LPos in the code.
    Any help is welcome.

    Mario
    Last edited by MarMo; 08-22-2012 at 08:43 AM. Reason: problem solved

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: change position in VBA of chartobject doesn't work

    The code probably fails because Sheets(Chart) is not a valid reference.

    What does the variable Chart contain?

            With Sheets(Chart).ChartObjects.Add _
                    (Left:=LPos, Width:=WPos, Top:=TPos, Height:=HPos).Chart
                .Chart.ChartType = xlLine
                .Chart.SetSourceData Source:=Sheets("DataRange").Range(ChartDataRange)
                
                .Axes(xlValue).Select
                .Axes(xlValue).MinimumScale = 0
                .Axes(xlValue).MaximumScale = 40
                .Axes(xlValue).MaximumScale = 45
                .Axes(xlValue).MajorUnit = 5
                .Axes(xlValue).MinorUnit = 1
                .Legend.Position = xlTop
        
            End With
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Contributor
    Join Date
    09-21-2009
    Location
    Belgium - Mechelen
    MS-Off Ver
    Office 365 - version 2310
    Posts
    278

    Re: change position in VBA of chartobject doesn't work

    Hello Andy ,

    I do not have a variable named Chart.
    I just refer to the sheet named "Chart" where the charts need to come.
    Krgds
    Mario

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: change position in VBA of chartobject doesn't work

    Not what you code says though.

     With Sheets("Chart").ChartObjects.Add _
    You need to wrap Chart in quotes for it to be a static reference rather than a variable.

  5. #5
    Forum Contributor
    Join Date
    09-21-2009
    Location
    Belgium - Mechelen
    MS-Off Ver
    Office 365 - version 2310
    Posts
    278

    Re: change position in VBA of chartobject doesn't work

    Hello Andy ,

    You're right.I forgot the quotes , but i already had a variable ready for use with a sheet name.
    Just forgot to use that variable , instead i used the sheet name.

    Again , thanks for your help.
    Mario

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1