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

Selection.Offset(1, 0).Select

Loop

in this line of statement, the code is unable to check the condition with the or part ie; it does not check the condition in the bracket. Is it expected?

4

1 回答 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

您的主要错误是将变量名放在引号之间。但是,我想指出,由于您允许用户在不检查的情况下输入任何数据,因此该代码可能对错误非常敏感。
但如果是自用的话,这并不重要。
另请注意, .Select/Offset 使您的代码总体上非常僵硬。

于 2012-07-24T14:06:13.757 回答