PDA

View Full Version : Solved: Error when clicking "Cancel" Button



pingwin77
03-17-2009, 01:01 PM
I have the follwing code assigned to a button. This allows me to enter how many parts I have on my report. When I click the "Cancel" button, I get an error. The Debug stops on the Highlighted line (RED). What am I doing wrong?

Private Sub CommandButtonADDparts_Click()
'
' AddParts Macro
' Macro recorded 5/7/2008 by AIS
Application.ScreenUpdating = False
Dim Check, Counter, Features As Range, Message, Title, Default, PartsValue

' Display message, title, and default value.
PartsValue = InputBox("Enter number of Parts", "Add Parts Columns", 0)
If PartsValue = 0 Then GoTo line2 Else GoTo line1
PartsValue = PartsValue - 1

line1:
Check = True: Counter = PartsValue ' Add number of parts
Range("J10").Select
Range("J10:K550").Select
Selection.Copy
Do ' Outer loop.
Do While Counter > 0 ' Inner loop.
Counter = Counter - 1 ' Increment Counter.

ActiveCell.Offset(0, 2).Activate
ActiveSheet.Paste
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = ActiveCell.Offset(0, -2) + 1
ActiveCell.Offset(0, -1).Activate

If Counter = 0 Then ' If condition is True.
Check = False ' Set value of flag to False.
Exit Do ' Exit inner loop.
End If
Loop
Loop Until Check = False ' Exit outer loop immediately.



Application.CutCopyMode = False
Range("B12").Select

line2:
End Sub

Bob Phillips
03-17-2009, 01:11 PM
Which Cancel button?

pingwin77
03-17-2009, 01:34 PM
if you click the "ADD PARTS" button on the top of the sheet, instead of entering a number and clicking OK, click CANCEL and you will see what I mean.

mdmackillop
03-17-2009, 01:45 PM
Don't put the 0 default value in the InputBox

' Display message, title, and default value.
PartsValue = InputBox("Enter number of Parts", "Add Parts Columns")
If PartsValue = "" Then GoTo line2 Else GoTo line1

Bob Phillips
03-17-2009, 01:46 PM
Private Sub CommandButtonADDparts_Click()
'
' AddParts Macro
' Macro recorded 5/7/2008 by AIS
Application.ScreenUpdating = False
Dim Check, Counter, Features As Range, Message, Title, Default, PartsValue

' Display message, title, and default value.
PartsValue = InputBox("Enter number of Parts", "Add Parts Columns", 0)
If PartsValue = "" Then Goto line2 Else Goto line1
PartsValue = PartsValue - 1

line1:
Check = True: Counter = PartsValue ' Add number of parts
Range("J10").Select
Range("J10:K550").Select
Selection.Copy
Do ' Outer loop.
Do While Counter > 0 ' Inner loop.
Counter = Counter - 1 ' Increment Counter.

ActiveCell.Offset(0, 2).Activate
ActiveSheet.Paste
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = ActiveCell.Offset(0, -2) + 1
ActiveCell.Offset(0, -1).Activate

If Counter = 0 Then ' If condition is True.
Check = False ' Set value of flag to False.
Exit Do ' Exit inner loop.
End If
Loop
Loop Until Check = False ' Exit outer loop immediately.



Application.CutCopyMode = False
Range("B12").Select

line2:
End Sub

pingwin77
03-17-2009, 01:50 PM
Thanks XLD! Your code works great!

pingwin77
03-18-2009, 02:04 PM
I just realized something about this after some more testing. I need the code to copy one less then the number entered.

(example: If i enter 3 for the input box I need the code to copy the reference cells 2 times so I have a total of 3 chunks of data.)

The "PartsValue = PartsValue - 1" is supposed to take care of this. The logic statement before that lines skips it. Where do I add this in my code so it works? I have tried a few other places in the code and it locks up on me.

Bob Phillips
03-18-2009, 02:47 PM
Private Sub CommandButtonADDparts_Click()
'
' AddParts Macro
' Macro recorded 5/7/2008 by AIS
Application.ScreenUpdating = False
Dim Check, Counter, Features As Range, Message, Title, Default, PartsValue

' Display message, title, and default value.
PartsValue = InputBox("Enter number of Parts", "Add Parts Columns", 0)
If PartsValue = "" Then Goto line2
PartsValue = PartsValue - 1

line1:
Check = True: Counter = PartsValue ' Add number of parts
Range("J10").Select
Range("J10:K550").Select
Selection.Copy
Do ' Outer loop.
Do While Counter > 0 ' Inner loop.
Counter = Counter - 1 ' Increment Counter.

ActiveCell.Offset(0, 2).Activate
ActiveSheet.Paste
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = ActiveCell.Offset(0, -2) + 1
ActiveCell.Offset(0, -1).Activate

If Counter = 0 Then ' If condition is True.
Check = False ' Set value of flag to False.
Exit Do ' Exit inner loop.
End If
Loop
Loop Until Check = False ' Exit outer loop immediately.



Application.CutCopyMode = False
Range("B12").Select

line2:
End Sub

pingwin77
03-19-2009, 06:34 AM
Perfect! Thanks again for all the help!