![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Join Date: Nov 2002
Location: spain
Posts: 500
|
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
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. |
|
|
|
|
|
#2 |
|
MrExcel MVP
Join Date: Aug 2002
Location: Wellington, New Zealand
Posts: 3,311
|
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 |
|
|
|
|
|
#3 |
|
Join Date: Jan 2004
Location: Melbourne
Posts: 3,456
|
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. |
|
|
|
|
|
#4 | |
|
MrExcel MVP
Join Date: Aug 2002
Location: Wellington, New Zealand
Posts: 3,311
|
Quote:
If you have several input boxes like this you may consider creating a userform instead. |
|
|
|
|
|
|
#5 |
|
Join Date: Nov 2002
Location: spain
Posts: 500
|
thanks guys. when I tried this first time round I had been using
Code:
If typetext = "" then Exit Sub 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
__________________
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. |
|
|
|
|
|
#6 |
|
MrExcel MVP
Join Date: Apr 2002
Location: Vancouver BC , Canada
Posts: 6,259
|
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> |
|
|
|
|
|
#7 |
|
MrExcel MVP
Join Date: Aug 2002
Location: Wellington, New Zealand
Posts: 3,311
|
Hi, should you also be testing whether the user pressed cancel on the very first input box (ie how many rows?). Just a thought.
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|