InputBox cancel button/data type problem (SOLVED)

phantom1975

MrExcel MVP
Joined
Jun 3, 2002
Messages
3,962
I have the following:

Private Sub CommandButton1_Click()
Dim TransAmt as Long
TransAmt = InputBox("Enter Amount","Enter Amount","1234")
If TransAmt = "" Then
Exit Sub
End If
MsgBox "Your amount is " & TransAmt
End Sub

I get an error message when the CANCEL button is clicked. I'm certain that is because I have the variable set as LONG instead of STRING. What else can I do other than change the data type?

I've also tried:

If TransAmt = vbCancel Then
Exit Sub

And that doesn't work either. I even thought I had seen something like:

If Cancel = True Then

But that didn't do the trick either.

_________________
Pass on what you have learned. Support this great website by clicking on the sponsor's ads!.
This message was edited by phantom1975 on 2002-08-22 23:53
This message was edited by phantom1975 on 2002-08-23 00:21
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Add error-testing:

Private Sub CommandButton1_Click()
Dim TransAmt As Long
On Error Resume Next
TransAmt = InputBox("Enter Amount", "Enter Amount", "1234")
errorcode = Err.Number
On Error GoTo 0
If errorcode <> 0 Then
Exit Sub
End If
MsgBox "Your amount is " & TransAmt
End Sub
 
Upvote 0
That does the trick. Are there other methods of making the Cancel button work? For instance, I have seen:

If Cancel = True

How is that used?
 
Upvote 0
I don't know. I have never seen it, but on the ather hand, I think I have a LOT!!! more to see.... :)
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top