r = 10
c = 10
Value = 1
Cells(r, c) = Value
For Count = 1 To 6^2
rc = yx(方向 Mod 4)
n = CLng(Count / 2 + 0.001)
For Repeat = 1 To n
r = r + rc(1)
c = c + rc(2)
Value = Value + 1
If Repeat = n Then
Cells(r, c) = yx((方向 + 1) Mod 4)(4) & " " & Value
Else
Cells(r, c) = rc(3) & " " & Value
End If
Next
方向 = 方向 + 1
Next
n = InputBox("輸入排列數字", , 25)
[A1:Z26].ClearContents
x = xc: y = yc: xp1 = x: yp1 = y
Cells(x, y) = 1
f1 = "": f2 = ""
For i = 2 To n
ds = [{"","","",""}]
For j = 1 To 4 '四個方向
yy = yx(j)(1): xx = yx(j)(2)
If Cells(y + yy, x + xx) = "" Then
ds(j) = ((y + yy - yc) ^ 2 + (x + xx - xc) ^ 2) * 10 + j '距離*10+方向
End If
Next
k = Application.Min(ds) Mod 10 '取距離原點最短及排列最前方向
Sub Ex()
'矩陣可為5*5,6*6,7*7,..256*256...視Excel版本
Dim xNo As Double, Rng As Range, Rc As Double, i As Integer
ActiveSheet.UsedRange.Clear
xNo = Application.InputBox("輸入排列數字", , 25, Type:=1)
Rc = Application.Evaluate("CEILING(Sqrt(" & xNo & "),1)")
If Rc Mod 2 <> 0 Then i = 1
If Int(Rc / 2) + i > Int(Columns.Count / 2) Then
MsgBox "輸入排列數字" & xNo & "的欄位數 " & Int(Rc / 2) + i & vbLf & "大於 工作表之總欄位/ 2 =>" & Columns.Count / 2
End
End If
Set Rng = Cells(Int(Rc / 2) + i, Int(Rc / 2) + i) '以A1為相對位置的中心儲存格
Rng.Select
Rng.Interior.Color = vbRed
Rng = 1
Do
'右,下,左,上 順時鐘方向
Do
If Rng >= Rc * Rc Then End
Rng.Offset(, 1) = Rng + 1
Set Rng = Rng.Offset(, 1) '右移一欄
Loop Until Rng.Offset(1) = "" '下一列 = ""
Do
Rng.Offset(1) = Rng + 1
Set Rng = Rng.Offset(1) '下移一列
Loop Until Rng.Offset(, -1) = "" '左一欄 = ""
Do
If Rng >= Rc * Rc Then End
Rng.Offset(, -1) = Rng + 1
Set Rng = Rng.Offset(, -1) '左移一欄
Loop Until Rng.Offset(-1) = "" '上一列 = ""
Do
Rng.Offset(-1) = Rng + 1
Set Rng = Rng.Offset(-1) '上移一列
Loop Until Rng.Offset(, 1) = "" '右一欄 = ""
Loop
只會基本的Range判定方法,以下程式碼供參考,
對初學VBA者應很容易看得懂: <四輪定位法> Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub 數字矩陣_四輪定位法()
Dim N, xR As Range, M(1 To 4) As Range, C%, R%, X%, Y%
N = InputBox("請輸入方陣欄列數", , 11)
Cells.ClearContents
Set xR = Cells(N / 2 + 2, N / 2 + 2): Set M(1) = xR(2)
For i = 2 To 4: Set M(i) = xR: Next
R = 1: C = 2
For j = 1 To N * N
xR = j: Set xR = xR(R, C): X = xR.Row: Y = xR.Column
If X < M(1).Row And Y > M(1).Column Then R = 2: C = 1: Set M(1) = xR
If X > M(2).Row And Y > M(2).Column Then R = 1: C = 0: Set M(2) = xR
If X > M(3).Row And Y < M(3).Column Then R = 0: C = 1: Set M(3) = xR
If X < M(4).Row And Y < M(4).Column Then R = 1: C = 2: Set M(4) = xR
Sleep 10
101: Next
End Sub
'===================================
Sub 方向矩陣_四輪定位法()
Dim N, xR As Range, M(1 To 4) As Range, C%, R%, X%, Y%, T$
N = InputBox("請輸入方陣欄列數", , 11)
Cells.ClearContents
Set xR = Cells(N / 2 + 2, N / 2 + 2): Set M(1) = xR(2)
For i = 2 To 4: Set M(i) = xR: Next
R = 1: C = 2: T = "◎"
For j = 1 To N * N - 1
If xR = "" Then xR = T
Set xR = xR(R, C): X = xR.Row: Y = xR.Column
If X < M(1).Row And Y > M(1).Column Then R = 2: C = 1: T = "↓": Set M(1) = xR: xR = "┐"
If X > M(2).Row And Y > M(2).Column Then R = 1: C = 0: T = "←": Set M(2) = xR: xR = "┘"
If X > M(3).Row And Y < M(3).Column Then R = 0: C = 1: T = "↑": Set M(3) = xR: xR = "└"
If X < M(4).Row And Y < M(4).Column Then R = 1: C = 2: T = "→": Set M(4) = xR: xR = "┌"
Sleep 10
101: Next
xR = "●"
End Sub作者: 准提部林 時間: 2015-10-27 14:40
<牆角摸索法> Sub 數字矩陣_牆角摸索法()
Dim i&, xR As Range, R&, C&, N
N = InputBox("輸入欄列數", , 11)
Cells.ClearContents
Set xR = Cells(N / 2 + 2, N / 2 + 2): xR = 1: Set xR = xR(1, 2)
For i = 2 To N * N
If xR(1, 0) <> "" And xR(2, 1) = "" Then R = 2: C = 1
If xR(0, 1) <> "" And xR(1, 0) = "" Then R = 1: C = 0
If xR(1, 2) <> "" And xR(0, 1) = "" Then R = 0: C = 1
If xR(2, 1) <> "" And xR(1, 2) = "" Then R = 1: C = 2
xR = i: Set xR = xR(R, C)
Sleep 10
Next i
End Sub
'====================================
Sub 方向矩陣_牆角摸索法()
Dim i&, xR As Range, R&, C&, T$, N
N = InputBox("輸入欄列數", , 11)
Cells.ClearContents
Set xR = Cells(N / 2 + 2, N / 2 + 2): xR = "◎": Set xR = xR(1, 2)
For i = 2 To N * N - 1
If xR(1, 0) <> "" And xR(2, 1) = "" And xR(0, 1) = "" Then R = 2: C = 1: xR = "┐": T = "↓"
If xR(0, 1) <> "" And xR(1, 0) = "" And xR(1, 2) = "" Then R = 1: C = 0: xR = "┘": T = "←"
If xR(1, 2) <> "" And xR(0, 1) = "" And xR(2, 1) = "" Then R = 0: C = 1: xR = "└": T = "↑"
If xR(2, 1) <> "" And xR(1, 2) = "" And xR(1, 0) = "" Then R = 1: C = 2: xR = "┌": T = "→"
If xR = "" Then xR = T
Set xR = xR(R, C)
Sleep 10
Next i
xR = "●"
End Sub作者: 准提部林 時間: 2015-10-27 14:41
<十字非空檢查法> Sub 數字矩陣_十字非空檢查法()
Dim i&, xR As Range, R&, C&, N, j&, k&
N = InputBox("輸入欄列數", , 11)
Cells.ClearContents
Set xR = Cells(N / 2 + 2, N / 2 + 2): xR = 1: Set xR = xR(1, 2)
For i = 2 To N * N
For j = 1 To 4
If xR(0, 0).Resize(3, 3).Item(j * 2) <> "" Then k = k & j
Next j
If k < 5 Then R = Mid("1201", k, 1): C = Mid("0112", k, 1)
xR = i: Set xR = xR(R, C): k = 0
Sleep 10
Next i
End Sub
'====================================
Sub 方向矩陣_十字非空檢查法()
Dim i&, xR As Range, R&, C&, N, j&, k&, T$
N = InputBox("輸入欄列數", , 11)
Cells.ClearContents
Set xR = Cells(N / 2 + 2, N / 2 + 2): xR = "◎": Set xR = xR(1, 2)
For i = 2 To N * N - 1
For j = 1 To 4
If xR(0, 0).Resize(3, 3).Item(j * 2) <> "" Then k = k & j
Next j
If k < 5 Then
R = Mid("1201", k, 1): C = Mid("0112", k, 1)
T = Mid("←↓↑→", k, 1): xR = Mid("┘┐└┌", k, 1)
End If
If xR = "" Then xR = T
Set xR = xR(R, C): k = 0
Sleep 10
Next i
xR = "●"
End Sub作者: n7822123 時間: 2015-10-30 11:23