+ Reply to Thread
Results 1 to 7 of 7

Populate Userform with values from table

Hybrid View

LKERN Populate Userform with values... 03-10-2020, 06:37 PM
mehmetcik Re: Populate Userform with... 03-11-2020, 07:29 AM
LKERN Re: Populate Userform with... 03-11-2020, 10:03 AM
mehmetcik Re: Populate Userform with... 03-11-2020, 10:33 AM
LKERN Re: Populate Userform with... 03-11-2020, 01:16 PM
LKERN Re: Populate Userform with... 03-12-2020, 08:41 AM
LKERN Re: Populate Userform with... 03-12-2020, 09:35 AM
  1. #1
    Forum Contributor
    Join Date
    11-08-2017
    Location
    Murfreesboro, TN
    MS-Off Ver
    MS 365
    Posts
    168

    Populate Userform with values from table

    Hi. I have a sample spreadsheet with userforms that I need help with.

    Userforms:
    ContactSearch
    ContactEdit

    ContactSearch:
    There is a button called "Open Contact" that currently navigates to the record in the table using the following code:

    Private Sub cmdGoToContact_Click()
    
    Dim X
    
        On Error GoTo booboo
        X = Split(ListContactSearch.Column(4), "'!")
        Application.Goto Sheets(X(0)).Range(X(1)), True
        Unload Me
        Exit Sub
        
    booboo:           MsgBox "Oops! Please click on a contact in the list, then click Go To Contact."
        
    End Sub
    I want it to open the "ContactEdit" form and populate the fields with the items from the table. How do I change the code to make that happen?

    Then, I want to re-save the changes to the same line in the table. I copied the "ContactAdd" form, so the "CmdSaveChanges_Click" code puts the changed information on the next open line instead of on the same line it came from. How do I change the code to do what I want? (I didn't input all of it here since it is so many lines. All of the lines are basically the same, so here is a snipet)

    Private Sub CmdSaveChanges_Click()
    
    Dim rng As Range
    Dim LastRow As Long
    
    Set rng = ActiveSheet.ListObjects("ContactCenter").Range
    
    LastRow = rng.Find("*", rng.Cells(1), xlFormulas, xlPart, xlByRows, xlPrevious, False).Row
    
    rng.Parent.Cells(LastRow + 1, 3).Value = TextBox1.Value
    
    Dim ctl As Control
    
        For Each ctl In Me.Controls
            Select Case TypeName(ctl)
                Case "TextBox"
                    ctl.Value = ""
                Case "ComboBox", "ListBox"
                    ctl.ListIndex = -1
            End Select
        Next ctl
    
    
    End Sub
    Attached Files Attached Files

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

    Re: Populate Userform with values from table

    Hi

    I have done the following for you:

    Created a Global variable "Entry"

    Added an additional Column in your Named Range ContactSearchResults this Column Store the Row Number of the Entries.

    Added an additional Column to your listbox on your search screen. Column Width is set to 0 so it is hidden

    Selecting an Entry in the List box sets the value of Entry to the Row Number of the Entry. Entry = Listbox.value

    I have added the code to your Edit Userform to load the data from the selected Entry.

    I have also added the code to save your edited Userform.

    This code is messy but is in line with your existing code:

    My normal Code to load the Userform requires each Control be numbered to match the column where its data is.
    Textbox10's data is in column 10, Combobox11's data is in column 11 etc.

    
    On Error Resume Next
    'CF is the flag to stop macros running when a Control is modified.  Example Below
    CF = True
    
    For Count =1 To 70
          Me.Controls("CheckBox" & Count).Value = LoadArray(Count)
          Me.Controls("ComboBox" & Count).Value = LoadArray(Count)
          Me.Controls("ListBox" & Count).Value = LoadArray(Count)
          Me.Controls("Label" & Count).Caption = LoadArray(Count)
          Me.Controls("TextBox" & Count).Value = LoadArray(Count)
          Me.Controls("SpinButton" & Count).Value = LoadArray(Count)
    Next
    
    CF = False
    On Error GoTo 0
    
    
    
    Private Sub ComboBox2_Change()
    If CF = True Then Exit Sub
    TextBox2.Value = ComboBox2.Value
    End Sub
    
    Private Sub UserForm_Activate()
    If Entry < 9 Then Exit Sub
     TextBox1.Value = Cells(Entry, 3).Value
     TextBox2.Value = Cells(Entry, 4).Value
     TextBox3.Value = Cells(Entry, 6).Value
     ComboBox1.Value = Cells(Entry, 7).Value
     ComboBox2.Value = Cells(Entry, 8).Value
     ComboBox3.Value = Cells(Entry, 9).Value
     TextBox4.Value = Cells(Entry, 10).Value
     ComboBox4.Value = Cells(Entry, 11).Value
     ComboBox5.Value = Cells(Entry, 12).Value
     ComboBox6.Value = Cells(Entry, 13).Value
     TextBox5.Value = Cells(Entry, 14).Value
     TextBox7.Value = Cells(Entry, 15).Value
     TextBox9.Value = Cells(Entry, 16).Value
     TextBox11.Value = Cells(Entry, 17).Value
     TextBox13.Value = Cells(Entry, 18).Value
     TextBox15.Value = Cells(Entry, 19).Value
     TextBox6.Value = Cells(Entry, 20).Value
     TextBox8.Value = Cells(Entry, 21).Value
     TextBox10.Value = Cells(Entry, 22).Value
     TextBox12.Value = Cells(Entry, 23).Value
     TextBox14.Value = Cells(Entry, 24).Value
     TextBox16.Value = Cells(Entry, 25).Value
     TextBox17.Value = Cells(Entry, 26).Value
     ComboBox7.Value = Cells(Entry, 27).Value
     TextBox18.Value = Cells(Entry, 28).Value
     ComboBox8.Value = Cells(Entry, 29).Value
     TextBox19.Value = Cells(Entry, 30).Value
     ComboBox9.Value = Cells(Entry, 31).Value
     TextBox20.Value = Cells(Entry, 32).Value
     ComboBox10.Value = Cells(Entry, 33).Value
     TextBox21.Value = Cells(Entry, 34).Value
     TextBox22.Value = Cells(Entry, 35).Value
     TextBox23.Value = Cells(Entry, 36).Value
     TextBox24.Value = Cells(Entry, 37).Value
     TextBox25.Value = Cells(Entry, 38).Value
     TextBox27.Value = Cells(Entry, 39).Value
     TextBox26.Value = Cells(Entry, 40).Value
     TextBox28.Value = Cells(Entry, 41).Value
     TextBox38.Value = Cells(Entry, 42).Value
     TextBox29.Value = Cells(Entry, 43).Value
     TextBox39.Value = Cells(Entry, 44).Value
     TextBox30.Value = Cells(Entry, 45).Value
     TextBox40.Value = Cells(Entry, 46).Value
     TextBox31.Value = Cells(Entry, 47).Value
     TextBox41.Value = Cells(Entry, 48).Value
     TextBox32.Value = Cells(Entry, 49).Value
     TextBox42.Value = Cells(Entry, 50).Value
     TextBox33.Value = Cells(Entry, 51).Value
     TextBox43.Value = Cells(Entry, 52).Value
     TextBox34.Value = Cells(Entry, 53).Value
     TextBox44.Value = Cells(Entry, 54).Value
     TextBox35.Value = Cells(Entry, 55).Value
     TextBox45.Value = Cells(Entry, 56).Value
     TextBox36.Value = Cells(Entry, 57).Value
     TextBox46.Value = Cells(Entry, 58).Value
     TextBox37.Value = Cells(Entry, 59).Value
     TextBox47.Value = Cells(Entry, 60).Value
     ComboBox11.Value = Cells(Entry, 61).Value
     ComboBox14.Value = Cells(Entry, 62).Value
     ComboBox17.Value = Cells(Entry, 63).Value
     ComboBox13.Value = Cells(Entry, 64).Value
     ComboBox15.Value = Cells(Entry, 65).Value
     ComboBox18.Value = Cells(Entry, 66).Value
     ComboBox12.Value = Cells(Entry, 67).Value
     ComboBox16.Value = Cells(Entry, 68).Value
     ComboBox19.Value = Cells(Entry, 69).Value
    Attached Files Attached Files
    Last edited by mehmetcik; 03-11-2020 at 08:19 AM. Reason: Save Edited Userform corrected.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Forum Contributor
    Join Date
    11-08-2017
    Location
    Murfreesboro, TN
    MS-Off Ver
    MS 365
    Posts
    168

    Re: Populate Userform with values from table

    Thank you mehmetcik. This is very helpful.

    All of that works, but when I click the "Save Changes" button on the ContactEdit userform, it still adds a new row to the table instead of overwriting the existing row.

    How do I change this code to make it overwrite? I know it has to do with the LastRow portion, I just don't know what to change that to. (I am only posting the first part of the code since the bottom part is just more of the same.)

    Private Sub CmdSaveChanges_Click()
    
    Dim rng As Range
    Dim LastRow As Long
    
    Set rng = ActiveSheet.ListObjects("ContactCenter").Range
    
    LastRow = rng.Find("*", rng.Cells(1), xlFormulas, xlPart, xlByRows, xlPrevious, False).Row
    
    Cells(LastRow + 1, 3).Value = TextBox1.Value
    Cells(LastRow + 1, 4).Value = TextBox2.Value
    ' repeat with rest of the lines
    
    End Sub
    Also, I have this code to clear the form, but it does not clear everything. I found this on another site. How can I change this to completely clear the form?

    
    Dim ctl As Control
    
        For Each ctl In Me.Controls
            Select Case TypeName(ctl)
                Case "TextBox"
                    ctl.Value = ""
                Case "ComboBox", "ListBox"
                    ctl.ListIndex = -1
            End Select
        Next ctl
    I really appreciate your help.

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

    Re: Populate Userform with values from table

    Sorry.

    I updated that afterwards. Upload a new sample from Post Number 2.

    Try this:
    
        For Each ctl In Me.Controls
    
    On Error Resume Next
            Select Case TypeName(ctl)
                Case "TextBox"
                    ctl.Value = ""
                Case "ComboBox", "ListBox"
                    ctl.ListIndex = -1
                    ctl.value =""
            End Select
        Next ctl
    On Error Goto 0
    Alternatively:

    Create a Change Flag. Set Your Emtry to an empty row and load a blank entry.

    Global CF as Boolean

    
    Sub ClearScreen()
    CF = True
    Entry = Cells(rows.count,1).End(xlUp).Row+2
    LoadEnty
    End Sub
    
    Sub LoadEntry()
    On Error Resume Next
    'CF is the flag to stop macros running when a Control is modified.  Example Below
    CF = True
    
    LoadArray = Range((Entry, "A"),(Entry, "BQ")).value
    
    For Count =3 To 69
          Me.Controls("CheckBox" & Count).Value = LoadArray(Count)
          Me.Controls("ComboBox" & Count).Value = LoadArray(Count)
          Me.Controls("ListBox" & Count).Value = LoadArray(Count)
          Me.Controls("Label" & Count).Caption = LoadArray(Count)
          Me.Controls("TextBox" & Count).Value = LoadArray(Count)
          Me.Controls("SpinButton" & Count).Value = LoadArray(Count)
    Next
    
    CF = False
    On Error GoTo 0
    End Sub


    '*************************************************************************************************
    Last edited by mehmetcik; 03-11-2020 at 01:38 PM.

  5. #5
    Forum Contributor
    Join Date
    11-08-2017
    Location
    Murfreesboro, TN
    MS-Off Ver
    MS 365
    Posts
    168

    Re: Populate Userform with values from table

    Thank you! This works great. I appreciate your help.

  6. #6
    Forum Contributor
    Join Date
    11-08-2017
    Location
    Murfreesboro, TN
    MS-Off Ver
    MS 365
    Posts
    168

    Re: Populate Userform with values from table

    Sorry to open this back up, but I am having trouble with the changes in my actual spreadsheet. The changes in Sample2.xlsm above work great in the sample, but it crashes Excel in my actual spreadsheet every time I click to add a contact to the table, from both the ContactAdd and the ContactEdit userforms.

    I suspect it has something to do with the Global Variable "Entry", but not sure. Is there another way to accomplish the copying without that variable?

  7. #7
    Forum Contributor
    Join Date
    11-08-2017
    Location
    Murfreesboro, TN
    MS-Off Ver
    MS 365
    Posts
    168

    Re: Populate Userform with values from table

    Nevermind! I figured it out. User error. Shocking, I know.

+ 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. [SOLVED] Populate UserForm List with values from an Array
    By plans in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-22-2019, 10:15 AM
  2. Populate new row in table from a form (not userform) on another worksheet
    By joeystraw in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 12-01-2018, 10:59 AM
  3. [SOLVED] populate userform with data from pivot table
    By Mathijs3 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-09-2016, 10:30 AM
  4. Populate userform from row values based on combobox
    By robertogavilan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-20-2014, 06:32 PM
  5. VLookup Excel table to populate combobox in userform
    By trecie13 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-14-2013, 02:17 PM
  6. Replies: 1
    Last Post: 11-12-2011, 05:38 PM
  7. Populate Excel table with userform data
    By waspandbee in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-21-2010, 09:34 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