我一直在尝试创建一个程序来自动解决在Excel中使用VBA作为项目的数独游戏。ReDim在循环过程中导致运行时错误9
我遇到了一个问题,在我的数组没有得到分配给它们的正确值后,通过一个循环来拾取它们。我已经为3x3矩阵进行了这种设计工作,但它在9x3x3(IE完全数独谜题)方面存在问题。
我检查了ReDim文档并意识到它只能重新分配数组的最后一个维度。正因为如此,我试图去处理逻辑,所以它永远不需要改变前两个维度的大小。我仍然遇到一个错误,当试图从数组中调用一个值时,我被认为超出了数组的范围。我已经使用调试器一步一步地完成了程序,并检查它是否正确地分配了所有值。我对自己为什么不能正常工作感到无能为力,因此有没有人可以给我的建议?
Dim guessArray() As integer
Dim blockArray() As Integer
Dim blockCheckArray() As integer
Dim correctArray() As Integer
Dim lenBlockArray (3, 3, 1) As Integer
Dim lenBlockCheck(3, 3, 1) As integer
Dim lenBlockCheckArray (3, 3, 1) As integer
Dim counter as integer
'Putting the sudoku into an array
ReDim guessArray (9, 9) As integer
For i = 1 to 9
For j = 1 to 9
guessArray (i, j) = Cells (i, j)
Next
Next
'Creating an array to find the numbers that are in a quadrant
ReDim blockCheckArray(3, 3, 1) As integer
For a = 1 to 3
For b = 1 to 3
For i = 1 to 3
For j = 1 to 3
'Looking through each quadrant for any number != 0 and dumping them into an array
If guessArray (i + ((a - 1) * 3), j + ((b - 1) * 3)) <> 0 Then
lenBlockArray (a, b, 1) = lenBlockArray (a, b, 1) + 1
ReDim Preserve blockArray (3, 3, lenBlockArray (a, b, 1)) As Integer
blockArray (a, b, lenBlockArray (a, b, 1)) = guessArray (i + ((a - 1) * 3), j + ((b - 1) * 3))
End If
Next
Next
Next
Next
'Writing out the numbers in the quadrants for debugging
counter = 1
For a = 1 to 3
For b = 1 to 3
For i = 1 to lenBlockArray (a, b, 1)
Cells (counter, i + 10) = blockArray (a, b, i)
Next
Next
Next
我使用Excel 2010中这是我在计算器上的第一篇文章,它从我的手机是(我在我的停机时间做这个项目的工作),所以如果有任何改变格式或任何细节关于我收到的错误,请随时提问。
缺少'lenBlockArray()'函数。请发布,以便提供帮助的人可以浏览您的代码。 –
@AdamVincent排序,谢谢。在移动设备上发布信息的危险。 – Hutsan