Excel do-until的复杂条件

Excel do-until的复杂条件,excel,vba,Excel,Vba,在这一行语句中,代码无法使用或部分ie检查条件;它不会检查支架中的状况。是预期的吗?试试这个: Do Until Selection.Value = "" Or _ (Selection.Value = theyear And Selection.Offset(0, 1).Value = themonth) Selection.Offset(1, 0).Select Loop 你的主要错误是在引号之间加了一个变量名。 但是,我想指出的是,由于您允许用户输入任何数据而无需检查,因此

在这一行语句中,代码无法使用或部分ie检查条件;它不会检查支架中的状况。是预期的吗?

试试这个:

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

Selection.Offset(1, 0).Select

Loop
你的主要错误是在引号之间加了一个变量名。 但是,我想指出的是,由于您允许用户输入任何数据而无需检查,因此此代码可能对bug非常敏感。
但如果是自己用的,这并不重要。

还要注意的是.Select/Offset使您的代码总体上相当严格

当我用“A”和“B”替换年份和月份时,给定的代码对我来说就像预期的那样工作。如果两条语句中的情况1为真,它将退出循环。仔细检查Year和themonth(本地窗口或debug.print)中包含的值。Year和themonth通过一个输入框获取,如:themonth=inputbox(“输入要附加的月份的前三个字母”,“月份首字母”),这不会造成任何区别。你检查过情况了吗?Debug.print Selection.Value=年份返回真/假?有些地方不应该匹配。如果你能上传文件,我愿意看一看。themonth=InputBox(“输入要附加的月份的前三个字母”,“月份首字母”)theyear=InputBox(“输入月份对应的年份”,“年”)直到Selection.Value=”“或(Selection.Value=“theyear”和Selection.Offset(0,1)。Value=“themonth”)选择。偏移量(1,0)。选择循环这是代码
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