2012-07-24 66 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 

你的主要错误是把双引号之间的变量名。 但是,我想指出,由于您允许用户输入任何数据而无需检查,因此此代码可能对错误非常明智。
但是,如果它是为自己使用,这并不重要。
另请注意,选择/偏移会使您的代码非常严格。