|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
顺时针和逆时针,只要通过转置就可以了。- Sub test44()
- Dim i&, j&, k1&, k2&, r1&, c1&, r2&, c2&, r&, n&, x&, y&, z&
-
- [a1].CurrentRegion = ""
- x = InputBox("行数x=", , Int(Rnd * 50 + 5))
- y = InputBox("列数y=", , Int(Rnd * 25 + 3))
- z = MsgBox("螺旋方向z=: Yes ??? / No 逆??", vbYesNo)
- If z = vbNo Then r = x: x = y: y = r
- ReDim a&(1 To x, 1 To y)
- If x < y Then r = x Else r = y
- r1 = 1: c1 = 1: r2 = x: c2 = y: k1 = 1: k2 = k1 + x + y - 2
- For n = 1 To r - 1 Step 2
- For j = 0 To c2 - c1 - 1
- a(r1, c1 + j) = k1 + j
- a(r2, c2 - j) = k2 + j
- Next
- k1 = k1 + j: k2 = k2 + j
- For j = 0 To r2 - r1 - 1
- a(r1 + j, c2) = k1 + j
- a(r2 - j, c1) = k2 + j
- Next
- k1 = k2 + j: k2 = k1 + x + y - n * 2 - 4
- r1 = r1 + 1: c1 = c1 + 1: r2 = r2 - 1: c2 = c2 - 1
- Next
- If c1 > 1 Then If z = vbYes Then Cells(r2 - j + 2, c1 - 1).Activate Else Cells(c1 - 1, r2 - j + 2).Activate
-
- If r Mod 2 Then
- If x < y Then
- For j = 0 To c2 - c1
- a(r1, c1 + j) = k1 + j
- Next
- If z = vbYes Then Cells(r1, c1 + j - 1).Activate Else Cells(c1 + j - 1, r1).Activate
- Else
- For j = 0 To r2 - r1
- a(r1 + j, c2) = k1 + j
- Next
- If z = vbYes Then Cells(r1 + j - 1, c2).Activate Else Cells(c2, r1 + j - 1).Activate
- End If
- End If
- Application.StatusBar = x & " x " & y & " = " & x * y
- If z = vbYes Then [a1].Resize(x, y) = a Else [a1].Resize(y, x) = Application.Transpose(a)
- End Sub
复制代码 |
|