简体   繁体   中英

VBA Code to Actively Find Range for Excel Graph

I'm currently building a VBA code to actively define the range for a graph.

Sub ABC()

Dim count As Integer
Dim countAll As Integer
Dim i As Integer
Dim j As Integer
Dim filter As Variant
Dim exfilter As Variant
Dim ws As Worksheet
Set ws = Worksheets(1)
Dim n As Integer

'Cancel updating
Application.ScreenUpdating = False

'Clear oldsheet
Sheets("FLAGGED rest # list").Activate
Range("a3:a9999").ClearContents

'Autofilter sheet and copy range
Sheets("restaurant performance 2016").Activate
Range("a7:ah7").Select
Selection.AutoFilter
ActiveSheet.Range("$A$7:$AH$510").AutoFilter Field:=11, Criteria1:="<>"
ActiveSheet.AutoFilter.Range.Copy

'Paste to new sheet
Sheets("FLAGGED rest # list").Activate
Cells(2, 1).Select
ActiveSheet.Paste
'
'Clean out irrelevant data
Range("b2:az9999").Clear

'Count number of restaurants
Sheets("flagged rest # list").Activate
Cells(3, 1).Select
Sheets("flagged rest # list").Range(Selection, Selection.End(xlDown)).Select
count = Application.WorksheetFunction.CountA(Selection)

'Count number of weeks
Sheets("12 week trend").Activate
Cells(2, 2).Select

'Create Chart
n = 2 + count
rangestring = "$C$3:$O$" & n
rangestring2 = "'12 week trend'!" & rangestring
Sheets("12 week trend").Activate
Range(rangestring).Select
ActiveSheet.Shapes.AddChart2(332, xlLineMarkers).Select
ActiveChart.SetSourceData Source:=Range(rangestring2)

For i = 3 To n
    legendString = "$B$" & i
    legendString2 = "='12 week trend'!" & legendString
    ActiveChart.FullSeriesCollection(i - 2).Name = legendString2
Next i
ActiveChart.FullSeriesCollection(1).XValues = "='12 Week Trend'!$C$2:$O$2"

'Turn on updating
Application.ScreenUpdating = True

End Sub

My main hurdle seems to be the fact that I need the code to be able to accommodate data being added to new columns. For example, in this code: the data range is hardcoded to column "O", but I'd like to build a model which would be able to determine which column the range stops at.

The variable rangestring2 dynamically calculates how many rows of data we possess, but at the moment, can't calculate the same for columns.

This is my first time asking for help in this forum! Thank you for your time guys!

You could use one of the usual "find last column" methods, such as

Dim c As Long
With Sheets("12 week trend")
    c = .Cells(2, .Columns.Count).End(xlToLeft).Column
End With

and then use that to determine your appropriate range strings such as

Dim HdgString As String
rangestring = Range(Cells(3, "C"), Cells(n, c)).Address
HdgString = Range(Cells(2, "C"), Cells(2, c)).Address
rangestring2 = "'12 week trend'!" & rangestring
'...
ActiveChart.FullSeriesCollection(1).XValues = "='12 Week Trend'!" & HdgString

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM