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

Selection.Offset(1, 0).Select

Loop

この文の行では、コードは or の部分で条件をチェックできません。括弧内の状態はチェックしません。それは期待されていますか?

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 に答える