|
楼主 |
发表于 2024-6-19 09:18
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 jx928867128 于 2024-6-21 22:25 编辑
这是我手抄版主帮忙写的代码,分享给有同样需求教务同行
Sub 按钮8_Click()
Sum = 0
Sheets(2).[a1].CurrentRegion.Offset(1, 1).ClearContents
lxl:
Sum = Sum + 1
If Sum = 999 Then
MsgBox "GAME OVER"
Exit Sub
End If
Set d = CreateObject("scripting.dictionary")
Set dw = CreateObject("scripting.dictionary")
Set dd = CreateObject("scripting.dictionary")
arr = Sheets(1).[a1].CurrentRegion
For j = 2 To UBound(arr)
Set dd(arr(j, 1)) = CreateObject("scripting.dictionary")
For i = 2 To UBound(arr, 2)
If Not d.exists(arr(j, i)) And Len(arr(j, i)) > 0 Then
Set d(arr(j, i)) = CreateObject("scripting.dictionary")
End If
d(arr(j, i))(arr(j, 1)) = -1
dd(arr(j, 1))(arr(j, i)) = 0
Next i
Next j
arr = Sheets(2).[a1].CurrentRegion
For i = 2 To UBound(arr, 2)
For j = 2 To UBound(arr)
For w = -1 To UBound(arr, 2)
dw.RemoveAll
For x = 1 To d.Count
dw(x) = ""
Next x
For xx = 1 To d.Count
y = WorksheetFunction.RandBetween(0, dw.Count - 1)
x = dw.keys()(y)
dw.Remove x
k = d.keys()(x - 1)
If d(k).exists(arr(j, 1)) Then
If d(k)(arr(j, 1)) = w Then
zz = 0
For Each kk In d(k).items
If kk = i Then zz = 1
Next
If zz = 0 Then
arr(j, i) = k
d(k)(arr(j, 1)) = i
GoTo 11
End If
End If
End If
Next xx
Next w
11:
Next j
Next i
For j = 2 To UBound(arr)
For i = 2 To UBound(arr, 2)
If Len(arr(j, i)) = 0 Then
For n = 0 To dd(arr(j, 1)).Count - 1
k = dd(arr(j, 1)).keys()(n)
If k <> arr(j, i - 1) Then
If i < UBound(arr, 2) Then
If k <> arr(j, i + 1) Then
For w = 2 To UBound(arr)
If w <> j Then
If k = arr(w, i) Then
For Z = 2 To UBound(arr, 2)
For y = 2 To UBound(arr)
If arr(y, Z) = k Then GoTo 12
If arr(w, Z) = arr(y, i) Then GoTo 12
Next y
arr(j, i) = k
tm = arr(w, Z)
arr(w, Z) = k
arr(w, i) = tm
GoTo 13
12:
Next Z
End If
End If
Next w
End If
End If
End If
Next n
13:
End If
Next i
Next j
For j = 2 To UBound(arr)
For i = 2 To UBound(arr, 2)
If Len(arr(j, i)) = 0 Then
GoTo lxl:
End If
Next i
Next j
For j = 2 To UBound(arr)
For i = 4 To UBound(arr, 2) - 1
If arr(j, i - 1) = arr(j, i - 2) Then
If arr(j, i) = arr(j, i - 1) Or arr(j, i + 1) = arr(j, i - 1) Then
GoTo lxl:
End If
End If
Next i
Next j
For j = 2 To UBound(arr)
For i = 2 To UBound(arr, 2) - 3
If arr(j, i + 2) = arr(j, i + 3) Then
If arr(j, i) = arr(j, i + 2) Or arr(j, i + 1) = arr(j, i + 2) Then
GoTo lxl:
End If
End If
Next i
Next j
Sheets(2).[a1].CurrentRegion = arr
MsgBox "数据提取完毕!"
End Sub
|
|