Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Rob Rob is offline
external usenet poster
 
Posts: 718
Default Capture InputBox vbCancel

Hello, I have the below code that prompts for the qty of rows to be inserted
but when the Cancel button is selected instead of providing a number and
clicking OK I get an error. I was hoping that putting the IF statement in
there would fix it but it did not. How can I fix this?

Thanks in Advance.


Sub AutoInsertRows()

'Shortcut Keystroke CTRL+Shift+A

Dim r As Integer

r = InputBox("How Many Rows?", "Auto-Insert Rows", 1)
i = 0

Do While i < r

If Not r = vbCancel Then

Selection.Insert Shift:=xlDown

i = i + 1

Else

Exit Sub

End If

Loop

End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default Capture InputBox vbCancel

Dim i As Long
Dim n As Long

On Error GoTo CancelInput
n = InputBox("No. of Rows")
CancelInput:

For i = 1 To n
Selection.Insert Shift:=xlDown
Next i

P.S. Throw out "Dim ... As Integer" Get in the habit of always using Long.
You won't regret it.

"Rob" wrote:

Hello, I have the below code that prompts for the qty of rows to be inserted
but when the Cancel button is selected instead of providing a number and
clicking OK I get an error. I was hoping that putting the IF statement in
there would fix it but it did not. How can I fix this?

Thanks in Advance.


Sub AutoInsertRows()

'Shortcut Keystroke CTRL+Shift+A

Dim r As Integer

r = InputBox("How Many Rows?", "Auto-Insert Rows", 1)
i = 0

Do While i < r

If Not r = vbCancel Then

Selection.Insert Shift:=xlDown

i = i + 1

Else

Exit Sub

End If

Loop

End Sub


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default Capture InputBox vbCancel

i usually use application.inputbox. cancel will return 0

lookup InputBox Method in vb help.


--


Gary


"Rob" wrote in message
...
Hello, I have the below code that prompts for the qty of rows to be inserted
but when the Cancel button is selected instead of providing a number and
clicking OK I get an error. I was hoping that putting the IF statement in
there would fix it but it did not. How can I fix this?

Thanks in Advance.


Sub AutoInsertRows()

'Shortcut Keystroke CTRL+Shift+A

Dim r As Integer

r = InputBox("How Many Rows?", "Auto-Insert Rows", 1)
i = 0

Do While i < r

If Not r = vbCancel Then

Selection.Insert Shift:=xlDown

i = i + 1

Else

Exit Sub

End If

Loop

End Sub




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 857
Default Capture InputBox vbCancel

Hi Rob,

I believe InputBox will return a null string if user clicks on Cancel. Try
something like this:

Sub AutoInsertRows()

'Shortcut Keystroke CTRL+Shift+A

Dim r As String

r = InputBox("How Many Rows?", "Auto-Insert Rows", 1)
If r = "" Then
MsgBox "user cancelled"
Exit Sub
End If
If Not IsNumeric(r) Then
MsgBox "value must be numeric"
Exit Sub
End If
i = 0

Do While i < r

If Not r = vbCancel Then

Selection.Insert Shift:=xlDown

i = i + 1

Else

Exit Sub

End If

Loop

End Sub





--
Hope that helps.

Vergel Adriano


"Rob" wrote:

Hello, I have the below code that prompts for the qty of rows to be inserted
but when the Cancel button is selected instead of providing a number and
clicking OK I get an error. I was hoping that putting the IF statement in
there would fix it but it did not. How can I fix this?

Thanks in Advance.


Sub AutoInsertRows()

'Shortcut Keystroke CTRL+Shift+A

Dim r As Integer

r = InputBox("How Many Rows?", "Auto-Insert Rows", 1)
i = 0

Do While i < r

If Not r = vbCancel Then

Selection.Insert Shift:=xlDown

i = i + 1

Else

Exit Sub

End If

Loop

End Sub


  #5   Report Post  
Posted to microsoft.public.excel.programming
Rob Rob is offline
external usenet poster
 
Posts: 718
Default Capture InputBox vbCancel

Thanks A ton, it's working now.

Cheers.

"Vergel Adriano" wrote:

Hi Rob,

I believe InputBox will return a null string if user clicks on Cancel. Try
something like this:

Sub AutoInsertRows()

'Shortcut Keystroke CTRL+Shift+A

Dim r As String

r = InputBox("How Many Rows?", "Auto-Insert Rows", 1)
If r = "" Then
MsgBox "user cancelled"
Exit Sub
End If
If Not IsNumeric(r) Then
MsgBox "value must be numeric"
Exit Sub
End If
i = 0

Do While i < r

If Not r = vbCancel Then

Selection.Insert Shift:=xlDown

i = i + 1

Else

Exit Sub

End If

Loop

End Sub





--
Hope that helps.

Vergel Adriano


"Rob" wrote:

Hello, I have the below code that prompts for the qty of rows to be inserted
but when the Cancel button is selected instead of providing a number and
clicking OK I get an error. I was hoping that putting the IF statement in
there would fix it but it did not. How can I fix this?

Thanks in Advance.


Sub AutoInsertRows()

'Shortcut Keystroke CTRL+Shift+A

Dim r As Integer

r = InputBox("How Many Rows?", "Auto-Insert Rows", 1)
i = 0

Do While i < r

If Not r = vbCancel Then

Selection.Insert Shift:=xlDown

i = i + 1

Else

Exit Sub

End If

Loop

End Sub


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I capture CANCEL on an Inputbox? Toxicdistortion Excel Programming 3 October 27th 06 01:23 AM
How do I capture CANCEL on an Inputbox? Toxicdistortion Excel Programming 0 October 27th 06 12:37 AM
capture only part of inputBox or of cell contents JCIrish Excel Programming 4 October 21st 06 05:43 PM
InputBox to capture user selected sheet names? quartz[_2_] Excel Programming 7 December 3rd 05 10:51 AM
vbCancel Robin Clay[_3_] Excel Programming 6 October 24th 03 12:03 PM


All times are GMT +1. The time now is 08:27 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"