How to update worksheet table data using userform listbox

Megg

New Member
Joined
Nov 5, 2023
Messages
11
Office Version
  1. 365
Platform
  1. Windows
I have a worksheet ("Breakdown") that consists of a table ("breakdownTable"). I have a button that opens a userform called "SearchForm" that displays a listbox. So far I have it so when I double click a selected item from this list box a second userform called "EditForm" displays that automatically inputs the details of the selected item into multiple textboxes.
I'm having trouble with getting the update button in the Editform to update both the listbox and the worksheet table. I've managed to get it to update the listbox but it won't do the same for the table.

This is what I have for the SearchForm Code:
VBA Code:
Private Sub playerList_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
With EditForm
.TextBox1.text = Me.playerList.Column(1)
.TextBox2.text = Me.playerList.Column(0)
.TextBox3.text = Me.playerList.Column(2)
.TextBox4.text = Me.playerList.Column(3)
.TextBox5.text = Me.playerList.Column(4)
.TextBox6.text = Me.playerList.Column(6)
.TextBox7.text = Me.playerList.Column(7)

End With

EditForm.Show

End Sub

Private Sub UserForm_Initialize()
    
    playerList.List = Worksheets("Breakdown").ListObjects("breakdownTable").DataBodyRange.Value2
    playerList.ColumnCount = 9
    playerList.ColumnHeads = False
  
End Sub

And this is what I have for the EditForm:
Code:
Private Sub CommandButton1_Click()
Dim DBS As Range
Dim IND, X As Integer
Set wso = ThisWorkbook.Worksheets("Breakdown")

With SearchForm.playerList

.Column(1) = Me.TextBox1.text
.Column(0) = Me.TextBox2.text
.Column(2) = Me.TextBox3.text
.Column(3) = Me.TextBox4.text
.Column(4) = Me.TextBox5.text
.Column(6) = Me.TextBox6.text
.Column(7) = Me.TextBox7.text

Set DBS = wso.ListObjects("breakdownTable").DataBodyRange.End(x1Up)
For X = 1 To 9
DBS.Offset(1, X - 1) = .Column(X - 1)

IND = .ListIndex + 1
wso.Cells(IND, X) = .Column(X - 1)

Next X
Unload Me
End With

End Sub

I'm quite new to excel vba so any assistance is appreciated. I've tried to follow tutorials and looked at other forums but I'm just not quite understanding. I know it's something to do with the Set DBS and the rest below it in the EditForm code. The error I get is '1004' - Application-defined or object-defined error.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi welcome to Forum

Generally speaking, you seldom need to create a separate userform to perform an edit function as this can normally be done from the main userform

However & sticking with what you are trying to do, see if these updates to your codes will do what you want

Make a backup of your workbook & then delete any relevant codes you have for searching & updating in your userforms code pages.

Place following codes in your SearchForm code page

Code:
Dim tblbreakdownTable    As ListObject
Public RecordRow         As Long

Private Sub playerList_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
   EditForm.UpdateRecord Me, tblbreakdownTable
End Sub

Private Sub UserForm_Initialize()

    Set tblbreakdownTable = ThisWorkbook.Worksheets("Breakdown").ListObjects("breakdownTable")
   
    With playerList
        .List = tblbreakdownTable.DataBodyRange.Value2
        .ColumnCount = 9
        .ColumnHeads = False
    End With

End Sub

Note the variables at the top - these MUST be placed at very TOP of your Userforms code page OUTSIDE any procedures.

Place following codes in your EditForm code page

Code:
Private Sub cmdUpdate_Click()
    Me.Hide
End Sub

Private Sub cmdCancel_Click()
    cmdCancel.Tag = vbCancel
    Me.Hide
End Sub

Sub UpdateRecord(ByVal Form As Object, ByVal objTable As Object)
    Dim rngData     As Range
    Dim i           As Long
   
    With Form.playerList
        For i = 1 To 7
            Me.Controls("TextBox" & i).Text = .Column(Choose(i, 1, 0, 2, 3, 4, 6, 7))
        Next i
        Form.RecordRow = .ListIndex + 1
    End With
   
    Me.Show
   
    If Not Val(Me.cmdCancel.Tag) = vbCancel Then
       
        For i = 1 To 7
            'post record to table
            With Me.Controls("TextBox" & i)
                objTable.DataBodyRange.Cells(Form.RecordRow, Choose(i, 2, 1, 3, 4, 5, 7, 8)).Value = .Value
            End With
        Next i
       
        Set rngData = objTable.DataBodyRange
       
        'update listbox
        With Form.playerList
            .Clear
            .List = rngData.Value2:  .ListIndex = Form.RecordRow - 1
        End With
        'inform user
        MsgBox "Record Updated", 64, "Success"
    End If
   
    Unload Me
   
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Cancel = CloseMode = 0
    If Cancel Then Call cmdCancel_Click
End Sub

Note I have included two commandbuttons on this useform which should be named as follows

- cmdUpdate
- cmdCancel

I have only lightly tested & solution may need some adjustment but hopefully, updates will do what you want

Hope Helpful

Dave
 
Upvote 1
Solution
That worked perfectly! Very helpful, thank you so much! I really appreciate it :)
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top