Plot xy scatter line up to nonempty cell

rathalex

New Member
Joined
Oct 25, 2017
Messages
32
Hello,
I am working to create xy scatter line graph using two data columns. Since data ranges change occasionally, i need to plot up to last non empty cells. Any advice how to approach this? Also, is there way to create a message box that will ask for a chart title? Im am very new to VBA,so dont know all the tricks yet. Thank in advance all!
 
There are a number of problems with the posted code. Try it like this...

Code:
[FONT=Courier New][COLOR=darkblue]Sub[/COLOR] Graph()

    [COLOR=darkblue]Dim[/COLOR] rData [COLOR=darkblue]As[/COLOR] Range
    [COLOR=darkblue]Dim[/COLOR] LastRow [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    
    [COLOR=darkblue]With[/COLOR] Worksheets("Data")
        LastRow = .Cells(.Rows.Count, "AB").End(xlUp).Row
        [COLOR=darkblue]Set[/COLOR] rData = .Range("AB1:AC" & LastRow)
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    
    [COLOR=darkblue]With[/COLOR] Charts.Add
        .ChartType = xlXYScatterSmoothNoMarkers
        .SetSourceData Source:=rData
        .SetElement msoElementChartTitleAboveChart
        .ChartTitle.Text = "Data"
        .PlotArea.ClearFormats
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]

[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR][/FONT]

Also, make sure that you pay close attention to the dots (.) in the code.

Hope this helps!
 
Last edited:
Upvote 0

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Thank you very much Domenic! That code is perfect! I would still have to add inputfunction for thr title message box. I can just simply add it to Charts section correct? without making a new With statement?
 
Upvote 0
With the following code, if the user enters a title, the title will be added to the chart. However, if the user clicks on Cancel, the title is removed.

Code:
[FONT=Courier New][COLOR=darkblue]Sub[/COLOR] Graph()

    [COLOR=darkblue]Dim[/COLOR] rData [COLOR=darkblue]As[/COLOR] Range
    [COLOR=darkblue]Dim[/COLOR] sChartTitle [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] LastRow [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    
    sChartTitle = InputBox("Enter the chart title...", "Chart Title")
    
    [COLOR=darkblue]With[/COLOR] Worksheets("Data")
        LastRow = .Cells(.Rows.Count, "AB").End(xlUp).Row
        [COLOR=darkblue]Set[/COLOR] rData = .Range("AB1:AC" & LastRow)
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    
    [COLOR=darkblue]With[/COLOR] Charts.Add
        .ChartType = xlXYScatterSmoothNoMarkers
        .SetSourceData Source:=rData
        [COLOR=darkblue]If[/COLOR] Len(sChartTitle) > 0 [COLOR=darkblue]Then[/COLOR]
            .SetElement msoElementChartTitleAboveChart
            .ChartTitle.Text = sChartTitle
        [COLOR=darkblue]Else[/COLOR]
            .SetElement msoElementChartTitleNone
        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
        .PlotArea.ClearFormats
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]

[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR][/FONT]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,941
Members
449,094
Latest member
teemeren

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top