+ Reply to Thread
Results 1 to 9 of 9

VBA, Userform with checkboxes - How to change the value from "Yes" to "1"

Hybrid View

  1. #1
    Registered User
    Join Date
    04-18-2013
    Location
    Bristol, England
    MS-Off Ver
    Excel 2010
    Posts
    33

    Exclamation VBA, Userform with checkboxes - How to change the value from "Yes" to "1"

    Hi all

    I'm creating a UserForm which will add data to my worksheet.
    I currently have checkboxes, which once the data is added the value of a checked box equals "Yes" and if unchecked it is "No"

    Is there anyway I can tell excel to change the added value from "Yes" to "1" and "No" to "0"?

    Otherwise, the easiest way will just be to add a Text Box and manually enter in the numbers.

    This is used so I can calculate the amount of times somebody was late, sick or on holiday.

    Thanks

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA, Userform with checkboxes - How to change the value from "Yes" to "1"

    You could use something like this, which will put 1 in C10 if the checkbox is checked, 0 otherwise.
    Range("C10").Value = Iif(Checkbox5.Value, 1, 0)
    PS Are you sure your checkboxes return Yes/No? All of mine return True/False.
    Last edited by Norie; 12-05-2013 at 12:19 PM. Reason: To many i in my Iif.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    04-18-2013
    Location
    Bristol, England
    MS-Off Ver
    Excel 2010
    Posts
    33

    Re: VBA, Userform with checkboxes - How to change the value from "Yes" to "1"

    I have added this;
    Range("").Value = IIf(Sickness_Check.Value, 1, 0)
    Range("").Value = IIf(SicknessPhone_Check.Value, 1, 0)
    Range("").Value = IIf(Holiday_Check.Value, 1, 0)
    Range("").Value = IIf(Lateness_Check.Value, 1, 0)
    Range("").Value = IIf(Absent_Check.Value, 1, 0)
    Range("").Value = IIf(Disc_Check.Value, 1, 0)
    but it's came up with this message;
    EXCELHELP.PNG

    When the Data is added it finds the last row and adds it there, is the range able to be limited to a collumn?

  4. #4
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: VBA, Userform with checkboxes - How to change the value from "Yes" to "1"

    With if statements.
    If CheckBox1.Value = True Then
        Range("A1").Value = 1
    Else
        Range("A1").Value = 0
    End If
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  5. #5
    Registered User
    Join Date
    04-18-2013
    Location
    Bristol, England
    MS-Off Ver
    Excel 2010
    Posts
    33

    Re: VBA, Userform with checkboxes - How to change the value from "Yes" to "1"

    You're right they are "TRUE" and "FALSE"
    What does "Range" refer to? If I use C10, will every new field added overwrite the existing values in C10 or will it carry on with adding it to the next available row?

    *EDIT* If I remove the cell so only "" shows will this keep it in flow with adding to last row?

    If CheckBox1.Value = True Then
        Range("").Value = 1
    Else
        Range("").Value = 0
    End If
    Last edited by jpoppet; 12-05-2013 at 12:31 PM. Reason: Another thought

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA, Userform with checkboxes - How to change the value from "Yes" to "1"

    C10 is a hard coded reference I used in the example code I posted.

    I don't know where you want the 1/0 to go so change it however you need to.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA, Userform with checkboxes - How to change the value from "Yes" to "1"

    What code were you using previously to put the data from the userform on the worksheet?

    PS Range("") is wrong.

  8. #8
    Registered User
    Join Date
    04-18-2013
    Location
    Bristol, England
    MS-Off Ver
    Excel 2010
    Posts
    33

    Re: VBA, Userform with checkboxes - How to change the value from "Yes" to "1"

    Here's the full code.
    I'm dubious of deleting the frame_click but I'm pretty sure I don't need them?

    Private Sub Added_text_Change()
    
    End Sub
    
    Private Sub Date_Label_Click()
    
    End Sub
    
    Private Sub Employee_Text_Change()
    
    End Sub
    
    Private Sub Frame1_Click()
    
    End Sub
    
    Private Sub Frame2_Click()
    
    End Sub
    
    Private Sub Frame4_Click()
    
    End Sub
    
    Private Sub Label1_Click()
    
    End Sub
    
    Private Sub save_button_Click()
    
    Dim iRow As Long
    Dim ws As Worksheet
    Set ws = Worksheets("RAW")
    
    
    'find first empty row in database
    iRow = ws.Cells(Rows.Count, "A").End(xlUp).Row + 1
    
    'check for a Name number
    If Trim(Me.Employee_Text.Value) = "" Then
    Me.Employee_Text.SetFocus
    MsgBox "Please complete the form"
    Exit Sub
    End If
    
    'copy the data to the database
    ws.Cells(iRow, 1).Value = Me.Date_Text.Value
    ws.Cells(iRow, 2).Value = Me.Employee_Text.Value
    ws.Cells(iRow, 3).Value = Me.Sickness_Check.Value
    ws.Cells(iRow, 4).Value = Me.SicknessPhone_Check.Value
    ws.Cells(iRow, 5).Value = Me.Holiday_Check.Value
    ws.Cells(iRow, 6).Value = Me.Lateness_Check.Value
    ws.Cells(iRow, 7).Value = Me.Absent_Check.Value
    ws.Cells(iRow, 8).Value = Me.Disc_Check.Value
    ws.Cells(iRow, 9).Value = Me.SelfCert_Text.Value
    ws.Cells(iRow, 10).Value = Me.HolidayForm_Text.Value
    ws.Cells(iRow, 11).Value = Me.DiscLetter_Text.Value
    ws.Cells(iRow, 12).Value = Me.LateTime_Text.Value
    ws.Cells(iRow, 13).Value = Me.Added_text.Value
    
    
    
    MsgBox "Data added", vbOKOnly + vbInformation, "Data Added"
    'clear the data
    Me.Employee_Text.Value = ""
    Me.Sickness_Check.Value = ""
    Me.SicknessPhone_Check.Value = ""
    Me.Holiday_Check.Value = ""
    Me.Lateness_Check.Value = ""
    Me.Absent_Check.Value = ""
    Me.Disc_Check.Value = ""
    Me.SelfCert_Text.Value = ""
    Me.HolidayForm_Text.Value = ""
    Me.DiscLetter_Text.Value = ""
    Me.LateTime_Text.Value = ""
    Me.Added_text.Value = ""
    Me.Employee_Text.SetFocus
    
    Range("Sick_Note").Value = IIf(Sickness_Check.Value, 1, 0)
    Range("Sick_No_Note").Value = IIf(SicknessPhone_Check.Value, 1, 0)
    Range("Holiday").Value = IIf(Holiday_Check.Value, 1, 0)
    Range("Late").Value = IIf(Lateness_Check.Value, 1, 0)
    Range("Absent").Value = IIf(Absent_Check.Value, 1, 0)
    Range("Disciplinary").Value = IIf(Disc_Check.Value, 1, 0)
    
    
    End Sub
    
    Private Sub exit_button_Click()
    
    Unload Me
    
    End Sub
    
    Private Sub Employee_Click()
    
    End Sub
    
    Private Sub SicknessPhone_Check_Click()
    
    End Sub
    
    Private Sub UserForm_Click()
    
    End Sub
    Obviously you can see that I've changed the range, I created a name range for each column but it's just added all values in all rows of each column :P
    Last edited by jpoppet; 12-05-2013 at 12:59 PM. Reason: Forgot to Add

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA, Userform with checkboxes - How to change the value from "Yes" to "1"

    Why have you created the ranges?

    You should just need to change this code,
    ws.Cells(iRow, 3).Value = Me.Sickness_Check.Value
    ws.Cells(iRow, 4).Value = Me.SicknessPhone_Check.Value
    ws.Cells(iRow, 5).Value = Me.Holiday_Check.Value
    ws.Cells(iRow, 6).Value = Me.Lateness_Check.Value
    ws.Cells(iRow, 7).Value = Me.Absent_Check.Val
    like this.
    ws.Cells(iRow, 3).Value = Iif(Me.Sickness_Check.Value,1,0)
    ws.Cells(iRow, 4).Value = Iif(Me.SicknessPhone_Check.Value,1,0)
    ws.Cells(iRow, 5).Value = Iif(Me.Holiday_Check.Value,1,0)
    ws.Cells(iRow, 6).Value = Iif(Me.Lateness_Check.Value,1,0)
    ws.Cells(iRow, 7).Value = Iif(Me.Absent_Check.Value,1,0)
    Also, I think you can get rid of this, and the names.
    Range("Sick_Note").Value = IIf(Sickness_Check.Value, 1, 0)
    Range("Sick_No_Note").Value = IIf(SicknessPhone_Check.Value, 1, 0)
    Range("Holiday").Value = IIf(Holiday_Check.Value, 1, 0)
    Range("Late").Value = IIf(Lateness_Check.Value, 1, 0)
    Range("Absent").Value = IIf(Absent_Check.Value, 1, 0)
    Range("Disciplinary").Value = IIf(Disc_Check.Value, 1, 0)
    PS It should be OK to delete the empty subs like Frame1_Click, in fact if you goto Debug>Compile Project they might disappear.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 4
    Last Post: 11-17-2013, 12:05 PM
  2. [SOLVED] How to USE """"" cells count """"" change font color
    By austin123456 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-09-2013, 06:14 AM
  3. Replies: 5
    Last Post: 10-12-2010, 06:46 AM
  4. Replies: 3
    Last Post: 07-27-2008, 06:31 AM
  5. Replies: 7
    Last Post: 05-13-2006, 05:02 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1