2016-03-03 17 views
0

在我正在構建的Excel VBA模塊中,如果超限貨物的表包含任何內容,我需要執行一些代碼。我最初寫這個:我在這個IsEmpty子句中缺少什麼?

If Not IsEmpty(Range("OOGData")) Then 
... 
Else 
... 
End If 

但即使OOGData 空的,它使返回False。我試着用If IsEmpty(Range(「OOGData」))= False Then`,但這似乎沒有任何區別。我當前的代碼是

If IsEmpty(Range("OOGData")) = False Then 

...但是,這仍然空範圍內激活。

我確定沒有公式,隱藏值或任何可能出現的內容。

任何想法可能是什麼問題?

+1

你清除了細胞?也就是說,刪除內容,我相信,不會清空單元格,相反,我認爲它包含0長度的字符串。所以檢查可能需要檢查它是空的還是長度爲0的字符串(這是我的頭,因此它可能不完全正確)。這是我用過的一些代碼'如果IsNull(cv)或IsEmpty(cv)或c​​v =「」Then' – MikeT

回答

1

根據this information

返回一個布爾值指示變量是否已經 初始化。

在你的代碼中你沒有使用變量,所以你不應該期待正確的值。

當檢查單細胞,你應該使用Len()函數:

If Len(Range("OOGData"))=0 Then 
    'cell is empty 

何時檢查,如果細胞的範圍是空的使用此解決方案:

If WorksheetFunction.CountA(Range("OOGData"))=0 Then 
    'rabge is empty 

我能想到的最後一種選擇是使用循環。

0

我決定作弊了。無論何時將OOG貨物添加到OOG列表中,我都添加了一個布爾變量(bContainsOOG),該變量設置爲True,然後報告子將對此進行檢查。但是,感謝你們兩位提出的這些建議,他們會派上用場,而我又陷入困境。 :-)

0

這是我認爲非常緊密配合的功能: -

' Function to determine the last (first blank/null/empty) cell in a series of data in either a column 
' or row 
' Usage :- DataSeriesEnd(w,r,b) 
'   where w is the name of the worksheet. 
'     r is the row or column as an integer 
'     b is a boolean (true/false) if true then processing a column, if false a row 
Public Function DataSeriesEnd(Worksheet As String, rc_index As Integer, bycolumn As Boolean) As Integer 
    Dim cv As Variant 

    For DataSeriesEnd = 1 To 500 
     If bycolumn Then 
      cv = Worksheets(Worksheet).Cells(DataSeriesEnd, rc_index) 
     Else 
      cv = Worksheets(Worksheet).Cells(rc_index, DataSeriesEnd) 
     End If 
     If IsNull(cv) Or IsEmpty(cv) Or cv = "" Then Exit For 
    Next DataSeriesEnd 

    ' Position to previous cell (i.e. this one is empty) 
    DataSeriesEnd = DataSeriesEnd - 1 

End Function 

注意!有500行/列的限制