Welcome to the Excel Forum

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed.

Please Register to Remove these Ads

Please Register to Remove these Ads



Reply
  #1  
Old 06-30-2009, 03:46 PM
dami dami is offline
Registered User
 
Join Date: 24 Jan 2008
Posts: 13
dami is becoming part of the community
Question Graphing two cell ranges using Macro

Please Register to Remove these Ads

Hey,
I am trying to write a macro to graph two specified columns that has a constant starting row( for ex: A22 for x-axis and D22 for y-axis), but the ending row changes depending on how many data points are acquired in the test.

1. I pasted the code I got so far from Macro recording and me trying to edit it to make it universal (trying to apply it to different files) and I can't figure out the code to make it run the macro in the Active sheet of any workbook as I will be doing many tests.

2. is it possible to find the last row of data and graph the points up to that row? (for ex: Two Columns and starting row always equals to A22 and D22, and the macro should find the last row, say it found A3000 and D3000. Then graph the range A22:A3000 as x and D22:D3000 as y. )

hope it makes sense!


Code:
Sub graphing()


    Range("E22:E4147").Select
    Charts.Add
    ActiveChart.ChartType = xlXYScatterSmooth
    ActiveChart.SetSourceData Source:=ActiveSheet.Range("E22:E4147"), PlotBy:=xlColumns
    ActiveChart.SeriesCollection(1).XValues = _
        "=ActiveSheet!R22C1:R4147C1"
    ActiveChart.Location Where:=xlLocationAsNewSheet
    With ActiveChart
        .HasTitle = False
        .Axes(xlCategory, xlPrimary).HasTitle = True
        .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Time (S)"
        .Axes(xlValue, xlPrimary).HasTitle = True
        .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Voltage (V)"
    End With
    ActiveChart.PlotArea.Select
    Selection.ClearFormats
    ActiveChart.Legend.Select
    Selection.Delete
    ActiveChart.PlotArea.Select
    With Selection.Border
        .ColorIndex = 57
        .Weight = xlMedium
        .LineStyle = xlContinuous
    End With
    Selection.Interior.ColorIndex = xlNone
    ActiveChart.Axes(xlValue).Select
End Sub

Last edited by dami; 07-02-2009 at 09:55 AM.
Reply With Quote
  #2  
Old 07-01-2009, 04:54 AM
Andy Pope's Avatar
Andy Pope Andy Pope is offline
Forum Guru
 
Join Date: 10 May 2004
Location: Essex, UK
MS Office Version:2003 & 2007 sp2
Posts: 7,223
Andy Pope Has a higher level of understanding Andy Pope Has a higher level of understanding Andy Pope Has a higher level of understanding Andy Pope Has a higher level of understanding Andy Pope Has a higher level of understanding Andy Pope Has a higher level of understanding Andy Pope Has a higher level of understanding Andy Pope Has a higher level of understanding
Re: Graphing two cell ranges using Macro

You can use the End method to locate last row with data.

Code:
Sub graphing()

    Dim lngLastRow As Long

    lngLastRow = Cells(Rows.Count, 5).End(xlUp).Row
    
    Range("E22:E" & lngLastRow).Select
    Charts.Add
    ActiveChart.ChartType = xlXYScatterSmooth
    ActiveChart.SetSourceData Source:=ActiveSheet.Range("E22:E" & lngLastRow), PlotBy:=xlColumns
    ActiveChart.SeriesCollection(1).XValues = _
        "='" & ActiveSheet.Name & "'!R22C1:R" & lngLastRow & "C1"
    ActiveChart.Location Where:=xlLocationAsNewSheet
    With ActiveChart
        .HasTitle = False
        .Axes(xlCategory, xlPrimary).HasTitle = True
        .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Time (S)"
        .Axes(xlValue, xlPrimary).HasTitle = True
        .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Voltage (V)"
        .PlotArea.ClearFormats
        .Legend.Delete
        With .PlotArea.Border
            .ColorIndex = 57
            .Weight = xlMedium
            .LineStyle = xlContinuous
        End With
    End With
    Selection.Interior.ColorIndex = xlNone
    ActiveChart.Axes(xlValue).Select
End Sub
__________________
Cheers
Andy
www.andypope.info
Reply With Quote
  #3  
Old 07-02-2009, 09:23 AM
dami dami is offline
Registered User
 
Join Date: 24 Jan 2008
Posts: 13
dami is becoming part of the community
Re: Graphing two cell ranges using Macro

Thanks for the reply Andy, but it gives a run time error 438, "Object does not support this Property or Method"

When I go to Debug it, it highlights the following row:

Code:
 ActiveChart.SetSourceData Source:=ActiveSheet.Range("E22:E" & lngLastRow), PlotBy:=xlColumns
Reply With Quote
  #4  
Old 07-02-2009, 09:41 AM
Andy Pope's Avatar
Andy Pope Andy Pope is offline
Forum Guru
 
Join Date: 10 May 2004
Location: Essex, UK
MS Office Version:2003 & 2007 sp2
Posts: 7,223
Andy Pope Has a higher level of understanding Andy Pope Has a higher level of understanding Andy Pope Has a higher level of understanding Andy Pope Has a higher level of understanding Andy Pope Has a higher level of understanding Andy Pope Has a higher level of understanding Andy Pope Has a higher level of understanding Andy Pope Has a higher level of understanding
Re: Graphing two cell ranges using Macro

The activesheet immediately after inserting a chartsheet is the chart sheet.
Chart sheets do not have cells or ranges.

So you need to create a references to the sheet

Code:
dim shtData as worksheet

set shtData = Activesheet

lngLastRow = shtdata.Cells(shtdata.Rows.Count, 5).End(xlUp).Row
    
    shtdata.Range("E22:E" & lngLastRow).Select
    Charts.Add
    ActiveChart.ChartType = xlXYScatterSmooth

ActiveChart.SetSourceData Source:=shtData.Range("E22:E" & lngLastRow), PlotBy:=xlColumns
__________________
Cheers
Andy
www.andypope.info
Reply With Quote
  #5  
Old 07-02-2009, 09:55 AM
dami dami is offline
Registered User
 
Join Date: 24 Jan 2008
Posts: 13
dami is becoming part of the community
Re: Graphing two cell ranges using Macro

Thanks a lot Andy,
ya before posting here I checked out your site, pretty cool stuff!
Reply With Quote


Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Forum Jump