返回列表 上一主題 發帖

[發問] Is Nothing與Empty的用法

[發問] Is Nothing與Empty的用法

要確認某一陣列是否存在時該用什麼方法判斷?
ReDim HeadSone(20)
t1 = 0
t2 = 0
j = 0
For fsi = 15 To 238
    If Cells(fsi, 5) = Cells(fsi + 1, 5) And Cells(fsi, 5) <> Cells(fsi - 1, 5) Then t1 = fsi
    If Cells(fsi, 5) = Cells(fsi - 1, 5) And Cells(fsi, 5) <> Cells(fsi + 1, 5) Then t2 = fsi
        If t1 <> 0 And t2 <> 0 Then
            Set HeadSone(j) = Range(Cells(t1, 5), Cells(t2, 5))
            HS_S(j) = t1
            HS_E(j) = t2
            j = j + 1
            t1 = 0
            t2 = 0
        End If
Next fsi
m = j - 1   '共有m-1組
If HeadSone(0) = Empty Then  '判斷HeadSone(0)是否存在
    m = 0
Else
    m = j - 1   '共有m-1組
End If
ReDim Preserve HeadSone(m)

當HeadSone(0)不存在時上述寫法OK,可是當HeadSone(0)不存在時上述寫法就會出現錯誤(型態不符合)
改成:
If HeadSone(0) = Is Nothing Then
    m = 0
Else
    m = j - 1   '共有m-1組
End If

當HeadSone(0)不存在時則會出現錯誤(此次需要物件),可是當HeadSone(0)不存在時該寫法就OK
請問Is Nothing與Empty的用法有何不同?該如何改?

回復 4# GBKEE
另外
若是我想將HeadSone(m)全數清空
Set HeadSone(0)=Nothing
Set HeadSone(1)=Nothing
Set HeadSone(2)=Nothing
....
該如何寫比較好?

TOP

        靜思自在 : 心中常存善解、包容、感思、知足、惜福。
返回列表 上一主題