2014-06-05 98 views
0

所以我想要做的是将一堆变量传递给If ... then语句。如果变量返回true,那么它将得到代码的另一部分使用的“”。如果它返回false,则会在消息框中打印一个不同的值。如何从MsgBox中删除变量

这是问题所在。有多个变量,当每个变量都在消息框中打印时,它们之间用行分隔。如果一个变量,比如说C,返回true,那么它将只是一个空白的消息框,剩下的就是一些东西。基本上,我试图删除变量,并将其放入消息框中。我怎么能这样做呢?

Set cellD = Selection.Find(What:="7/27/2013") 
If cellD Is Nothing Then 
    D = "7/27/2013, " 
Else: 
    D = "" 
End If 

If A = "" And B = "" And C = "" And D = "" Then 
    MsgBox (Pass) 

Else: 
    If A = "" Then Set A = Nothing 
    MsgBox ("Missing:" & vbNewLine & A & vbNewLine & B & vbNewLine & C & vbNewLine & D) 
+1

这是什么'A'? - 你测试它就像是一个字符串,但然后将它设置为Nothing,就像它是一个对象...很难遵循你的代码 - 可能会添加更多的行,所以你的意图更清晰。 –

回答

1

这里有一种方法:

Sub test() 

Dim output As String 

Dim A As String 
Dim B As String 
Dim C As String 
Dim D As String 

If A <> "" Then output = A 

If B <> "" Then 
    If output = "" Then 
     output = B 
    Else 
     output = output & vbNewLine & B 
    End If 
End If 

If C <> "" Then 
    If output = "" Then 
     output = C 
    Else 
     output = output & vbNewLine & C 
    End If 
End If 

If D <> "" Then 
    If output = "" Then 
     output = D 
    Else 
     output = output & vbNewLine & D 
    End If 
End If 

If output <> "" Then 
    MsgBox ("Missing: " & output) 
Else 
    MsgBox ("Pass") 
End If 

End Sub 

还有其他的方法,但我认为这是一个很好的地方给你开始(使你的VBA技术水平的假设)。我认为A,B和C都是字符串,但是您应该根据自己的情况对这些字符进行调暗。

希望能让你接近!

+0

谢谢!这非常有帮助! – Drew

+1

@ user3485595太棒了,很高兴我能帮到你。如果它解决了您的问题,那么通常会接受和/或接受解决方案。这让有相似问题的人知道答案是有帮助的。 – sous2817