vendredi 9 novembre 2018

Checkbox in Userform1 to display yes and no. VBA code

I have a sheet named keylist and made a userform1 for it. Simple and nothing fancy.

The options on the userform1 are Date, Key #, Aaddress, Key in (checkbox1), Key out(checkbox2) and Comment.

I have the userform1 working and all, except the check boxes. This is what I've got:

Private Sub CommandButton1_Click()

Dim dcc As Long
 Dim abc As Worksheet

 Set abc = Worksheets("Key List")

With abc

   dcc = .Range("A" & Rows.Count).End(xlUp).Row

.Cells(dcc + 1, 1).Value = Date
.Cells(dcc + 1, 2).Value = Me.TextBox1.Value
.Cells(dcc + 1, 3).Value = Me.TextBox2.Value
.Cells(dcc + 1, 4).Value = Me.CheckBox1.Value
'.Cells(dcc + 1, 5).Value = Me.TextBox4.Value
.Cells(dcc + 1, 6).Value = Me.TextBox3.Value

If Me.CheckBox1.Value = True Then

.Cells(dcc + 1, 4).Value = "Yes"

Else

TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""

End If
End With

End Sub  

And:

Private Sub UserForm_Initialize()

Dim wsActive As Worksheet
Dim i As Long, LastRow As Long
Set wsActive = ActiveSheet
LastRow = wsActive.Cells(wsActive.Rows.Count).End(xlUp).Row

End Sub

I had the code after the else checkbox1 but it wasn't really working so I deleted it. The ' in textbox4 is intentional because I was thinking of adding another field in sheet and form and then decided not to but left it in case I need it.

As I said above all works but the checkbox functionality is not working. What I'm aiming was/is that if "In" is checked in form then yes goes in on the sheet and if "In" is checked that "Out" can not be checked. Logically it won't make sense for anything to be in and out at the same time and it's one or the other at any given time. So how do I go about it and make them check boxes work?

Sheet Form

I tried few things and none worked. If anybody can help with the VBA code and help get checkboxes working, I'd appreciate it very much. One more thing that if add entry is clicked on userform1 and no fields are filled data is entered still. How can I stop that from happening? User must fill in the fields in order to make an entry or else if by mistake add entry is clicked and blank entry goes in the sheet, sort of blank because date is entered still.

Thanks in advance and I appreciate your help. Cheers.




Aucun commentaire:

Enregistrer un commentaire