Results 1 to 9 of 9

Thread: Problems setting a chart range

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Location
    Loughborough
    Posts
    4

    Problems setting a chart range

    Hi
    First timer, so please bear with me. I have the code below running to prompt for a new sheet name, create it, copy data and a chart to it, and up to that point it all works fine. Problem is I need the chart on the new sheet to point to the data on that sheet and not from whence it came ?
    Note : The Data is always in the same position on each sheet C9:I9

    Any guidance would be greatly appreciated ?

    KR ... Ian

    Code Below :
    Sub Button2_Click()

    Dim newwks As Worksheet
    Dim OldWks As Worksheet
    Dim NewName As String
    Dim UpperLeftCell As String
    Dim numbPoints As Long
    Dim newsht As String
    Dim oldsht As String


    Set OldWks = Worksheets("Tables")

    NewName = Trim(InputBox(prompt:="what do you want to call it?"))

    If NewName = "" Then
    Exit Sub
    End If

    Set newwks = Worksheets.Add
    On Error Resume Next
    newwks.Name = NewName
    If Err.Number <> 0 Then
    MsgBox "Something went wrong with the naming!" & vbLf & _
    "Please change: " & newwks.Name & " to what you want."
    Err.Clear
    End If

    OldWks.Range("Chart_Data").Copy
    newwks.Range("b2").Select
    ActiveSheet.Paste
    newwks.Range("b2:k9").Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Range("A1").Select
    Application.CutCopyMode = False
    Application.ScreenUpdating = False

    newwks.Range("B2:L35").Select

    With ActiveSheet.PageSetup
    .PrintTitleRows = ""
    .PrintTitleColumns = ""
    End With
    ActiveSheet.PageSetup.PrintArea = "$B$2:$L$35"
    With ActiveSheet.PageSetup
    .PaperSize = xlPaperA4
    .FirstPageNumber = xlAutomatic
    .Order = xlDownThenOver
    .BlackAndWhite = False
    .Zoom = False
    .FitToPagesWide = 1
    .FitToPagesTall = 1
    .PrintErrors = xlPrintErrorsDisplayed
    End With

    "THIS IS THE BIT I AM HAVING PROBS WITH"

    newwks.Cells(1, 1) = "'" & newsht
    newsht = ActiveSheet.Text

    ActiveSheet.ChartObjects().Activate
    ActiveChart.PlotArea.Select
    ActiveChart.SeriesCollection(1).Values = "=newsht & !R9C3:R9C10"

    End Sub

  2. #2

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Location
    Loughborough
    Posts
    4

    Re: Problems setting a chart range

    Hi

    Many thanks for your reply, yes i am doing this from within Excel

  4. #4

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Problems setting a chart range

    "THIS IS THE BIT I AM HAVING PROBS WITH"
    so what happens?
    wrong result?
    nothing?
    error?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Location
    Loughborough
    Posts
    4

    Re: Problems setting a chart range

    Hi

    Thanks for your reply, as stated, all code works up until the bit I have highlighted as "THIS IS THE BIT I AM HAVING PROBS WITH"

    From here is where I am looking at changing the chart values range to those in the newly created sheet.

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Problems setting a chart range

    all code works up until the bit I have highlighted
    so what happens then?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  8. #8

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Location
    Loughborough
    Posts
    4

    Re: Problems setting a chart range

    Sorry

    I had better try and explain a little clearer. The last part of code beneath :

    newwks.Cells(1, 1) = "'" & newsht
    newsht = ActiveSheet.Text

    ActiveSheet.ChartObjects().Activate
    ActiveChart.PlotArea.Select
    ActiveChart.SeriesCollection(1).Values = "=newsht & !R9C3:R9C10"

    I san attempt to re-assign the values to the current worksheet, I tried using the previously defined "newsht" variable but nothing happens. I cannot hard type the sheet name as this changes each time the user runs and generates a new sheet.

    HTH

    Ian

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Problems setting a chart range

    you need specify which chartobject to work with

    values has to be an array, or possibly a range object
    possibly the formula property of the series collection, is where you can assign "=newsht & !R9C3:R9C10"

    if you have on error resume next anywhere, remove it so you can see any errors that occur
    if you are using inline error handling at some point make sure to reset error handling after that point
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width