Stop Previous button:counts into negatives

DaveAtlanta

Board Regular
Joined
Nov 9, 2005
Messages
100
I'm sure it's elementary coding for many.

I have two little buttons that increase / decrease a number in my cell. What can I add to set the lowest number to one or zero and not allow it to continue into negatives?

Code:
Sub NextButton()
Dim Button As Integer
Button = Range("C1")

Button = Button + 1
Range("C1") = Button
End Sub

Sub PrevButton()
Dim Button As Integer
Button = Range("C1")

Button = Button - 1
Range("C1") = Button
End Sub

This number is used as the row number which other data is displayed.

Thanks in advance for any help.
Dave
In Atlanta
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi,

Try something like
Code:
Sub PrevButton()
Dim Button As Integer
Button = Range("C1")

Button = Button - 1
Range("C1") = Button
prevButton.Enabled=Button>1
End Sub
 
Upvote 0
Try

Code:
Sub PrevButton()
Dim Button As Integer
Button = Range("C1")
If Button = 1 Then Exit Sub
Button = Button - 1
Range("C1") = Button
End Sub
 
Upvote 0
Actually the code should be:
Code:
Sub NextButton()
Dim Button As Integer
Button = Range("C1")

Button = Button + 1
Range("C1") = Button
PrevButton.Enabled = Button > 1
End Sub

Sub PrevButton()
Dim Button As Integer
Button = Range("C1")

Button = Button - 1
Range("C1") = Button
PrevButton.Enabled = Button > 1

End Sub

Erm... Shouldnt these be Click events?
 
Upvote 0
Thank You! !


Alan, I haven't learned 'click events' yet, so I'm not sure if they should be or not.

Thanks for everyones help.
Dave
in Atlanta
 
Upvote 0
Hi Dave,

If you created your command button by:
View / toolbars / Control toolbox
then
Click the 'Command Button' icon then on worksheet click and drag to size.

You can then right-click the command button, select 'View code' and place this code into the CommandButton1 click event:
Code:
Private Sub CommandButton1_Click()
Dim iButton As Integer

iButton = Range("C1").Value
iButton = iButton - 1
Range("C1").Value = iButton
CommandButton1.Enabled = iButton > 1
End Sub

You will, of course, also need to create a 'Next' button with this line inserted at a suitable point:
Code:
CommandButton1.Enabled = iButton > 1
 
Upvote 0

Forum statistics

Threads
1,203,491
Messages
6,055,727
Members
444,814
Latest member
AutomateDifficulty

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