2012-07-24 67 views
1
Do Until Selection.Value = "" Or _ 
    (Selection.Value = theyear And Selection.Offset(0, 1).Value = themonth) 

Selection.Offset(1, 0).Select 

Loop 

在這一行語句中,代碼無法檢查條件與或部分ie;它不檢查括號中的條件。是否預計?複雜條件一直做到

+0

當我用「A」和「B」替換年和月時,給定的代碼按預期工作。如果兩個語句中的第一個爲真,它將退出循環。仔細檢查包含在heear和month(本地窗口或debug.print)中的值。 – Trace 2012-07-24 11:46:19

+0

這個月和月是通過輸入框作爲:themonth = InputBox(「輸入月份的前三個字母來追加」,「月份縮寫」) – 2012-07-24 12:17:13

+0

這應該沒有什麼區別Dinesh。你檢查了條件嗎? Debug.print Selection.Value = theyear返回true/false?某處不應該匹配。如果你可以上傳文件,我願意看看。 – Trace 2012-07-24 12:29:24

回答

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 

你的主要錯誤是把雙引號之間的變量名。 但是,我想指出,由於您允許用戶輸入任何數據而無需檢查,因此此代碼可能對錯誤非常明智。
但是,如果它是爲自己使用,這並不重要。
另請注意,選擇/偏移會使您的代碼非常嚴格。