我有以下VBA代码来检查名称列表中的Excel工作簿的名称,如果它不匹配任何一个,那么它应该显示一条消息。文件名为" C12 - PICU Assignment Calculator.xlsm"并匹配下面列表中的名称,但它仍然会生成一条消息,说明它不匹配。
If ThisWorkbook.Name <> "Day Shift - Respiratory Care Cover Sheet.xlsm" _
Or ThisWorkbook.Name <> "Night Shift - Respiratory Care Cover Sheet.xlsm" _
Or ThisWorkbook.Name <> "C7 - SD-NICU Assignment Calculator.xlsm" _
Or ThisWorkbook.Name <> "C11 - TICU Assignment Calculator.xlsm" _
Or ThisWorkbook.Name <> "C12 - PICU Assignment Calculator.xlsm" _
Or ThisWorkbook.Name <> "D3 - CVICU Assignment Calculator.xlsm" _
Or ThisWorkbook.Name <> "D7 - NICU Assignment Calculator.xlsm" _
Or ThisWorkbook.Name <> "D9 - PULM Assignment Calculator.xlsm" _
Or ThisWorkbook.Name <> "General Floors Assignment Calculator.xlsm" _
Or ThisWorkbook.Name <> "Plano Assignment Calculator.xlsm" _
Then
MsgBox "The name does not match."
End If
我错过了什么?我应该使用不同的方法来实现这一目标吗?
我通过使用此声明检查了文件名是否正确
If ThisWorkbook.Name = "C12 - PICU Assignment Calculator.xlsm" Then
MsgBox "The name matches"
End If
并显示&#34;名称匹配&#34;消息它应该。
答案 0 :(得分:3)
复合谓词是true
。这就是显示消息"The name does not match."
的原因。如果您要检查列表中的任何名称是否匹配,则应使用And
代替Or
。
答案 1 :(得分:2)
看起来OP中的思想是If
语句评估为&#34;如果A不匹配B或C或D或......那么A与任何东西都不匹配&#34;。这种使用&#34;或&#34;用简单的英语说出来;但是,If
语句需要对单词&#34;或&#34;具有非常特定的含义,即&#34;或&#34;用于创建&#34; OR Gate&#34;的上下文中。这意味着它结合逻辑测试,以便,如果任何单个测试为真,那么整个测试都是真的。因此,If
语句实际上正在评估为&#34;如果A&lt;&gt; B然后继续(即A与任何东西都不匹配)或者如果A&lt;&gt; C然后继续(即A与任何东西都不匹配)......等等。
将Or
更改为And
可以将If
语句转换为&#34;和Gate&#34;来解决问题,这要求每个单一的联合逻辑测试都在命令结果为真。这将逻辑改变为&#34;(如果A&lt;&gt; B)AND(如果A&lt;&gt; C)......那么A与任何东西都不匹配&#34;。或者,您可以将<>
比较更改为=
,以便&#34;如果A = B则继续(即A匹配某些内容)或者如果A = C则继续(即A匹配某些内容) ......等等#34;
最终,我建议使用类似于数组,列表,字典或集合的内容来进行任何包含测试。这倾向于简化if
语句并将其重点细化到其主要目的,即测试包含,而列表可以独立管理。这样,列表可以增长,缩小和修改,而不会影响测试包含的逻辑。这是ArrayList
Sub NameTest()
Set NameList = CreateObject("System.Collections.ArrayList")
NameList.Add "Day Shift - Respiratory Care Cover Sheet.xlsm"
NameList.Add "Night Shift - Respiratory Care Cover Sheet.xlsm"
NameList.Add "C7 - SD-NICU Assignment Calculator.xlsm"
NameList.Add "C11 - TICU Assignment Calculator.xlsm"
NameList.Add "C12 - PICU Assignment Calculator.xlsm"
NameList.Add "D3 - CVICU Assignment Calculator.xlsm"
NameList.Add "D7 - NICU Assignment Calculator.xlsm"
NameList.Add "D9 - PULM Assignment Calculator.xlsm"
NameList.Add "General Floors Assignment Calculator.xlsm"
NameList.Add "Plano Assignment Calculator.xlsm"
If NameList.Contains(ThisWorkbook.Name) Then
MsgBox "The name matches"
Else
MsgBox "The name does not match."
End If
End Sub
答案 2 :(得分:1)
试试这段代码:
Public Sub Match()
Dim wb As Workbook
Dim wbnames(1 To 10) As String
Dim i As Integer
Set wb = ActiveWorkbook
On Error Resume Next
MsgBox wb.Name
wbnames(1) = "Day Shift - Respiratory Care Cover Sheet.xlsm"
wbnames(2) = "Night Shift - Respiratory Care Cover Sheet.xlsm"
wbnames(3) = "C7 - SD-NICU Assignment Calculator.xlsm"
wbnames(4) = "C11 - TICU Assignment Calculator.xlsm"
wbnames(5) = "C12 - PICU Assignment Calculator.xlsm"
wbnames(6) = "D3 - CVICU Assignment Calculator.xlsm"
wbnames(7) = "D7 - NICU Assignment Calculator.xlsm"
wbnames(8) = "D9 - PULM Assignment Calculator.xlsm"
wbnames(9) = "General Floors Assignment Calculator.xlsm"
wbnames(10) = "Plano Assignment Calculator.xlsm"
For i = 1 To 10
If wb.Name = wbnames(i) Then
MsgBox "this workbook name matches" & wbnames(i)
End If
Next
答案 3 :(得分:1)
这不是一个答案,因为它已经由@Amnon
提供,但是另外的变体(仅用于扩展理解)如何以更简单和更易读的方式实现相同的结果:
Sub test()
Select Case ThisWorkbook.Name
Case "Day Shift - Respiratory Care Cover Sheet.xlsm", _
"Night Shift - Respiratory Care Cover Sheet.xlsm", _
"C7 - SD-NICU Assignment Calculator.xlsm", _
"C11 - TICU Assignment Calculator.xlsm", _
"C12 - PICU Assignment Calculator.xlsm", _
"D3 - CVICU Assignment Calculator.xlsm", _
"D7 - NICU Assignment Calculator.xlsm", _
"D9 - PULM Assignment Calculator.xlsm", _
"General Floors Assignment Calculator.xlsm", _
"Plano Assignment Calculator.xlsm"
Case Else
MsgBox "The name does not match."
End Select
End Sub