ExcelVBA以两列数据绘制散点图 设置坐标轴的代码
时间: 2023-04-12 21:01:38 浏览: 268
Excel-VBA宏编程实例源代码-图表的处理-设置坐标轴刻度的类型.zip
以下是使用Excel VBA绘制散点图并设置坐标轴的代码示例:
Sub DrawScatterChart()
'定义变量
Dim xRange As Range
Dim yRange As Range
Dim chartRange As Range
Dim chartObj As ChartObject
Dim chart As Chart
'设置数据范围
Set xRange = Range("A1:A10")
Set yRange = Range("B1:B10")
Set chartRange = Union(xRange, yRange)
'创建图表对象
Set chartObj = ActiveSheet.ChartObjects.Add(Left:=100, Width:=400, Top:=100, Height:=300)
Set chart = chartObj.Chart
'设置图表类型
chart.ChartType = xlXYScatter
'设置数据源
chart.SetSourceData chartRange
'设置坐标轴
chart.Axes(xlCategory).HasTitle = True
chart.Axes(xlCategory).AxisTitle.Text = "X轴"
chart.Axes(xlValue).HasTitle = True
chart.Axes(xlValue).AxisTitle.Text = "Y轴"
End Sub
阅读全文