|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub ywy() '2024-5-15
Dim n As Integer
Dim matrix() As Integer
Dim num As Integer
Dim left As Integer, right As Integer, top As Integer, bottom As Integer
Dim i As Integer, j As Integer
num = 1
n = 9 ' 假设填充一个9x9的矩阵
ReDim matrix(1 To n, 1 To n)
left = 1
right = n
top = 1
bottom = n
While left <= right And top <= bottom
For i = left To right
matrix(top, i) = num
num = num + 1
Next i
top = top + 1
For i = top To bottom
matrix(i, right) = num
num = num + 1
Next i
right = right - 1
For i = right To left Step -1
matrix(bottom, i) = num
num = num + 1
Next i
bottom = bottom - 1
For i = bottom To top Step -1
matrix(i, left) = num
num = num + 1
Next i
left = left + 1
Wend
For i = 1 To n
For j = 1 To n
Cells(i + 1, j) = matrix(i, j)
Next j
Next i
End Sub
|
|