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!
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Let's assume that Column A and Column B contain your data. If the data is bound by empty an empty row and empty column, you can use the CurrentRegion property to define your range..

Code:
[FONT=Courier New]    [COLOR=darkblue]Dim[/COLOR] rData [COLOR=darkblue]As[/COLOR] Range
    
    [COLOR=darkblue]Set[/COLOR] rData = Worksheets("Sheet1").Range("A1").CurrentRegion[/FONT]

Otherwise, here's an alternative...

Code:
[FONT=Courier New]    [COLOR=darkblue]Dim[/COLOR] rData [COLOR=darkblue]As[/COLOR] Range
    [COLOR=darkblue]Dim[/COLOR] LastRow
    
    [COLOR=darkblue]With[/COLOR] Worksheets("Sheet1")
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        [COLOR=darkblue]Set[/COLOR] rData = .Range("A1:B" & LastRow)
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR][/FONT]

To propmpt the user for a chart title, you can use the InputBox function...

Code:
[FONT=Courier New]    [COLOR=darkblue]Dim[/COLOR] sTitle [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    
    sTitle = InputBox("Enter the chart title...", "Chart Title")
    [COLOR=darkblue]If[/COLOR] Len(sTitle) = 0 [COLOR=darkblue]Then[/COLOR]
        MsgBox "User cancelled...", vbInformation [COLOR=green]'optional[/COLOR]
        [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR][/FONT]

Hope this helps!
 
Upvote 0
Thank you Domenic! How do I incorporate that range into the xlXYScatterSmoothNoMarkers type of plot. Do i use SetSourceData?
 
Upvote 0
Yes, use SetSourceData, like this...

Code:
ActiveChart.SetSourceData Source:=rData

or

Code:
Worksheets("Sheet1").ChartObjects("Chart 1").Chart.SetSourceData Source:=rData


Note, though, the chart needs to be active for the first one.
 
Upvote 0
For some reason cant get this code to run

Dim rData As Range
Dim LastRow

With Worksheets("Sheet1")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
Set rData = .Range("A1:B" & LastRow)
End With

Is LastRow a String or Range? I think im missing something
 
Upvote 0
Sorry, LastRow should have been declared as Long...

Code:
Dim LastRow As Long

When you say that "you can't get this code to run", what do you mean exactly? What happens? Do you get an error? If so, which one and on which line?
 
Upvote 0
Keep getting error 424. Object required
Sounds like I am missing something...

This far I have:

Sub Graph()
Charts.Add

Dim rData As Range
Dim LastRow As Long

With Worksheets("Data")
LastRow=.Cells(Rows.Count,"AB").End(xlUp).Row
Set rData=.Range("AB1:AC" & LastRow).Value

ChartType=xlXYScatterSmoothNoMarkers
.ChartObjects("Chart1").SetSourceData Source:=rData
.PlotArea.ClearFormats
.HasTittle=True
.ChartTitle.Select
.ChartTitle.Text = "Data"
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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