Input Box Cancel function

M

Munchkin

My input box works great when a user is filling in the info. But I also want
to give them the option to press cancel if they made a mistake & don't want
to proceed. If they press cancel I want Row 4 to be deleted & the macro to
end. How to I do that?


Range("B4") = InputBox("ENTER YOUR NAME - Press 'Enter' when done")
Range("C4") = InputBox("ENTER THE DATE YOU NEED THE FILE BY - Press
'Enter' when done")

Range("B4").Select
 
J

Jacob Skaria

Dim varTemp As Variant
Dim blnExit As Boolean

varTemp = InputBox("ENTER YOUR NAME - Press 'Enter' when done")
Range("B4") = varTemp
varTemp = InputBox("ENTER THE DATE YOU NEED THE FILE BY - Press 'Enter' when
done")
Range("C4") = varTemp
If Range("B4") = "" And Range("C4") = "" Then Rows(4).Delete: Exit Sub

If this post helps click Yes
 
R

Rick Rothstein

Something like this maybe...

Dim Answer As String
Answer = InputBox("ENTER YOUR NAME")
If Len(Answer) = 0 Then
Rows(4).Delete
Exit Sub
End If
Range("B4").Value = Answer
Answer = InputBox("ENTER THE DATE YOU NEED THE FILE BY")
If Len(Answer) = 0 Then
Rows(4).Delete
Exit Sub
End If
Range("C4") = Answer
Range("B4").Select
'
' Rest of your code goes here
'

NOTE: Test the code out on a copy of your data as you cannot Undo changes
made my macro code (I'm thinking about the Row 4 delete when I say that)
 
J

john

Another approach which includes a test for valid date entry

Dim Question As Variant
Dim mysheet As Worksheet

Set mysheet = ThisWorkbook.Worksheets("Sheet1") '<< change as required

Question = InputBox("ENTER YOUR NAME - Press 'Enter' when done")

If Question = "" Then GoTo progend

mysheet.Range("B4") = Question

AddDate:
Question = InputBox("ENTER THE DATE YOU NEED THE FILE BY - Press 'Enter'
when done")

If Question = "" Then

GoTo progend

ElseIf IsDate(Question) = False Then

msg = MsgBox("Please Enter A Valid Date", 16, "Date Error")

GoTo AddDate

Else

mysheet.Range("C4") = Question

End If

progend:

With mysheet

If .Range("$B$4").Value = "" Or _
.Range("$C$4").Value = "" Then .Rows(4).Delete

End With
 
T

Tim Zych

Sub InputboxExample()

Dim vResult As Variant
vResult = InputBox("Enter a value")

If StrPtr(vResult) = 0 Then
' Cancel pressed
MsgBox "Cancel"
ElseIf vResult = "" Then
' Zero-length string entered
MsgBox "zls"
Else
' Some entry
MsgBox CStr(vResult)
End If

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top