MrExcel Message Board


Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Mar 9th, 2004, 02:24 AM   #1
whiteghost
 
whiteghost's Avatar
 
Join Date: Nov 2002
Location: spain
Posts: 500
Default modify inputboxs so cancel = exit sub

Hi
how can I modify this code or the inputboxes sothat if "cancel" is clicked on any of the inputboxes will exit the sub.
Code:
Sub star1()
'
Dim loopcount As Integer
Dim i As Integer
loopcount = InputBox("how many rows?")
For i = 1 To loopcount

Range("a65536").Select

Dim x As Double
    
    x = Range("a525").End(xlUp).Row + 1
    Range("a" & x).Select
 

typetext = InputBox(prompt:="Enter Code.", Default:="PCode")
codetext = InputBox(prompt:="Enter coutry Code.", Default:="Country Code")
quantext = InputBox(prompt:="Enter quantity.", Default:="Qty.")
pricetext = InputBox(prompt:="Enter price.", Default:="")
ActiveCell.FormulaR1C1 = typetext
Range("a65536").End(xlUp).Offset(1, 0) = vbNullString
Range("a65536").End(xlUp).Offset(0, 0) = typetext
Range("a65536").End(xlUp).Offset(0, 2) = codetext
Range("a65536").End(xlUp).Offset(0, 4) = quantext
Range("a65536").End(xlUp).Offset(0, 5) = pricetext
    
Next i

x = Range("a525").End(xlUp).Row + 1
    Range("a" & x).Select


End Sub
the macro position the input , the loopcount input box allowser to define how many rows they want to add. What I want to be able to do is exit the sub if cancel is clicked on typetext, codetext, quantext or pricetext.

thanks
__________________
Grant me the serenity to accept the things I cannot change, the courage to change the
things I cannot accept and the wisdom to hide the bodies of all the people
who seriously pissed me off.
whiteghost is offline   Reply With Quote
Old Mar 9th, 2004, 02:37 AM   #2
parry
MrExcel MVP
 
parry's Avatar
 
Join Date: Aug 2002
Location: Wellington, New Zealand
Posts: 3,311
Default Re: modify inputboxs so cancel = exit sub

Hi, if cancel is pressed then it will return a null string to your variable. So do an if statement testing for this.

eg
loopcount = InputBox("how many rows?")
if loopcount = vbNullString then exit sub

you could also use
if loopcount = "" then exit sub
parry is offline   Reply With Quote
Old Mar 9th, 2004, 02:43 AM   #3
tactps
 
Join Date: Jan 2004
Location: Melbourne
Posts: 3,456
Default Re: modify inputboxs so cancel = exit sub

You can use:
If loopcount = "" Or typetext = "" Or codetext = "" Or quantext = "" Or pricetext = "" Then Exit Sub
__________________
There are three kinds of people - those that can count and those that can't.
tactps is offline   Reply With Quote
Old Mar 9th, 2004, 02:59 AM   #4
parry
MrExcel MVP
 
parry's Avatar
 
Join Date: Aug 2002
Location: Wellington, New Zealand
Posts: 3,311
Default Re: modify inputboxs so cancel = exit sub

Quote:
Originally Posted by tactps
You can use:
If loopcount = "" Or typetext = "" Or codetext = "" Or quantext = "" Or pricetext = "" Then Exit Sub
Hi Tactps, yes you could do that but its not suited to this situation. The if test is only made once so you would have to wait until all the input boxes have been presented to the user to know whether they pressed cancel on any one of them. That means if they didnt enter anything in the first one then the others would still have to appear before you would know to exit the procedure. Its best to test each individual box and stop it at the point of the first cancel.

If you have several input boxes like this you may consider creating a userform instead.
parry is offline   Reply With Quote
Old Mar 9th, 2004, 03:17 AM   #5
whiteghost
 
whiteghost's Avatar
 
Join Date: Nov 2002
Location: spain
Posts: 500
Default Re: modify inputboxs so cancel = exit sub

thanks guys. when I tried this first time round I had been using
Code:
If typetext = "" then
Exit Sub
which threw up a "Next without for" or "block if without end if" dependingon where I put it. Never thought to have Eit sub on the same line :o

the best solution (using you guy's suggestion) works out like this:
Code:
Sub star1()
'
Dim loopcount As Integer
Dim i As Integer
loopcount = InputBox("how many rows?")
For i = 1 To loopcount

Range("a65536").Select

Dim x As Double
    
    x = Range("a525").End(xlUp).Row + 1
    Range("a" & x).Select
 

typetext = InputBox(prompt:="Enter Code.", Default:="PCode")
if typetet = "" then  Exit sub
codetext = InputBox(prompt:="Enter coutry Code.", Default:="Country Code")
if codetext ="" then Exit Sub
quantext = InputBox(prompt:="Enter quantity.", Default:="Qty.")
if quantext ="" then Exit Sub
pricetext = InputBox(prompt:="Enter price.", Default:="")
if pricetext ="" then Exit Sub
ActiveCell.FormulaR1C1 = typetext
Range("a65536").End(xlUp).Offset(1, 0) = vbNullString
Range("a65536").End(xlUp).Offset(0, 0) = typetext
Range("a65536").End(xlUp).Offset(0, 2) = codetext
Range("a65536").End(xlUp).Offset(0, 4) = quantext
Range("a65536").End(xlUp).Offset(0, 5) = pricetext




    
    
Next i

x = Range("a525").End(xlUp).Row + 1
    Range("a" & x).Select

'

'
End Sub
this way it exits immediately and does not show all the inputboxes before exiting. Thanks again
__________________
Grant me the serenity to accept the things I cannot change, the courage to change the
things I cannot accept and the wisdom to hide the bodies of all the people
who seriously pissed me off.
whiteghost is offline   Reply With Quote
Old Mar 9th, 2004, 03:25 AM   #6
Nimrod
MrExcel MVP
 
Join Date: Apr 2002
Location: Vancouver BC , Canada
Posts: 6,259
Default Re: modify inputboxs so cancel = exit sub

Public Sub InputDemo()
typetext = Application.InputBox(prompt:="Enter Code.", Default:="Code", Type:=2)
If typetext = False Then Exit Sub

codetext = Application.InputBox(prompt:="Enter coutry Code.", Default:="Country Code", Type:=2)
If codetext = False Then Exit Sub

quantext = Application.InputBox(prompt:="Enter quantity.", Default:="Qty.", Type:=1)
If quantext = False Then Exit Sub

pricetext = Application.InputBox(prompt:="Enter price.", Default:="", Type:=1)
If pricetext = False Then Exit Sub

End Sub

The InputBox method differs from the InputBox function in that it allows selective validation of the user's input, and it can be used with Microsoft Excel objects, error values, and formulas. Note that Application.InputBox calls the InputBox method; InputBox with no object qualifier calls the InputBox function.

This method will only allow numbers to be entered for price and quantity
__________________

<MARQUEE>...........Never be afraid to try something new. Remember, amateurs built the ark, professionals built the Titanic...............The easiest thing to find is fault, don't be easy !.. --Anonymous--...</marquee>
Nimrod is offline   Reply With Quote
Old Mar 9th, 2004, 03:34 AM   #7
parry
MrExcel MVP
 
parry's Avatar
 
Join Date: Aug 2002
Location: Wellington, New Zealand
Posts: 3,311
Default Re: modify inputboxs so cancel = exit sub

Hi, should you also be testing whether the user pressed cancel on the very first input box (ie how many rows?). Just a thought.
parry is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT +1. The time now is 02:45 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
All contents Copyright 1998-2009 by MrExcel Consulting.