Button of 0 and 1

andrichara

New Member
Joined
Jan 13, 2019
Messages
14
hi guys i would like to make a 2 buttons.The one will result 0 and the other one 1 at A7:A47.so when i press the zero button for examplle will result a zero at A7,then when i press again any of both will result at A8..then A9 and so on.can you help me on that?thank you!
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
51,409
Office Version
365
Platform
Windows
Assuming that you have nothing in column A below row 47, here are the two prcoedures you need:
Code:
Sub ZeroButton()

    Dim r As Long
    
'   Find next row to populate
    r = Cells(Rows.Count, "A").End(xlUp).Row + 1
    
'   Verify row selection
    If r < 7 Then
        r = 7
    Else
        If r > 47 Then
            MsgBox "Cell A47 already populated", vbOKOnly
            Exit Sub
        End If
    End If
    
'   Populate cell
    Cells(r, "A") = 0
    
End Sub


Sub OneButton()

    Dim r As Long
    
'   Find next row to populate
    r = Cells(Rows.Count, "A").End(xlUp).Row + 1
    
'   Verify row selection
    If r < 7 Then
        r = 7
    Else
        If r > 47 Then
            MsgBox "Cell A47 already populated", vbOKOnly
            Exit Sub
        End If
    End If
    
'   Populate cell
    Cells(r, "A") = 1
    
End Sub
After adding the code, then just add a few command buttons and add this VBA code to each one.
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
8,733
Office Version
2007
Platform
Windows
Another option:

Code:
Sub Button1()
  Call FillValue(1)
End Sub
Sub Button2()
  Call FillValue(0)
End Sub
Sub FillValue(n As Long)
  Dim i As Long
  For i = 7 To 47
    If Cells(i, "A").Value = "" Then
      Cells(i, "A").Value = n
      Exit For
    End If
  Next
  If i = 48 Then MsgBox "No cells available"
End Sub
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
51,409
Office Version
365
Platform
Windows
Nice one Dante!

I knew there had to be a way to avoid duplicating very similar code, and overlooked the obvious (a single procedure with parameters!)
 

Forum statistics

Threads
1,081,441
Messages
5,358,709
Members
400,508
Latest member
fish31

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top