VBA代码记录4 - 散点图

时间:2025-04-25 18:17:36
Sub 散点图() For i = 2 To '遍历需要创建散点图的Sheet Sheets(i).Select lastrow = For j = 6 To 7 '把第六、第七列的数据作为Y轴 If Cells(1, j).Value <> -99 Then Sheets(i).Shapes. '创建图表,并Select For k = To 1 Step -1 '删除默认的系列,Office2016有五个默认的系列且全部显示 (k).Delete Next k = xlXYScatterLines '设置图标属性为带直线与数据标记的散点图 '新建系列,一条折线表示一个系列 (1).XValues = "='" & & "'!" & "$E$1:$E$" & lastrow 'E列数据作为X轴数据 (xlCategory, xlPrimary).MinimumScale = (Range("E1:E" & lastrow)) '设置横坐标的最小值为E列的最小值,设置X轴数据后才能设置最小值 If j = 6 Then (1).Name = "depth" '新插入的数据系列名称,用于图例显示 (1).Values = "='" & & "'!" & "$F$1:$F$" & lastrow '一列数据作为Y轴数据 sensortype = "depth" Else (1).Name = "pressure" '新插入的数据系列名称,用于图例显示 (1).Values = "='" & & "'!" & "$G$1:$G$" & lastrow '一列数据作为Y轴数据 sensortype = "pressure" End If = & " " & sensortype '设置图标名称 End If Next j = 300 '设置图表位置,距编辑区域顶部 (1).Left = 300 '设置图表位置,距编辑区域左侧 If > 1 Then '多个图表排列 (2).Left = 900 + (1).Width + 100 End If Next i End Sub