itsource

및/또는 VBA의 기타 기능

mycopycode 2023. 5. 12. 22:15
반응형

및/또는 VBA의 기타 기능

저는 다음과 같은 방법으로 엑셀 매크로에 '그리고'로 게으른 평가를 받으려고 합니다.

If Not myObject Is Nothing *And* myObject.test() Then
    'do something'
Else
    'do something else'
End If

저는 VB에 게으른 평가가 존재한다는 것을 알고 있습니다.과 같은 AndAlso그리고.OrElse하지만 VBA에서 비슷한 것을 찾을 수 없습니다.VBA에 게으른 평가가 존재하지 않는다면, 코드가 내가 기대하는 방식을 평가할 수 있도록 코드를 구성하는 가장 좋은 방법은 무엇입니까?

(종류의) 유일한 단락은 다음과 같습니다.Case표현 평가, 그래서 다음의 보기 흉한 진술은 당신이 요구하는 것을 수행합니다.

Select Case True
    Case (myObject Is Nothing), Not myObject.test()
        MsgBox "no instance or test == false"
    Case Else
        MsgBox "got instance & test == true"
    End Select
End Sub

이것은 오래된 질문이지만, 이 문제는 여전히 살아있고 잘 있습니다.한 가지 해결 방법:

Dim success As Boolean       ' False by default.

If myObj Is Nothing Then     ' Object is nothing, success = False already, do nothing.
ElseIf Not myObj.test() Then ' Test failed, success = False already, do nothing.
Else: success = True         ' Object is not nothing and test passed.
End If

If success Then
    ' Do stuff...
Else
    ' Do other stuff...
End If

이것은 기본적으로 원래 질문의 논리를 뒤집지만, 같은 결과를 얻을 수 있습니다.여기 있는 다른 사람들보다 더 깨끗한 해결책이라고 생각합니다.If진들술한. 를솔션을 Select진술은 영리하지만, 만약 당신이 오직 사용하는 대안을 원한다면.If진술서, 저는 이것이 사용할 것이라고 생각합니다.

또는 개체를 매개 변수로 사용하고 두 경우 모두 부울을 반환하는 함수를 만들 수 있습니다.그게 제가 평소에 하는 일입니다.

예.

if Proceed(objMyAwesomeObject) then
       'do some really neat stuff here
else
       'do something else, eh
end if
...
end sub

private function Proceed(objMyAwesomeObject as Object)
     if not objMyAweseomeObject is nothing then
            Proceed = true
     elseif objMyAwesomeObject.SomeProperty = SomeValue then
            Proceed = true
     else
            Proceed = false
     endif
end function

동일한 기본적인 문제에 대한 다른 질문에 대한 이 답변을 개선하면서, 제가 선택한 것은 다음과 같습니다.

dim conditionsValid as boolean

conditionsValid = myObject Is Nothing
if conditionsValid then conditionsValid = myObject.test()
if conditionsValid then conditionsValid = myObject.anotherTest() 

if conditionsValid then
   'do something'
else
   'do something else'
end if

이 코드는 제안된 다른 답변보다 명확하며, 각 검증에 대해 (일반적으로) 다른 변수가 필요하지 않습니다. 즉, 다른 질문에 대한 원래 답변보다 개선된 것입니다.참고로, 필요한 새로운 조건마다 코드가 한 줄만 더 추가됩니다.

If Not myObject Is Nothing Then
    If myObject.test() Then
        'do something'
    End If
Else
   'do something else'
End If

저는 그렇게 해야 한다고 생각합니다.

편집

이런 식으로.

Dim bTestsFailed as Boolean
bTestsFailed = False

If Not myObject Is Nothing Then
    If myObject.test() Then
        'do something'
    Else
        bTestsFailed = True
    End If
Else
   bTestsFailed = True
End If

If bTestsFailed Then
    'do something else
End If

VBA 좋죠?

결측값에 대한 트릭은 다음과 같은 도움이 될 수 있습니다.

Dim passed, wrongMaxPercent, wrongPercent, rightMinPercent, rightPercent
wrongPercent = 33
rightPercent = 55

'rightMinPercent = 56
wrongMaxPercent = 40

passed = (Len(wrongMaxPercent) = 0 Or wrongPercent < wrongMaxPercent) And _
         (Len(rightMinPercent) = 0 Or rightPercent >= rightMinPercent)

다음 구문이 작동하므로

If myObject.test() Then do something

그런 다음 하나의 라이너 구문을 사용하여 평가를 단락시킬 수 있습니다.첫 아래, 첫번째.If진술은 다음을 보장합니다.myObject뭔가.그렇지 않으면 두 번째를 평가하려 하지도 않을 것입니다.If.

If Not myObject Is Nothing Then If myObject.test() Then
    'do something'
Else
    'do something else'
End If

물론, 내 개체가 아무 것도 아니라면 '다른 것을 하라'고 한다면, 이것은 작동하지 않을 수도 있습니다.


2020/06/30 업데이트

이 답변이 작동하지 않았다는 지적을 받은 후, 저는 구문이 현대 VBA에서 작동하지 않는 것으로 보인다는 것을 확인했습니다.기존의 목적을 위해 원래의 답을 남깁니다.

논리적 조건을 전환하여 Or 연산자와 작업하고 다음과 같은 오류 메시지를 끌 수 있습니다.

Err.Clear
On Error Resume Next
If myObject Is Nothing Or Not myObject.test() Then
    Err.Clear
    'do something else'
Else
    'do something'
End If
On Error Goto 0 ' or On Error Goto ErrorHandler (depending on previous setting in the code)

아무것도 아닌 것에 대한 테스트는 필요하지 않습니다. 단지 의미를 명확히 하는 역할을 할 뿐입니다.

에 필적하는 것은 아무것도 모릅니다.OrElse하지만 제한적이지만 유용한 해결책이 있습니다.AndAlso다음 두 가지는 동일합니다.

  • vb.net :If Condition1 AndAlso Condition2 Then DoSomething
  • vba:If Condition1 Then If Condition2 Then DoSomething

두 가지 제한 사항이 있습니다.

  1. 하나의 라이너로만 작동하므로 if-block을 시작하는 데 사용할 수 없습니다.
  2. Else첫 번째 조건이 거짓일 때가 아니라 첫 번째 조건이 참이고 두 번째 조건이 거짓일 때 블록이 실행됩니다.

이 두 가지의 꽤나 치명적인 한계를 고려하더라도, 저는 종종 이 작은 속임수를 사용합니다. 제 작은 속임수를 사용합니다.Else블록으로 막다

다음은 예입니다.

Sub Test()
  Dim C As Collection

  ' This is what I often use
  If Not C Is Nothing Then If C.Count Then DoSomethingWith C

  ' Here are other usages I stay away from, because of bad readability
  If Not C Is Nothing Then If C.Count Then Debug.Print "not empty" Else Debug.Print "empty or nothing"
  Set C = New Collection
  If Not C Is Nothing Then If C.Count Then Debug.Print "not empty" Else Debug.Print "empty or nothing"
  C.Add 1
  If Not C Is Nothing Then If C.Count Then Debug.Print "not empty" Else Debug.Print "empty or nothing"
End Sub```

우리는 다음과 같이 말하고 싶습니다.XX < 7이면 XX = 19이지만 XX가 Null일 수 있으므로 테스트해야 합니다.

이렇게 할 수 있습니다. 스위치(IsNull(XX)), True, True, XX < 7)이면 XX = 19입니다. 따라서 XX가 Null이면 테스트 XX < 7이 True인 경우에만 할당을 수행합니다.

언급URL : https://stackoverflow.com/questions/3242560/andalso-orelse-in-vba

반응형