LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Macro to increase cell number by 1 at a time

Button from the Forms Toolbar.

Keypress
Control, Ctrl : Not used
Shift, shft : Decrement by 1
alt : return to zero
Nothing : increment by 1

Assign this macro to all buttons needing to do this.

The declarations should be at the top of a general module:


Option Explicit
Declare Function GetKeyState Lib "user32" _
(ByVal nVirtKey As Long) As Integer

Const VK_SHIFT = &H10
Const VK_CONTROL = &H11
Const VK_MENU = &H12
Const KEYISDOWN = &HF0000000

Sub BtnClick()

Dim ikey As Integer
Dim btn As Button
Dim sName As String
Dim rng As Range

'CHECK SEE IF Ctrl or Alt key is down

ikey = 0
If (GetKeyState(VK_MENU) And KEYISDOWN) = _
KEYISDOWN Then
ikey = 1
End If

If ikey < 1 Then
If (GetKeyState(VK_SHIFT) And KEYISDOWN) = _
KEYISDOWN Then
ikey = 2
End If
End If

sName = Application.Caller
Set btn = ActiveSheet.Buttons(sName)
Set rng = btn.TopLeftCell
btn.Caption = rng.Value


Select Case ikey
Case 0 ' neither Ctrl or Shift
rng.Value = rng.Value + 1
Case 1 ' Alt depressed, reset to 0
rng.Value = 0
Case 2 ' shift pressed, subtract 1
If rng.Value 0 Then
rng.Value = rng.Value - 1
End If
End Select
btn.Caption = rng.Value

End Sub

If you hold down Ctrl, then you select the button, so I couldn't use ctrl.

To subtract 1, hold down Shift while you click

To zero hold down Alt while you click.

--
Regards,
Tom Ogilvy



"felipe" wrote in message
...
Wow, thanks for the prompt response. Is there a way to have the same

button
take out 1 point when clicked by holding ctrl or something like that? That
way I would only need one button per cell. The stat sheet I'm doing has

many
categories, so clearing everything would not work for me. Thanks again.

"Tom Ogilvy" wrote:

Create code like this

Public Sub BtnClick()
Dim btn as Button
Dim sName as String
Dim rng as Range
sName = Application.Caller
set btn = activesheet.Buttons(sName)
set rng = btn.TopLeftCell
rng.value = rng.value + 1
btn.caption = rng.value
End Sub


On the forms toolbar (make it visible), click on the button, then

rubberband
an area on the sheet where you want it.

You will be prompted to assign a macro (or right click on the button and
select assign macro). Assign the above macro.

A macro to clear scores

Public Sub ClearScores()
Dim btn as Button
Dim rng as Range
for each btn in Activesheet.Buttons
set rng = btn.TopLeftCell
rng.Value = 0
btn.Caption = rng.value
Next
End Sub

--
Regards,
Tom Ogilvy


"felipe" wrote in message
...
I'm trying to create a stat sheet to record points for a volleyball

match.
How can I create a macro as a button to add 1 to the cell each time I

click
it and have the number be represented in the button. Thanks.








 
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 to increase a number by 1 every time the spreadsheet opens? SLD Excel Discussion (Misc queries) 1 September 15th 09 08:30 PM
how do i have a cell number increase by 1 each time file opened DJbase Excel Discussion (Misc queries) 1 May 25th 08 04:17 AM
Number in cell increase with increase in font size. Value increases with increase in font.[_2_] Excel Discussion (Misc queries) 2 August 9th 07 01:58 PM
How to automtically increase a number every time a file is used falcp Excel Discussion (Misc queries) 1 January 16th 06 03:29 PM
Macro to increase cell value by 1 each time button clicked fozzer[_2_] Excel Programming 2 April 29th 04 01:58 PM


All times are GMT +1. The time now is 06:54 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"