一个不规则的循环型数组,在一列7个格内检测x1,然后在其前后自动填充其他,如何做?
7个不规则的数字组成数组:x1,x2....,x7
我在A1至A7的七个格里,随便一个填上x1,例如:我在A5填上x1
那么其余的要自动填上如下
A1: x4
A2: x5
A3: x6
A4: x7
A5: x1
A6: x2
A7: x3
这样的宏应该如何写???
谢谢!!!!
[解决办法]
算法很烂,恳请指正
Sub x1_to_x7()
Dim dic_num As Integer
dic_num = 7
Dim i, j, k, sum As Integer
Dim arr As Variant
arr = Array("x1", "x2", "x3", "x4", "x5", "x6", "x7")
'MsgBox arr(0)
j = 0
For Each cell In Selection
If cell.Value <> "" Then
k = 0
For i = 0 To dic_num - 1
If arr(i) = cell.Value Then
k = i
GoTo break
End If
Next i
Else
End If
j = j + 1
Next cell
GoTo jump
break:
i = 0
For Each cell In Selection
sum = k - j + i
While (True)
If sum < 0 Then
sum = sum + dic_num
ElseIf sum > dic_num - 1 Then
sum = sum - dic_num
Else
GoTo break2
End If
Wend
break2:
cell.Value = arr(sum)
i = i + 1
Next cell
jump:
End Sub