复杂的条件与做直到

  • 本文关键字:条件 复杂 excel vba
  • 更新时间 :
  • 英文 :

Do Until Selection.Value = "" Or _
     (Selection.Value = theyear And Selection.Offset(0, 1).Value = themonth)
Selection.Offset(1, 0).Select
Loop

在此语句行中,代码无法使用 OR 部分 IE 检查条件;它不检查括号中的条件。这是意料之中的吗?

试试这个:

Sub Test()
Dim sMonth As String
Dim iYear As Integer
sMonth = UCase(Trim(InputBox("Enter the first three alphabets of the month to append", "Month Initials")))
iYear = InputBox("Enter the year to which the month corresponds", "Year")
Do Until Selection.Value = "" Or _
    (UCase(Trim(Selection.Value)) = sMonth And Selection.Offset(0, 1).Value = iYear)
    Selection.Offset(1, 0).Select
Loop
End Sub

您的主要错误是在引号之间放置一个变量名称。但是,我想指出的是,由于您允许用户输入任何数据而无需检查,因此此代码可能对错误非常敏感。
但如果是供自己使用,这并不重要。
另请注意。选择/偏移使您的代码总体上非常僵化。

最新更新