Command button macro

M

Mark N

I want to assign a macro to a command button so that it increases the value
of a cell by 1
Thanks in advance if you can help me.
 
J

Jim Thomlinson

Place this code in a standard code module and assign it to a coomand button...

Public Sub Increment()
Range("A1").Value = Range("A1").Value + 1
End Sub
 
S

Sean Timmons

So, the Macro will be Range("A1") = Range("A1") + 1

And just add a command button and assign the macro.
 
M

Mark N

Thanks Jim,
I forgot but i would need to reset the cell also. do i need another command
button to reset?
 
S

Sean Timmons

Public Sub Increment()
Range("A1").Value = 0
Range("A1").Value = Range("A1").Value + 1
End Sub

Would reset the cell, but then that means you're always goingto have 0, then
1........
 
J

Jim Thomlinson

How you wish to rest is up to you. A command button seems logical. In that
case attach this code

public sub ResetTo0()
range("A1").value = 0
end sub
 
C

Chip Pearson

First, create the follow procedures in a VBA module.

Public Sub IncrementSpecificCell()
With ThisWorkbook.Worksheets("Sheet1").Range("A1")
.Value = .Value + 1
End With
End Sub

Public Sub IncrementActiveCell()
With ActiveCell
.Value = .Value + 1
End With
End Sub

Public Sub ClearSpecificCell()
ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = ""
End Sub

Public Sub ClearActiveCell()
ActiveCell.Value = ""
End Sub

Then, pick the ones you want to assign to command buttons.
IncrementSpecificCell always increments cell A1, regardless of what
cell happens to be active. IncrementActiveCell increments the active
cell. ClearSpecificCell always clears A1, regardless of what cell is
active. ClearActiveCell clears the active cell.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top