excel vba如果声明与OR和AND场景

我有一个电子邮件系统,根据当前logging的国籍select不同的语言模板。 我需要使语言selectdynamic化,因为最终用户并不总是希望以条目的国家语言发送消息,这意味着例如如果logging说明“IT”(意大利)国籍,我想在继续之前检查语言模板是否设置为“激活”,如果模板是“非激活”,则分别默认为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语句来检查模板是否处于活动状态?

 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 

Select Case语句可以帮助整理多个匹配项。

 if lcase(Range("B5").Value) = "active" then select case ucase(nationality) case "DT", "AT", "CH" With ActiveWorkbook.Sheets("PostStayEmail_DE") Set rng = .Range("A1:B30").SpecialCells(xlCellTypeVisible) 'do something End With case "IT" With ActiveWorkbook.Sheets("PostStayEmail_IT") Set rng = .Range("A1:B30").SpecialCells(xlCellTypeVisible) 'do something End With case else 'do thing if no matches end select else 'do stuff when B5 is not 'Active' end if 

根据ISO 3166-1 alpha-2两字母国家代码组合的代码的重复情况,您可能只希望logging工作表名称,并将所有进一步处理留在select案例下面的一个块中。

 If (nationality = "DE" OR nationality = "AT" OR nationality = "CH") _ AND Range("B5") = "Active" then 'do something End If 

这应该工作