generate a graph with variables length of data in VBA

fares paris

New Member
Joined
Jun 5, 2013
Messages
3
I'm curently working in a workbook where data from other sheets are copied onto, after having recieved all the data (wich are of variable legth) the macro adds a Time column, previously left blak, and then generates a chart. I ran the code in several pieces and using the method above I end up with an error 13 on my chart function, I'm not sure the error comes from. could any one advise me on the path to follow?

Code:
Sub t_feuille(f_Work As Worksheet)


Dim num_line As Integer, t_Work As Worksheet 
Dim myrange As Range

'instals time column
f_Work.Activate
f_Work.Cells(1, 1) = "Time"
For num_line = 2 To last_line(t_Work)
  f_Work.Cells(num_line, 1) = num_line - 2
Next

'graph set up

  Set myrange = ActiveSheet.Cells(1, 1).CurrentRegion
  Application.CutCopyMode = False
  ActiveChart.ChartWizard _
    Source:=Sheets(f_Work).Range(myrange), _
    Gallery:=xlXYScatterSmoothNoMarkers, Format:=4, PlotBy:=xlColumns, _
    CategoryLabels:=1, SeriesLabels:=1, HasLegend:=1, _
    Title:=f_Work, CategoryTitle:="", _
    ValueTitle:="", ExtraTitle:=""

End Sub


Function last_line(f_Work) As Integer
Dim num_col As Integer, num_line As Integer

  num_line = 0
  For num_col = 1 To ActiveSheet.Cells(1, 10000).End(xlToLeft).Column
    If ActiveSheet.Cells(100000, num_col).End(xlUp).Row > num_line Then
      num_line = ActiveSheet.Cells(100000, num_col).End(xlUp).Row
    End If
Next

  last_line = num_line
End Function
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hello
Please test this version:

Code:
Sub Main()
    t_feuille Sheets("Sheet2")
End Sub


Sub t_feuille(f_Work As Worksheet)


Dim num_line%, t_Work As Worksheet, myrange As Range, dummy$


'creates time column
f_Work.Activate
f_Work.Cells(1, 1) = "Time"
For num_line = 2 To lastrow(ThisWorkbook.Name, f_Work.Name)
  f_Work.Cells(num_line, 1) = num_line - 2
Next


Set myrange = ActiveSheet.Cells(1, 1).CurrentRegion
Application.CutCopyMode = False
On Error Resume Next
dummy = ActiveChart.Name
If Err <> 0 Then Exit Sub   ' there is no active chart
On Error GoTo 0
ActiveChart.ChartWizard Source:=myrange, Gallery:=xlXYScatterSmoothNoMarkers, _
Format:=4, PlotBy:=xlColumns, CategoryLabels:=1, SeriesLabels:=1, HasLegend:=1, _
Title:=f_Work.Name, CategoryTitle:="", ValueTitle:="", ExtraTitle:=""


End Sub


Public Function lastrow(wname$, which$) As Long
    Workbooks(wname).Sheets(which).Activate
    If WorksheetFunction.CountA(Cells) = 0 Then
        lastrow = 0
        Exit Function
    End If
    lastrow = Cells.Find(What:="*", After:=[a1], SearchOrder:=xlByRows, _
    SearchDirection:=xlPrevious).Row
End Function
 
Upvote 0
What would the issues be?
If you describe them, we can tackle them...
 
Upvote 0

Forum statistics

Threads
1,216,746
Messages
6,132,478
Members
449,729
Latest member
davelevnt

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