我想編寫一個函數,作爲輸入,應該在與上述對角線(從底部從左到右在其細胞VBA - 在For循環進入溢出錯誤
方陣頂部)的正數,和
對角線空單元的下方,
喜歡這個3×3的矩陣(第一小區包含#10,第二小區#5等):
10 5 1
9 6
4
在我對這個矩陣做任何事情之前,我需要驗證對角線上方和上方的單元格包含正數,而其他單元格是空的。我寫了下面的代碼。
問題是它在第二個For循環中給我一個溢出錯誤,對於我的生活,我不明白爲什麼。 對於3 x 3矩陣(即n = 3,p = 3),第二個For循環的第一行將爲:For i = 3 to 2 Step -1。這裏有什麼問題?
Option Base 1
Private Sub triangle1()
Dim y As Variant
y = Application.InputBox(_
Prompt:=" Choose a square matrix in a worksheet : ", Type:=8)
Dim i As Byte, j As Byte, n As Byte, p As Byte, s As Byte
n = UBound(y, 1)
p = UBound(y, 2)
Debug.Print LBound(y, 1), n, p
s = 1
If n <> p Then
Debug.Print "False"
Exit Sub
Else
' First For loop
For i = 1 To n
For j = 1 To p
If y(i, j) <= 0 Or Not IsNumeric(y(i, j)) Then
Debug.Print "False"
Exit Sub
End If
Next
p = p - 1
Next
' Second For loop
For i = n To 2 Step -1
s = s + 1
For j = s To p
If Not IsEmpty(y(i, j)) Then
Debug.Print "False"
Exit Sub
End If
Next
Next
Debug.Print "True"
End If
End Sub
這是因爲-1不會成爲其字節,請在即時窗格中嘗試cbyte(-1)。 –