VBA Graph

ThijsVBANoob

New Member
Joined
Jan 10, 2024
Messages
1
I'm really new at this and need help.
after searching the web I created 2 VBA's to plot a graph from data ranges however

1 VBA is going correctly as the datarange is in consecutive order and easy added
however the 2nd test VBA that I want to test (and eventually implement in my main project) is not working.

Setup is columns A to AB but my datarange source for the graph needs to be column B-D-E etc. till last data column

Using Union function and determine last data and last row it's not plotting a graph but also not coming up want a runtime error.

'Data needs to be entered as followed:
'Data from row 2 till lastRow of with data entry
'Data from Column x till lastcolumn
'Column data need to be Column skipped = B (skipping C) D (skipping E) till lastcolumn

The code that I've so far is

Dim ws As Worksheet
Dim rng As Range
Dim cht As Object
Dim LastRow As Long
Dim LastColumn As Long
Dim currentcol As Long

Set ws = ThisWorkbook.Sheets("DataFilter")

Set cht = Sheets("Chart").Shapes.AddChart2
cht.Chart.ChartType = xlLine

LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
LastColumn = ws.Cells(2, ws.Columns.Count).End(xlToLeft).Column

Set rng = ws.Range(ws.Cells(2, 1), ws.Cells(LastRow, 1))

For currentcol = 3 To lastcol Step 2
Set rng = Union(rng, ws.Cells(1, currentcol).Resize(LastRow))
Next currentcol

cht.Chart.SetSourceData Source:=rng

Hope somebody can help me
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
try this on a blank workbook:
Put some data in columns A to K :
Put the row numbers in column A with
Excel Formula:
=ROW()
and then in columns B to K I put :
Excel Formula:
=COLUMN()+$A1
This gives data where you can clearly see which column is plotted, then run this code:
VBA Code:
Sub test()
Dim ran1 As Range
lrow = Cells(Rows.Count, "B").End(xlUp).Row
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlLine
    ActiveChart.SetSourceData Source:=Range("Sheet1!$B$1:$B" & lrow)

    For i = 1 To 6 Step 1
    Set ran1 = ActiveSheet.Range(Cells(1, i * 2), Cells(lrow, i * 2))
    
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(i).Name = "column! & i"
    ActiveChart.SeriesCollection(i).Values = ran1
    ActiveChart.SeriesCollection(i).AxisGroup = 1
    
    Next i
    

End Sub
If you want more or less column just change the loop index i=1 to 6 or you can detect it automatically
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,976
Members
449,095
Latest member
Mr Hughes

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