以以下這個簡單的代碼為例:
dim f() as double
dim h() as double
Private Sub Command1_Click() ‘’這是一個計算程序
redim f(2,2)
redim h(2,2)
'''f來自于一個excel
h=f
end sub
Private Sub Command2_Click()
if h為空陣列 then
msgbox"錯誤:未計算,請重新計算"
else
將h寫入另一個表格
end if
end sub
請問各位大神
,有誰知道怎么實作Command2的功能。主要是判斷空陣列的問題。
uj5u.com熱心網友回復:
Private Sub Command1_Click()
Dim f() As String
If CheckNull(f) Then MsgBox "空" Else MsgBox "非空"
ReDim f(1)
If CheckNull(f) Then MsgBox "空" Else MsgBox "非空"
ReDim f(1, 1)
If CheckNull(f) Then MsgBox "空" Else MsgBox "非空"
Erase f
If CheckNull(f) Then MsgBox "空" Else MsgBox "非空"
End Sub
'檢測是否空陣列
Private Function CheckNull(aryIn As Variant) As Boolean
On Error GoTo errhandle
Dim a As Long
a = LBound(aryIn)
CheckNull = False
Exit Function
errhandle:
CheckNull = True
End Function
uj5u.com熱心網友回復:
Private Declare Function VarPtr Lib "msvbvm60.dll" (ByRef Ptr() As Any) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Function IsEmptyArray(Arr() As Double) As Boolean
Dim Result As Long
CopyMemory Result, ByVal VarPtr(Arr), 4
IsEmptyArray = CBool(Result = 0)
End Function
uj5u.com熱心網友回復:
判斷動態陣列是否初始化Dim MyArray() As String, NumberOfElements As Long
If (Not MyArray) = -1 Then
NumberOfElements = 0 'The array is uninitialised
Else
NumberOfElements = UBound(MyArray) + 1
End If
uj5u.com熱心網友回復:
用API判斷:'API判斷陣列為慷訓沒有初始化
Private Declare Function SafeArrayGetDim Lib "oleaut32.dll" (ByRef saArray() As Any) As Long
值為0表示未初始化
uj5u.com熱心網友回復:
學習了.之前一直用API.這個好像很不錯...發現新大陸..
uj5u.com熱心網友回復:
Private Sub Command2_Click()將h寫入另一個表格
Dim i, j, k, num As Integer 'i,j,k是中間變數,num是確定矩陣的大小(具體幾*幾)
num = UBound(a) * UBound(a, 2)
For i = 0 To UBound(a) - 1 '從1維開始
For j = 0 To UBound(a, 2) - 1 '第2維度判斷
If a(i, j) = "" Then k = k + 1 '記錄空的個數
Next
Next
If k = num Then '判斷是不是矩陣都是空值
msgbox"錯誤:未計算,請重新計算":exit sub
else
'
end sub
[/code]
uj5u.com熱心網友回復:
你挖墳了,
陣列為空時 UBound 回傳值可能是-1,也有可能是報錯,
你這個代碼還是有問題的。
uj5u.com熱心網友回復:
在陣列中設定一個判斷用的位置。uj5u.com熱心網友回復:
你還是多讀幾年書再說吧…………
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/43626.html
標籤:VB基礎類
上一篇:VB 實作TCP/IP通信
