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,434
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,768
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,434
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,728
Messages
5,360,920
Members
400,602
Latest member
newaqua

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top