简体   繁体   中英

How to incorporate relative references into VBA Macro

I am trying to create 400 charts, one for each student in my school. the data for the loop is set up in columns A..E with the data labels in row 1 and then the data for each student appearing in successive rows (2-400)

I have created a for/ next loop that steps through each row of data and creates a radar graph (using a user defined graph called CR3) for each student.

For num = 3 To 400
Range("A1:E1").Select
    ActiveCell.Offset(num, 0).Range("A1").Activate
Charts.Add
ActiveChart.ApplyCustomType ChartType:=xlUserDefined, TypeName:= _
    "CR3"
ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("A1:E1,A" & num & ":E" & num,)
ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1

I now want to add a new series of information to the graph. The new information appears in columns H to L for each student. How do I add the new series into the row that starts (Active.chart.setsourceData Source...)

After creating the graph add the following lines

ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(2).Name = "=""Series 2 Name"""
ActiveChart.SeriesCollection(2).Values = "='Sheet1'!$B$1:$B$6"  

HTH!

that was very helpful and got me thinking. I ultimately got it using the following code

ActiveChart.ApplyCustomType ChartType:=xlUserDefined, TypeName:= _
    "CR3"        
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).Name = Sheets("Sheet1").Range("Am" & num)
ActiveChart.SeriesCollection(1).Values = Sheets("Sheet1").Range("b" & num & ":e" & num)

ActiveChart.SeriesCollection(2).Name = Sheets("Sheet1").Range("Aq" & num) ActiveChart.SeriesCollection(2).Values = Sheets("Sheet1").Range("I" & num & ":L" & num)

Note that "AM", "b", "e", "aq","i" and "L" are columns where the data resides.

I never could have gotten it without your help. thanks.

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