VBA图表问题

时间:2015-05-22 09:01:08

标签: excel excel-vba charts rows vba

我合并了我在这些帖子中找到的两个代码,但现在图表没有显示范围中的数据(“B2:C2”)。我刚刚开始学习如何编写宏,所以请耐心等待。有人可以帮忙吗?

提前谢谢

Sub test()

Range("A2").Select

Do Until IsEmpty(ActiveCell)

 Dim ws As Worksheet
 Dim rng As Range

 Set ws = Sheets("Sheet1")
 Set rng = ws.Range("B2:C2").Offset(Row, 0)

 ActiveSheet.Shapes.AddChart.Select
 ActiveChart.SetSourceData Source:=Range(ws.Name & "!" & rng.Address)
 ActiveChart.ChartType = xlLineMarkers
 ActiveChart.PlotArea.Select
 ActiveChart.SeriesCollection(1).XValues = "='Sheet1'!$B$2:$C$2"
 ActiveChart.SeriesCollection(1).Name = ws.Range("A2").Offset(Row, 0).Value
 ActiveChart.Location Where:=xlLocationAsNewSheet
 ws.Select

ActiveCell.Offset(1, 0).Select

Loop

 Set ws = Nothing
 Set rng = Nothing
End Sub

2 个答案:

答案 0 :(得分:2)

您可以在For / Next循环中建立包含数据的最后一行并使用该行号。像这样:

Sub test()
 Dim Row As Integer, lastRow As Integer
 Dim ws As Worksheet
 Dim rng As Range

 Set ws = Sheets("Sheet1")
 lastRow = ws.Cells(Rows.Count, "B").End(xlUp).Row - 1
 Debug.Print lastRow

 For Row = 1 To lastRow
 Set rng = ws.Range("B1:C1").Offset(Row, 0)

 ActiveSheet.Shapes.AddChart.Select
 ActiveChart.SetSourceData Source:=Range(ws.Name & "!" & rng.Address)
 ActiveChart.ChartType = xlLineMarkers
 ActiveChart.PlotArea.Select
 ActiveChart.SeriesCollection(1).XValues = "='Sheet1'!$B$1:$C$1"
 ActiveChart.SeriesCollection(1).Name = ws.Range("A1").Offset(Row, 0).Value
 ActiveChart.Location Where:=xlLocationAsNewSheet
 ws.Select

 Next Row

 Set ws = Nothing
 Set rng = Nothing
End Sub

答案 1 :(得分:1)

另一种方式。避免使用string[] python_args = new string[] { @"C:\Work\Scripts\XML_Parsing.py", "arg1", "arg2" }; process.StartInfo.Arguments = String.Join(" ", python_args); .Select等...

.ActiveChart