12、有趣的画线
NlZbXA9u.rar
(11.57 KB, 下载次数: 393)
Private Sub CommandButton1_Click()
Dim rng As Range, c As Range
Dim arr()
Dim k%, i%
Set rng = [a1:h26].SpecialCells(xlCellTypeConstants) '定位a1:h26里所有常量的单元格
ReDim arr(1 To rng.Count, 1 To 2) '重新定义一个数组,记录每个有数字的单元格的坐标
For Each c In rng
k = k + 1
arr(k, 1) = c.Left + c.Width / 2 '单元格的右侧距离+单元格本身宽度的一半,即等于单元格正中间距左侧的位置,X
arr(k, 2) = c.Top + c.Height / 2 '单元格的顶端距离+单元格本身高度的一半,即等于单元格正中间距顶端的位置,Y
Next
For i = 1 To k - 1 '增加画线,四个数据为:起点的X坐标,Y坐标,终点的X坐标,Y坐标
ActiveSheet.Shapes.AddLine arr(i, 1), arr(i, 2), arr(i + 1, 1), arr(i + 1, 2)
Next
End Sub
|