我的日期范围在B2到文档末尾。我想要通过输入框“您要查找的日期”提示用户,用户将输入日期。然后,用户数据将用于搜索行B2到最后并找到该特定日期。如果该日期不存在,则会弹出另一个框,让用户知道他们需要扩展范围并再试一次。即使我在输入该范围内的正确数据时,我仍然会收到“添加天数”。任何人都可以帮助我吗?
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
答案 0 :(得分:1)
您只需将日期转换为日期。
datein = CDate(InputBox("Date Project Will Start"))
答案 1 :(得分: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