VB如何判断数组为空矩阵

weixin_39619172 2017-12-22 04:48:21
以以下这个简单的代码为例:

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的功能。主要是判断空数组的问题。
...全文
1916 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
舉杯邀明月 2018-08-18
  • 打赏
  • 举报
回复
引用 8 楼 zhanghaaaa 的回复:
在数组中设置一个判断用的位置。

你还是多读几年书再说吧…………

zhanghaaaa 2018-08-18
  • 打赏
  • 举报
回复
在数组中设置一个判断用的位置。
笨狗先飞 2018-08-10
  • 打赏
  • 举报
回复
引用 6 楼 qq_42927700 的回复:
Private Sub Command2_Click()
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
'
将h写入另一个表格
end sub
[/code]

你挖坟了,
数组为空时 UBound 返回值可能是-1,也有可能是报错,
你这个代码还是有问题的。
qq_42927700 2018-08-09
  • 打赏
  • 举报
回复
Private Sub Command2_Click()
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
'
将h写入另一个表格
end sub
[/code]
小噤 2018-01-26
  • 打赏
  • 举报
回复
引用 3 楼 zzyong00 的回复:
判断动态数组是否初始化 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
学习了.之前一直用API.这个好像很不错...发现新大陆..
东方之珠 2018-01-26
  • 打赏
  • 举报
回复
用API判断: 'API判断数组为空或没有初始化 Private Declare Function SafeArrayGetDim Lib "oleaut32.dll" (ByRef saArray() As Any) As Long 值为0表示未初始化
zzyong00 2018-01-13
  • 打赏
  • 举报
回复
判断动态数组是否初始化 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
笨狗先飞 2017-12-23
  • 打赏
  • 举报
回复

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
脆皮大雪糕 2017-12-22
  • 打赏
  • 举报
回复

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

7,759

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧