Consulting

Results 1 to 16 of 16

Thread: Cant refer to graph from other sheet?

  1. #1

    Cant refer to graph from other sheet?

    Hi everybody!

    I want to refer to a graph from my sheet called "Overview", the graph is located in a sheet called "Historic". The code is the following:

    Dim shtHis As Worksheet: Set shtHis = Sheets("Historic")
    Dim lInterval As Long: lInterval = shtHis.Range("A2").End(xlDown).Row


    shtHis.ChartObjects("Chart 11").Activate
    ActiveChart.SetSourceData Source:=shtHis.Range("A1:A" & lInterval & ",B1:B" & lInterval)

    The code is located in a module for the whole workbook.
    When i push the button to run the code in "overview" i get a bug: "Run-time error "1004": Method "range" of Object" worksheet" failed.

    Im not very familiar with graphs within VBA, am i refering to it the wrong way?
    - When i press f8 within the vba window, in compile mode, the code works just fine.

    Hope someone can help! Cheers!

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Which line is highlighted by the debugger? The error message is not related to charts.

    BTW, it's generally safer to use End(xlUp):

    lInterval = shtHis.Cells(shtHis.Rows.Count, "A").End(xlUp).Row
    and you don't need to activate the chart:

    shtHis.ChartObjects("Chart 11").Chart.SetSourceData Source:=shtHis.Range("A1:A" & lInterval & ",B1:B" & lInterval)
    Be as you wish to seem

  3. #3
    Ah thank you for the tip!

    The line which is bugging, is the following:
    ActiveChart.SetSourceData Source:=shtHis.Range("A1:A" & lInterval & ",B1:B" & lInterval)

    The code above the bugging line is the following, it doesn't seem to have any problems running these:
    Sub Adjust_graphs()


    Dim shtHis As Worksheet: Set shtHis = Sheets("Historic")
    Dim lInterval As Long: lInterval = shtHis.Range("A2").End(xlDown).Row


    shtHis.ChartObjects("Chart 11").Activate
    ActiveChart.SetSourceData Source:=shtHis.Range("A1:A" & lInterval & ",B1:B" & lInterval)

  4. #4
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    What's the actual value of lInterval when the error is shown?
    Be as you wish to seem

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Try something like this, That wasn't the correct way to specify a Range (as you found out)

    Note the colon in the .Range


    Option Explicit
    
    Sub Macro1()
        Dim L As Long
        
        L = 8
        
        Worksheets("Sheet2").Select
        ActiveSheet.Shapes.AddChart.Select
        ActiveChart.ChartType = xlXYScatter
        ActiveChart.SetSourceData Source:=Worksheets("Sheet1").Range("A1:B" & L)
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    I checked under "Locals" it says 1238, which is my last observation, so it should be ok!?

  7. #7
    Hi Paul!
    The reason for why i have written it as i have, is because i dont have 1 graph but 4!
    Im using Column A,B for graph 1, Column A, C for graph 2, A,D for graph 3 and A,E as input for graph 4

    The weird thing is that the code goes into debug mode, when launching from the button, but when i launch it in the compile mode, it works fine!

  8. #8
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    The code is fine - just tested running from a button without issue. What kind of button is it and where is it located?
    Be as you wish to seem

  9. #9
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Ok, thanks for the additional information

    Then you'd want to do something like this

    Option Explicit
    
     
    Sub Macro1()
        Dim L As Long
        Dim ws As Worksheet
         
        Set ws = Worksheets("Sheet1")
        
        L = 8
         
         'ActiveChart.SetSourceData Source:=shtHis.Range("A1:A" & lInterval & ",B1:B" & lInterval) -- wrong way
         
         
        Worksheets("Sheet2").Select
        ActiveSheet.Shapes.AddChart.Select
        ActiveChart.ChartType = xlXYScatter
        ActiveChart.SetSourceData Source:=Union(ws.Range("A1:A" & L), ws.Range("B1:B" & L))
    
        ActiveSheet.Shapes.AddChart.Select
        ActiveChart.ChartType = xlXYScatter
        ActiveChart.SetSourceData Source:=Union(ws.Range("A1:A" & L), ws.Range("C1:C" & L))
        ActiveSheet.Shapes.AddChart.Select
        ActiveChart.ChartType = xlXYScatter
        ActiveChart.SetSourceData Source:=Union(ws.Range("A1:A" & L), ws.Range("D1:D" & L))
        ActiveSheet.Shapes.AddChart.Select
        ActiveChart.ChartType = xlXYScatter
        ActiveChart.SetSourceData Source:=Union(ws.Range("A1:A" & L), ws.Range("E1:E" & L))
     
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  10. #10
    Thanks man! And good news that im not completely useless at this! But the button you used to launch the code, was it in another sheet than where the code took in the data? I think that might be my problem.

    The button is located in another sheet called Overview, all the data related to the button is in a specific sheet called "Historic"
    The button i just created using insert and form controls, the code for it is located under "Module1"

  11. #11
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    I tested it with the button on another sheet and it still works without problem in 2010.

    To be clear: there is nothing wrong with the way you are referring to the range. It is perfectly valid syntax
    since it resolves to this:

    shtHis.Range("A1:A1238,B1:B1238")
    Be as you wish to seem

  12. #12
    Yeah i think i get you! Now i just have to figure out, whether i placed some code in the wrong module or something like that - i suppose

  13. #13
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Never mind, forgot the comma
    Last edited by Paul_Hossler; 10-12-2015 at 07:18 AM. Reason: Ooops
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  14. #14
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    You missed the comma before the B1.
    Be as you wish to seem

  15. #15
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    U B 2 fast

    I was typing as fast as I could to cover my Oops
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  16. #16
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Be as you wish to seem

Posting Permissions

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