ProgramingTip

VBA 언어로 "값이 비어 있지 않은 경우"를 어떻게 표현합니까?

bestdevel 2020. 12. 15. 19:37
반응형

VBA 언어로 "값이 비어 있지 않은 경우"를 어떻게 표현합니까?


VBA 언어에서 "값이 비어 있지 않은 경우"조건을 어떻게 표현할 수 있습니까? 이런 건가요?

"if value is not empty then..."
Edit/Delete Message

사용 Not IsEmpty().

예를 들면 :

Sub DoStuffIfNotEmpty()
    If Not IsEmpty(ActiveCell.Value) Then
        MsgBox "I'm not empty!"
    End If
End Sub

테스트하려는 항목에 따라.

  • 의 경우 문자열 If strName = vbNullString또는 IF strName = ""또는 Len(strName) = 0(마지막 항목이 더 빠름)을 사용할 수 있습니다.
  • 개체의 경우 다음을 사용할 수 있습니다. If myObject is Nothing
  • 레코드 세트 필드의 경우 다음을 사용할 수 있습니다. If isnull(rs!myField)
  • Excel 셀의 경우 If range("B3") = ""또는IsEmpty(myRange)

에서 여기 자세한 설명을 볼 수 있습니다 (액세스의 경우이지만 대부분 엑셀에서도 작동 함).


이 시도 :

If Len(vValue & vbNullString) > 0 Then
  ' we have a non-Null and non-empty String value
  doSomething()
Else
  ' We have a Null or empty string value
  doSomethingElse()
End If


내장 된 Format () 함수를 사용하지 않는 이유는 무엇입니까?

Dim vTest As Variant
vTest = Empty ' or vTest = null or vTest = ""

If Format(vTest) = vbNullString Then
    doSomethingWhenEmpty() 
Else
   doSomethingElse() 
End If

Format ()은 빈 변형과 null 변형을 잡아서 이동합니다. null / 빈 유효성 검사와 같은 항목에 사용하고 콤보 상자에서 항목이 선택되었는지 확인합니다.


이것이 당신이 찾고있는 것인지 잘 모르겠습니다

if var<>"" then
           dosomething

또는

if isempty(thisworkbook.sheets("sheet1").range("a1").value)= false then

ISEMPTY 기능도 사용할 수 있습니다.


Alexphi의 제안은 좋습니다. Variant다음에 할당 된 변수를 먼저 할당하여이를 하드 코딩 할 수도 있습니다 Empty. 그런 다음 다음을 사용하여 채울 수 있습니다. 채워지면 비어 있지 않습니다. 그런 다음 IsEmpty.

Sub TestforEmpty()

    Dim dt As Variant
    dt = Empty

    Dim today As Date
    today = Date
    If today = Date Then
        dt = today
    End If

    If IsEmpty(dt) Then
        MsgBox "It not is today"
    Else
        MsgBox "It is today"
    End If

End Sub

참조 URL : https://stackoverflow.com/questions/1983649/how-do-i-express-if-value-is-not-empty-in-the-vba-language

반응형