我对 EXCEL VBA 很陌生。请为我提供以下要求的帮助。
要求:需要一个 EXCEL VBA 宏来满足以下要求。
有n
列数。第一列是文本。所有其他列都是数字。我需要为
- 第 1 列(X 轴)和第 2 列(Y 轴)
- 第 1 列(X 轴)和第 3 列(Y 轴)
- .
- .
- . n. 第 1 列(X 轴)和第 n 列(Y 轴)
所以状态是第一列是一直不变的。如何通过动态更改列来循环上述状态?
如果我没有解释清楚,请告诉我。
基本上这是您录制的宏的通用版本,它将循环所有列,所有行。
Sub PlotLineGraph()
Dim height As Long
Dim width As Long
Dim sourceWs As Worksheet
Dim targetWs As Worksheet
Dim targetWorksheetName As String
Dim targetRange As Range
Dim targetChart As Chart
Dim chartsPerRow As Long
Dim chartWidth As Double
Dim chartHeight As Double
'customize the parameters
chartsPerRow = 2
chartWidth = 300
chartHeight = 300
Set sourceWs = Worksheets("Sheet1") ' replace Sheet1 with the sheet name the data is stored ( Case sensitive)
targetWorksheetName = "Sheet2" ' the output sheet name
Set targetWs = Worksheets(targetWorksheetName)
'--------------------------------------------------------------------------------
'optional
'this part deletes all previous shapes on the target worksheet
For Each Shape In targetWs.Shapes
Shape.Delete
Next Shape
'--------------------------------------------------------------------------------
With sourceWs
height = .Cells(.Rows.Count, 1).End(xlUp).Row ' getting height of column A
width = .Cells(1, .Columns.Count).End(xlToLeft).Column 'getting columns of row 1
If width > 1 Then
For j = 2 To width
targetWs.Activate
targetWs.Shapes.AddChart.Select
ActiveChart.ChartType = xlLine
Set targetChart = ActiveChart
.Activate
Set targetRange = Union(.Range(.Cells(1, 1), .Cells(height, 1)), .Range(.Cells(1, j), .Cells(height, j)))
targetChart.SetSourceData Source:=targetRange
With targetChart.Parent
.height = chartHeight
.width = chartWidth
.Top = CLng((j - 2) / chartsPerRow) * chartHeight
.Left = ((j - 2) Mod chartsPerRow) * chartWidth
End With
Next j
End If
End With
End Sub