-2

我在 B2 到文档末尾的日期范围。我想让用户通过输入框提示“您在寻找什么日期”,用户将输入日期。然后用户数据将用于搜索 B2 行到末尾并找到该特定日期。如果该日期不存在,则会弹出另一个框,让用户知道他们需要扩展该范围并重试。即使当我输入该范围内的正确数据时,我仍然会收到“添加天数”,但它不会输出“Your Good”。谁能帮我吗?

 Sub Macro2()
     datein = CDate(InputBox("Date Project Will Start"))

     For Each c In Worksheets("sheet1").Range("A1:D100").Cells
           If datein = c.Value Then MsgBox "Your Good"

     Next
     MsgBox "Add More Days"
 End Sub
4

2 回答 2

1

您只需要将 datein 转换为日期。

datein = CDate(InputBox("Date Project Will Start"))
于 2013-07-30T02:53:04.230 回答
1

grantnz 提供的答案确实解决了值比较的问题,但是您需要打破循环并执行某种验证以避免第二条消息......像这样:

Sub Macro2()
   Dim datein as Date, found as Boolean ' I prefer explicit variable declaration

   datein = CDate(InputBox("Date Project Will Start"))
   found = False

   For Each c In Worksheets("sheet1").Range("A1:D100").Cells
       If datein = c.Value Then 
           MsgBox "Your Good"
           found = True
           Break ' You don't need to continue the iteration if you find a date
       End If
    Next
    If Not found Then 
        MsgBox "Add More Days" ' This message pops up only if found is false
    End If
End Sub
于 2013-07-30T04:29:49.240 回答