+ Reply to Thread
Results 1 to 5 of 5

how to select current range

  1. #1
    Registered User
    Join Date
    08-02-2005
    Posts
    7

    how to select current range

    Hi all,
    While I'm comfortable with programming, I'm rusty and I have never seen VBA before.

    I'm trying to make a macro that makes a graph with trendline based on whatever range of numbers I have selected. I guess that means that in the code below, I want to change the sheet and range references to whatever is currently selected. Your answer will be appreciated. Thanks.

    Charts.Add
    ActiveChart.ChartType = xlLineMarkers
    ActiveChart.SetSourceData Source:=Sheets("R1 INT").Range("AC12:AC15")
    ActiveChart.Location Where:=xlLocationAsObject, Name:="R1 INT"
    ActiveChart.SeriesCollection(1).Select
    ActiveChart.SeriesCollection(1).Trendlines.Add(Type:=xlLinear, Forward:=0, _
    Backward:=0, DisplayEquation:=True, DisplayRSquared:=False).Select
    ActiveChart.SeriesCollection(1).Trendlines(1).DataLabel.Select
    Selection.Left = 141
    Selection.Top = 1
    End Sub

  2. #2
    Tom Ogilvy
    Guest

    Re: how to select current range

    Possibly:

    Charts.Add
    ActiveChart.ChartType = xlLineMarkers
    ActiveChart.SetSourceData Source:=Selection
    ActiveChart.Location Where:=xlLocationAsObject, Name:="R1 INT"
    ActiveChart.SeriesCollection(1).Select
    ActiveChart.SeriesCollection(1).Trendlines.Add(Type:=xlLinear,
    Forward:=0, _
    Backward:=0, DisplayEquation:=True,
    DisplayRSquared:=False).Select
    ActiveChart.SeriesCollection(1).Trendlines(1).DataLabel.Select
    Selection.Left = 141
    Selection.Top = 1
    End Sub

    --
    Regards,
    Tom Ogilvy

    "mikerr" <mikerr.1t59ra_1122998801.1344@excelforum-nospam.com> wrote in
    message news:mikerr.1t59ra_1122998801.1344@excelforum-nospam.com...
    >
    > Hi all,
    > While I'm comfortable with programming, I'm rusty and I have never seen
    > VBA before.
    >
    > I'm trying to make a macro that makes a graph with trendline based on
    > whatever range of numbers I have selected. I guess that means that in
    > the code below, I want to change the sheet and range references to
    > whatever is currently selected. Your answer will be appreciated.
    > Thanks.
    >
    > Charts.Add
    > ActiveChart.ChartType = xlLineMarkers
    > ActiveChart.SetSourceData Source:=Sheets("R1
    > INT").Range("AC12:AC15")
    > ActiveChart.Location Where:=xlLocationAsObject, Name:="R1 INT"
    > ActiveChart.SeriesCollection(1).Select
    > ActiveChart.SeriesCollection(1).Trendlines.Add(Type:=xlLinear,
    > Forward:=0, _
    > Backward:=0, DisplayEquation:=True,
    > DisplayRSquared:=False).Select
    > ActiveChart.SeriesCollection(1).Trendlines(1).DataLabel.Select
    > Selection.Left = 141
    > Selection.Top = 1
    > End Sub
    >
    >
    > --
    > mikerr
    > ------------------------------------------------------------------------
    > mikerr's Profile:

    http://www.excelforum.com/member.php...o&userid=25830
    > View this thread: http://www.excelforum.com/showthread...hreadid=392199
    >




  3. #3
    Registered User
    Join Date
    08-02-2005
    Posts
    7
    Thanks, Tom

    Strangely, I'm getting an error of "run-time error 13, type-mismatch. " The debugger then throws me back to the line:

    ActiveChart.SetSourceData Source:=Selection

    This seems to be happening regardless of what sheet or cell range I am on. As far as I can tell, this is the only code below that you recommended changed.

    Again, I do appreciate your help (or anyone else's) as this would save me quite a few keystrokes.

  4. #4
    Tom Ogilvy
    Guest

    Re: how to select current range

    Then try:

    ActiveChart.SetSourceData Source:=Selection.Address(1,1,xlR1C1,True)


    --
    Regards,
    Tom Ogilvy

    "mikerr" <mikerr.1t5fbb_1123006016.0082@excelforum-nospam.com> wrote in
    message news:mikerr.1t5fbb_1123006016.0082@excelforum-nospam.com...
    >
    > Thanks, Tom
    >
    > Strangely, I'm getting an error of "run-time error 13, type-mismatch. "
    > The debugger then throws me back to the line:
    >
    > ActiveChart.SetSourceData Source:=Selection
    >
    > This seems to be happening regardless of what sheet or cell range I am
    > on. As far as I can tell, this is the only code below that you
    > recommended changed.
    >
    > Again, I do appreciate your help (or anyone else's) as this would save
    > me quite a few keystrokes.
    >
    >
    > --
    > mikerr
    > ------------------------------------------------------------------------
    > mikerr's Profile:

    http://www.excelforum.com/member.php...o&userid=25830
    > View this thread: http://www.excelforum.com/showthread...hreadid=392199
    >




  5. #5
    Registered User
    Join Date
    08-02-2005
    Posts
    7
    Well, as it turns out, I managed to find a different way:

    Charts.Add
    ActiveChart.Location Where:=xlLocationAsObject, Name:="R1 INT"
    ActiveChart.ChartType = xlLineMarkers
    ActiveChart.SeriesCollection(1).Select
    ActiveChart.SeriesCollection(1).Trendlines.Add(Type:=xlLinear, Forward:=0, _
    Backward:=0, DisplayEquation:=True, DisplayRSquared:=False).Select
    ActiveChart.SeriesCollection(1).Trendlines(1).DataLabel.Select
    Selection.Left = 142
    Selection.Top = 1
    End Sub

    I'm not quite sure how it works, but it does. Peltiertech.com recommended changing the order of 2 of the lines, and that did the trick.

    Only 1 problem left: It puts these graphs on the "R1 Int" Sheet, when I want it on whatever is my active sheet. I tried:

    ActiveChart.Location Where:=xlLocationAsObject, Name:=ActiveSheet

    but it throws me back a "run-time error 5". Any ideas? Thanks.

+ 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