我有一个宏来检查数据是否满足某些条件。如果是,则为每个匹配的标准显示一个消息框。
我希望有一个唯一消息框,显示所有匹配的条件。
解决此问题的最佳方法是什么?以下示例是我到目前为止所做的。
N I Esp Par T DBH H_m Cod
2 111 E_cit 432 1 1
2 111 E_cit 432 2 1
2 111 E_cit 432 3 1
2 111 E_cit 432 4 3 17.4
2 111 E_cit 432 5
2 111 E_cit 432 6 14.48 15
宏(以两个条件为例):
Option Explicit
Dim DBH As Range
Dim Cod As Range
Sub Conditions_1()
'------------------------------------------------
'DBH should be between 5 and 45 cm
For Each DBH In Range("F2:F7")
If DBH < 5 And DBH <> 0 Or DBH > 45 And DBH <> 0 Then
MsgBox "1 - DBH is not between 5 cm and 45 cm"
Exit For
End If
Next DBH
'------------------------------------------------
'There can't be an empty cell on DBH column
'together with an empty cell on the Code column
For Each DBH In Range("F2:F7")
If DBH = 0 And DBH.Offset(, 2) = 0 Then
MsgBox "2 - DBH is empty and also the Code"
Exit For
End If
Next DBH
End Sub
此当前代码分别生成两个消息框
但是,我想要一个列出所有条件的单一框。任何帮助表示赞赏。
答案 0 :(得分:1)
Option Explicit
Dim DBH As Range
Dim Cod As Range
Sub Conditions_1()
Dim msg as string
msg=""
'------------------------------------------------
'DBH should be between 5 and 45 cm
For Each DBH In Range("F2:F7")
If DBH < 5 And DBH <> 0 Or DBH > 45 And DBH <> 0 Then
msg = msg & "1 - DBH in " & DBH.Address() & _
" is not between 5 cm and 45 cm" & vbLf
End If
Next DBH
'------------------------------------------------
'There can't be an empty cell on DBH column
'together with an empty cell on the Code column
For Each DBH In Range("F2:F7")
If DBH = 0 And DBH.Offset(, 2) = 0 Then
msg = msg & "2 - DBH in " & DBH.Address() & _
" is empty and also the Code" & vbLf
End If
Next DBH
If Len(msg) > 0 Then MsgBox msg
End Sub