+ Reply to Thread
Results 1 to 5 of 5

Populate userform radio (option) buttons based on data in worksheet

Hybrid View

d.sanchez Populate userform radio... 10-24-2013, 12:57 AM
AlphaFrog Re: Populate userform radio... 10-24-2013, 01:28 AM
d.sanchez Re: Populate userform radio... 10-24-2013, 01:58 AM
AlphaFrog Re: Populate userform radio... 10-24-2013, 02:15 AM
d.sanchez Re: Populate userform radio... 10-24-2013, 01:21 PM
  1. #1
    Registered User
    Join Date
    08-16-2012
    Location
    Calgary, Alberta
    MS-Off Ver
    Office2011 (Mac)
    Posts
    16

    Populate userform radio (option) buttons based on data in worksheet

    Hi everybody.

    I have a worksheet with row entries of client profile information. In order to protect the data from user error, a Userform is used to edit the client's contact information. By clicking on a button, "Edit Client", an Input box is loaded asking the user what client ID# they wish to edit. This number input is put into a textbox on the userform, and all the textboxes and radio buttons on the userform are populated by referencing the Row# that the Client ID# appears on in the worksheet.

    Currently, all the textboxes are populating perfectly. However, the radio button selections are not populating correctly. When the userform initializes, the radio buttons will not populate. BUT... BUT... I have a command to re-initialize the userform if the Client ID# is changed on the userform, and if that is triggered the radio buttons populate correctly.

    I have been scratching my head why and I can't see what the problem is. So, I am reaching out to see if anybody else has had this problem, or can offer a solution.

    Anybody have any insight?

    Code:

    Sub EditClientForm()
    
    Dim ws As Worksheet
    
    '/ set the worksheet as the sheet that contains the target database
    Set ws = Sheet4
    
    '/ Activate the worksheet so the user can see what client they wish to select when the Inputbox appears
    ws.Activate
    
    ClientSelection = InputBox("What Client do you want to edit?" & Chr(10) & "(1, 2, 3, etc)", "Select a client to edit", "Enter CLIENT ID # here")
    
    If ClientSelection = "" Then Exit Sub
    
    '/ The following puts the user's input into a textbook on the userform, 
    '/ which will in turn target a line entry in the worksheet, and thereby control the population of the textbooks and radio buttons
    
    EditClient.tbClientNumber.Value = ClientSelection  
    
    EditClient.Show       '/ This initializes the userform
    
    End Sub
    
    
    
    
    Private Sub UserForm_Initialize()
    
    	'/ THIS POPULATES THE USERFORM WITH THE EXISTING CLIENT INFO, AS APPEARS IN THE WORKSHEET
    
    	'/ DECLARATIONS:
    Dim ws As Worksheet
    	'/ set the worksheet as the sheet that contains the target database
    Set ws = Sheet4
    
    ClientNumber = tbClientNumber.Value
    
    	'/ Search the sheet (Column A) to find the row that contains the target client number
    Dim SearchRange As Range
    Dim FindRow As Range
    Set SearchRange = ws.Range("A1", ws.Range("A65536").End(xlUp))
    Set FindRow = SearchRange.Find(ClientNumber, LookIn:=xlValues, lookat:=xlWhole)
    EnterRow = FindRow.Row
    
    	'/ Enter the information from the Worksheet into the Userform
    
    tbCompanyName = ws.Range("B" & EnterRow).Value
    
    	'/ Prefix Options
    If ws.Cells(EnterRow, 3).Value = "Mr." Then
        btnMr.Value = True
        Else
        btnMr.Value = False
    End If
    If ws.Cells(EnterRow, 3).Value = "Mrs." Then
        btnMrs.Value = True
        Else
        btnMrs.Value = False
    End If
    If ws.Cells(EnterRow, 3).Value = "Ms." Then
        btnMs.Value = True
        Else
        btnMs.Value = False
    End If
    If ws.Cells(EnterRow, 3).Value = "Dr." Then
        btnDr.Value = True
        Else
        btnDr.Value = False
    End If
    
    
    	'/ Property Owner, Company Rep's Name
    tbFirstName = ws.Range("D" & EnterRow).Value
    tbLastName = ws.Range("E" & EnterRow).Value
    
    	'/ Domestic Address
    tbStreetAddress1 = ws.Range("F" & EnterRow).Value
    tbCityTown1 = ws.Range("G" & EnterRow).Value
    tbProvince1 = ws.Range("H" & EnterRow).Value
    tbPostalCode1 = ws.Range("I" & EnterRow).Value
    tbHomePhone1 = ws.Range("J" & EnterRow).Value
    tbWorkPhone1 = ws.Range("K" & EnterRow).Value
    tbCellPhone1 = ws.Range("L" & EnterRow).Value
    tbEmail1 = ws.Range("M" & EnterRow).Value
    
    End Sub

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Populate userform radio (option) buttons based on data in worksheet

    I'm assuming the Prefix buttons are the option buttons in question?

    You would only have to set one option button to true. The others will automatically set to false as is the nature of grouped option buttons.

    Try something like this.

    The LCase function makes it case insensitive e.g., Mr. = MR.

    If you're missing a period, it's not a match e.g., Mr <> Mr.

        '/ Prefix Options
        Select Case LCase(ws.Cells(EnterRow, 3).Value)
            Case LCase("Mr."): btnMr.Value = True
            Case LCase("Mrs."): btnMrs.Value = True
            Case LCase("Ms."): btnMs.Value = True
            Case LCase("Dr."): btnDr.Value = True
        End Select
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    08-16-2012
    Location
    Calgary, Alberta
    MS-Off Ver
    Office2011 (Mac)
    Posts
    16

    Re: Populate userform radio (option) buttons based on data in worksheet

    Ah, brilliant. Select Case, a much better approach! Thank you for the insight.

    I will try this in the morning and update the thread with a result and comment.

    Much appreciated!

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Populate userform radio (option) buttons based on data in worksheet

    This is a little more robust. The period is now optional e.g., Mr = Mr.

        '/ Prefix Options
        Select Case Trim(LCase(Replace(ws.Cells(EnterRow, 3).Value, ".", "")))
            Case "mr": btnMr.Value = True
            Case "mrs": btnMrs.Value = True
            Case "ms": btnMs.Value = True
            Case "dr": btnDr.Value = True
        End Select

  5. #5
    Registered User
    Join Date
    08-16-2012
    Location
    Calgary, Alberta
    MS-Off Ver
    Office2011 (Mac)
    Posts
    16

    Re: Populate userform radio (option) buttons based on data in worksheet

    Worked perfectly. I will apply this technique to other userforms as well. Thank you very much!!

+ 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. userform with 6 option buttons to populate 1 listbox
    By simeonmein in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-23-2013, 05:18 PM
  2. How can I insert multiple sets of option buttons (radio buttons) in Excel 2010?
    By mickwooduclan in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-25-2013, 10:01 AM
  3. Excel 2007 : Help with option/radio buttons
    By Twilock in forum Excel General
    Replies: 0
    Last Post: 07-12-2012, 09:14 PM
  4. Evaluation with Radio buttons and Option buttons
    By soph0101 in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 11-15-2010, 12:23 PM
  5. Grouping radio option buttons
    By ahmed in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-01-2006, 06:15 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