Is variable required instead of “or”

夙愿已清 提交于 2019-12-26 03:10:12

问题


Simple Hide Row not functioning when given multiple options

I've tried setting a Variable for the range and got same mismatch

If Range("J178") = "Credit" Or "Debit" Or "Both" Then
    Rows("180").Hidden = False
Else
    Rows("180").Hidden = True
End If

I expected the code to run or not; not the Run time 13 type mismatch I'm receiving.


回答1:


Option 1 - Write out each condition longhand (case sensitive)

If Range("J178") = "Credit" Or Range("J178") = "Debit" Or Range("J178") = "Both" Then
    Rows("180").Hidden = False
Else
    Rows("180").Hidden = True
End If

Option 2 - Check an array with the worksheet's Match (not case sensitive)

If IsNumeric(application.Match(Range("J178"), Array("Credit","Debit", "Both"), 0)) Then
    Rows("180").Hidden = False
Else
    Rows("180").Hidden = True
End If

Option 3 - Use a Select Case method (case sensitive)

Select Case Range("J178"),Value
    Case "Credit", "Debit", "Both"
        Rows("180").Hidden = False
    Case Else
        Rows("180").Hidden = True
End Select

Option 4 - Use a modified Select Case method (not case sensitive)

Select Case lcase(Range("J178"),Value)
    Case "credit", "debit", "both"
        Rows("180").Hidden = False
    Case Else
        Rows("180").Hidden = True
End Select



回答2:


... Option 5 (following from @user10970498)

Dispense with the If-Then-Else-EndIf construct:

Rows("180").Hidden = (Range("J178") = "Credit" Or Range("J178") = "Debit" Or Range("J178") = "Both")

or

Rows("180").Hidden = IsNumeric(application.Match(Range("J178"), Array("Credit","Debit", "Both"), 0))

This works because the Hidden property is a Boolean.




回答3:


A bit shorter alternative using Evaluate :

Rows("180").Hidden = Not [or(J178 = "Credit", J178 = "Debit", J178 = "Both")]


来源:https://stackoverflow.com/questions/54383275/is-variable-required-instead-of-or

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!