带有OR和AND场景的excel vba if语句

时间:2016-04-01 04:48:14

标签: excel vba excel-vba

我有一个电子邮件消息系统,根据当前记录的国籍选择不同的语言模板。我需要使语言选择动态,因为最终用户并不总是希望以条目的国家语言发送消息,这意味着,例如,如果记录表明国家是" IT&# 34; (意大利),我想检查语言模板是否设置为" Active"在继续之前,如果模板是"非活动"则分别默认为EN(英语)。

'check for nationality and select letter template accordingly
    ' Use german for DE, AT and CH nationalities
    If nationality = "DE" Or nationality = "AT" Or nationality = "CH" Then
        Set rng = Sheets("PostStayEmail_DE").Range("A1:B30").SpecialCells(xlCellTypeVisible)
        With ActiveWorkbook.Sheets("PostStayEmail_DE")
            'do something
        End With`

    'Use italian for IT nationalities
        ElseIf nationality = "IT" Then
        Set rng = Sheets("PostStayEmail_IT").Range("A1:B30").SpecialCells(xlCellTypeVisible)
        With ActiveWorkbook.Sheets("PostStayEmail_IT")
              'do something

        End With

现在,if语句需要的是这样的

if (nationality = "DE" or nationality = "AT" or nationality = "CH") _
                          AND (Range("B5") = "Active") then
    'do something

Elseif ... 'check other languages

如何组合多种语言的OR语句,还可以添加AND语句来检查模板是否处于活动状态?

3 个答案:

答案 0 :(得分:1)

If ((nationality = "DE" Or nationality = "AT" Or nationality = "CH") And Range("B5").Value = "Active") Then
    Set Rng = Sheets("PostStayEmail_DE").Range("A1:B30").SpecialCells(xlCellTypeVisible)
    With ActiveWorkbook.Sheets("PostStayEmail_DE")
        'do something
    End With
ElseIf ((nationality = "IT") And Range("B5").Value = "Active") Then
    Set Rng =   Sheets("PostStayEmail_IT").Range("A1:B30").SpecialCells(xlCellTypeVisible)
    With ActiveWorkbook.Sheets("PostStayEmail_IT")
        'do something
    End With
End If

答案 1 :(得分:1)

Select Case statement可能有助于整理多场比赛。

min height or "wrap_content".

根据ISO 3166-1 alpha-2双字母国家/地区代码的各种组合的代码重复,您可能只希望记录工作表名称并将所有进一步处理保留在Select Case下面的单个块中。

答案 2 :(得分:0)

alert

这应该有效