+ Reply to Thread
Results 1 to 17 of 17

Capture data from OptionBox into datasheet

Hybrid View

  1. #1
    Registered User
    Join Date
    07-30-2013
    Location
    England
    MS-Off Ver
    Excel 2003, Excel 2010
    Posts
    13

    Capture data from OptionBox into datasheet

    Hello again!

    Could anyone please help me with the below?

    I have a userForm to capture various data into excel sheet. everything works fine but i have two things to add and zero idea how to do it.

    1. add data entry from a option boxes...

    There are 2 groups of optionBoxes - "Unit" (3 to choose - UNIT A, UNIT B, UNIT C) and "type" - 9 to choose - red1 red2 etc....
    I would like the user to be able to select ONE unit and ONE Type and after the form is submitted it would add the exact values into specific row as in the code below. (Grouping is done)
    Here's thecode I already have:

    Private Sub cmdSubmit_Click()
    Dim ssheet As Worksheet
    
    Set ssheet = ThisWorkbook.Sheets("Workers")
    
    
    nr = ssheet.Cells(Rows.Count, 3).End(xlUp).Row + 1
    
    
    Cells(nr, 3) = Me.boxSurname.Text
    Cells(nr, 4) = Me.boxForename.Text
    Cells(nr, 6) = Me.boxPhone.Value
    Cells(nr, 9) = Me.boxDob.Value
    Cells(nr, 16) = Me.boxoptout.Text
    MsgBox "Employee Added"
    
    
    UserForm1.Hide
    End Sub
    
    Private Sub boxDob_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    On Error Resume Next
    Me.boxDob = Format(Me.boxDob, "dd/mm/yyyy")
    End Sub
    
    
    Private Sub boxIdexp_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    On Error Resume Next
    Me.boxIdexp = Format(Me.boxIdexp, "dd/mm/yyyy")
    End Sub
    Also i would like it to convert the input of "y" in
    Cells(nr, 16) = Me.boxoptout.Text
    into UPPERCASE automaticly before submitting data.
    And Finally at the very end I would like the form to clear before it closes.

    Any help would be amazing!
    Last edited by Daniel B; 08-21-2013 at 04:14 PM. Reason: added extra request

  2. #2
    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: Capture data from OptionBox into datasheet

    I'm a bit confused, what type of control are you using? OptionButton? ComboBox? ListBox?

    It looks like you have most of what you want. Without testing the code I see a couple of things you might want to change. You need to qualify which cells you want to place the values in, and what you'd like to change about the cells. Like:
    Cells(nr, 3) = Me.boxSurname.Text
    ssheet.Cells(nr, 4).value = Me.boxForename.Text
    ssheet.Cells(nr, 6).value = Me.boxPhone.Value
    ssheet.Cells(nr, 9).value = Me.boxDob.Value
    ssheet.Cells(nr, 16).value = UCase(Me.boxoptout.Text)
    MsgBox "Employee Added"
    e/ On your second question, if you use unload.me to close the userform, I believe it will reset all controls in the userform.
    Last edited by Solus Rankin; 08-21-2013 at 04:28 PM.
    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."

  3. #3
    Registered User
    Join Date
    07-30-2013
    Location
    England
    MS-Off Ver
    Excel 2003, Excel 2010
    Posts
    13

    Re: Capture data from OptionBox into datasheet

    Quote Originally Posted by XeRo Solus View Post
    I'm a bit confused, what type of control are you using? OptionButton? ComboBox? ListBox?

    It looks like you have most of what you want. Without testing the code I see a couple of things you might want to change. You need to qualify which cells you want to place the values in, and what you'd like to change about the cells. Like:
    Cells(nr, 3) = Me.boxSurname.Text
    ssheet.Cells(nr, 4).value = Me.boxForename.Text
    ssheet.Cells(nr, 6).value = Me.boxPhone.Value
    ssheet.Cells(nr, 9).value = Me.boxDob.Value
    ssheet.Cells(nr, 16).value = UCase(Me.boxoptout.Text)
    MsgBox "Employee Added"
    e/ On your second question, if you use unload.me to close the userform, I believe it will reset all controls in the userform.
    First of all thanks for the "UPPER" value things... so simple I feel embarrassed now....

    To specify what I wanted in the first placechoice.jpg
    this is a screen of what the selection part of user form look like. Those are "OptionButton"s
    I want to be able to select "Unit" and "Shift" and capture the data into appropriate row in excel.

    Hope this helps ...

  4. #4
    Registered User
    Join Date
    07-30-2013
    Location
    England
    MS-Off Ver
    Excel 2003, Excel 2010
    Posts
    13

    Re: Capture data from OptionBox into datasheet

    First of all thanks for the "UPPER" value things... so simple I feel embarrassed now....

    To specify what I wanted in the first placeAttachment 259700
    this is a screen of what the selection part of user form look like. Those are "OptionButton"s
    I want to be able to select "Unit" and "Shift" and capture the data into appropriate row in excel.

    Hope this helps ...
    Last edited by Daniel B; 08-21-2013 at 05:16 PM.

  5. #5
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Capture data from OptionBox into datasheet

    Ok re the option buttons.

    You should be using Comboboxes.

    you can populate these as part of the userform activation process.

    Private Sub UserForm_Activate()
    
    'Fill Comboboxes
    ComboBox1.List = Array("No", "Yes")
    ComboBox2.List = Array("Red1", "Red2")
    
    End Sub

    When you click on a combobox you trigger the combobox Change event. Use this to get at your data.

    Private Sub ComboBox1_Change()
    
    'The elist index tells you which item on the list you selected from 0 to ( items in list -1 )
    
    SelectedOption = ComboBox1.ListIndex
    
    SelectedText = ComboBox1.value
    
    ' So act on the data.
    
    End Sub
    I hope that helps.

    If you want all options to be visible then use a listbox instead of a combobox.

  6. #6
    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: Capture data from OptionBox into datasheet

    Okay you have frames around your option buttons are you trying to place the caption on the option button in the cell? or the caption from the frame the option button is in?

    The only reason I ask is that Unit and Shift are you frame captions.

  7. #7
    Registered User
    Join Date
    07-30-2013
    Location
    England
    MS-Off Ver
    Excel 2003, Excel 2010
    Posts
    13

    Re: Capture data from OptionBox into datasheet

    There are 2 values to go into two separate cells - Unit and Shift.

  8. #8
    Registered User
    Join Date
    07-30-2013
    Location
    England
    MS-Off Ver
    Excel 2003, Excel 2010
    Posts
    13

    Re: Capture data from OptionBox into datasheet

    It seems like i managed to confuse everyone.
    To be hones i only put those frames around as it looks good that way...
    If they are not needed than i can get rid of them.

    As far as my very limited VBA knowledge goes, the "Unit" and "Shift" are grouped separately inside of that frame...
    so sorry for confusing u

  9. #9
    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: Capture data from OptionBox into datasheet

    Why don't you attach the workbook you're working on using the method I described earlier and we can talk specifics

  10. #10
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,952

    Re: Capture data from OptionBox into datasheet

    Daniel B,
    Your post does not comply with Rule 12 of our Forum RULES. Don't quote whole posts -- it's just clutter. If you are responding to a post out of sequence, limit quoted content to a few relevant lines that makes clear to whom and what you are responding.
    Ben Van Johnson

  11. #11
    Registered User
    Join Date
    07-30-2013
    Location
    England
    MS-Off Ver
    Excel 2003, Excel 2010
    Posts
    13

    Re: Capture data from OptionBox into datasheet

    Ok, i have attached the workbook.
    I had to delete all the data and other tabs as it was over 20MB is size....:O
    hope this helps!
    Attached Files Attached Files

  12. #12
    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: Capture data from OptionBox into datasheet

    Okay. Now I see what you're working with. And I agree, it looks good with frames.

    Describe to me what cells you want information placed in when you click one of these option buttons. For example:

    If the Unit A button is clicked I want ________ placed in X cell.

  13. #13
    Registered User
    Join Date
    07-30-2013
    Location
    England
    MS-Off Ver
    Excel 2003, Excel 2010
    Posts
    13

    Re: Capture data from OptionBox into datasheet

    So, The selection of UNIT goes into Column "AL"
    and the shift goes to column "AM"
    It has to type Exactly what the selection is.

    ALSO, if possible

    IF
    nothing was selected
    than it would choose
    for "UNIT - "ALL"
    for "SHIFT" - "FLEX"
    and place the input in the cells as above.

  14. #14
    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: Capture data from OptionBox into datasheet

    So if I click the Unit A button it will say Unit A in AL1?

    Next time I click Unit C button it will say Unit C in AL2?

  15. #15
    Registered User
    Join Date
    07-30-2013
    Location
    England
    MS-Off Ver
    Excel 2003, Excel 2010
    Posts
    13

    Re: Capture data from OptionBox into datasheet

    Quote Originally Posted by XeRo Solus View Post
    So if I click the Unit A button it will say Unit A in AL1?

    Next time I click Unit C button it will say Unit C in AL2?
    No. When all data from user form gets filled in, than after clicking submit it will paste it all in the next empty Row. so Unit will be in AL8 and shift in AM8.
    Last edited by Daniel B; 08-21-2013 at 08:25 PM.

  16. #16
    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: Capture data from OptionBox into datasheet

    Why don't you try placing this in the submit button macro wherever you want the option buttons to be evaluated. Test it thoroughly and let me know what you think.

    Dim cOpt As Control
    Dim x As Integer
    Dim y As Integer
    
    x = 0
    y = 0
    For Each cOpt In Me.Unit
        If cOpt.Value = True Then
            Sheets("Workers").Range("AL" & Rows.Count).End(xlUp).Offset(1).Value = cOpt.Caption
            x = x + 1
        End If
    Next cOpt
    If x > 0 Then Sheets("Workers").Range("AL" & Rows.Count).End(xlUp).Offset(1).Value = "ALL"
    For Each cOpt In Me.Shift
        If cOpt.Value = True Then
            Sheets("Workers").Range("AM" & Rows.Count).End(xlUp).Offset(1).Value = cOpt.Caption
        End If
    Next cOpt
    If y > 0 Then Sheets("Workers").Range("AM" & Rows.Count).End(xlUp).Offset(1).Value = "FLEX"
    I wasn't able to test it.

  17. #17
    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: Capture data from OptionBox into datasheet

    Why don't you try placing this in the submit button macro wherever you want the option buttons to be evaluated. Test it thoroughly and let me know what you think.

    Dim cOpt As Control
    Dim x As Integer
    Dim y As Integer
    
    x = 0
    y = 0
    For Each cOpt In Me.Unit
        If cOpt.Value = True Then
            Sheets("Workers").Range("AL" & Rows.Count).End(xlUp).Offset(1).Value = cOpt.Caption
            x = x + 1
        End If
    Next cOpt
    If x > 0 Then Sheets("Workers").Range("AL" & Rows.Count).End(xlUp).Offset(1).Value = "ALL"
    For Each cOpt In Me.Shift
        If cOpt.Value = True Then
            Sheets("Workers").Range("AM" & Rows.Count).End(xlUp).Offset(1).Value = cOpt.Caption
        End If
    Next cOpt
    If y > 0 Then Sheets("Workers").Range("AM" & Rows.Count).End(xlUp).Offset(1).Value = "FLEX"
    I wasn't able to test it.

    ps The frame was the right way to go. It's more than just looks its functionality. Option buttons within a frame are evaluated differently. Only one option button in a frame can be clicked at a time, so if you didn't have the frames you couldn't select a shift and a unit at the same time.

+ 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. Return data to datasheet when checkbox in the form is checked
    By Apple Ling in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-14-2013, 02:09 AM
  2. need help to get data from datasheet on to the userform
    By shankar.nstl in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-04-2011, 06:12 AM
  3. how to update data from one datasheet to a master sheet, automatically
    By mikej2009 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 03-31-2010, 11:09 PM
  4. Replies: 2
    Last Post: 02-19-2010, 02:59 AM
  5. [SOLVED] Saving data to a form datasheet
    By University of Maine student in forum Excel General
    Replies: 0
    Last Post: 03-14-2006, 04:50 PM

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