Search code examples
excelvbauserform

How to update the selected row in the listbox?


Hello so I'm trying to develop a UserForm and it's my first time.

UI

I'm having a problem with my ComboBox and ListBox (pretty much not functioning well).

You see, I have a ComboBox right above the ListBox and that's where I would choose a value and the row values should be displayed BOTH on the ListBox and the ComboBoxes.

Problem 1: The values only show in the ComboBoxes BUT NOT in the ListBox.

If I clicked on the "VIEW DATA" button, all the values from the spreadsheet are displayed on the ListBox with 8 columns. If clicked on a row from the ListBox (obviously it will be highlighted) it will also be displayed on the ComboBoxes below.

Problem 2: Whenever I try to edit the values in the ComboBox and click the update button, nothing happens. It doesn't update anything.

I'm looking for answers everywhere but I can't seem to find one.

     Private Sub btnDelete_Click()

        Dim a As Integer

            If MsgBox("Are you sure you want to delete this row?", vbYesNo + vbQuestion, "Yes") = vbYes Then

                For a = 1 To Range("A100000").End(xlUp).Row
                    If Cells(a, 1) = listHeader.List(listHeader.ListIndex) Then
                    Rows(a).Select
                    Selection.Delete
                End If
            Next a
        End If

    End Sub

Private Sub btnSearch_Click()

'IM THINKING ABOUT REMOVING THE SEARCH BUTTON BECAUSE THE COMBOBOX ITSELF CAN BE USED FOR SEARCHING THE ROW
'IT MAKES THE SEARCH BUTTON USELESS

'Dim x As Long
'Dim y As Long

'x = Sheets("PRESTAGE DB").Range("A" & Rows.Count).End(xlUp).Row
'For y = 2 To x

'If Sheets("PRESTAGE DB").Cells(y, 1).Text = cmbSearch.Value Then
    'cmbSchema.Text = Sheets("PRESTAGE DB").Cells(y, 1)
    'cmbEnvironment.Text = Sheets("PRESTAGE DB").Cells(y, 2)
    'cmbHost.Text = Sheets("PRESTAGE DB").Cells(y, 3)
    'cmbIP.Text = Sheets("PRESTAGE DB").Cells(y, 4)
    'cmbAccessible.Text = Sheets("PRESTAGE DB").Cells(y, 5)
    'cmbLast.Text = Sheets("PRESTAGE DB").Cells(y, 6)
    'cmbConfirmation.Text = Sheets("PRESTAGE DB").Cells(y, 7)
    'cmbProjects.Text = Sheets("PRESTAGE DB").Cells(y, 8)

'End If
'Next y

End Sub

    Private Sub btnView_Click()

        listHeader.RowSource = "A4:H200"

    End Sub

Private Sub cmbAdd_Click()
    Dim sheet As Worksheet
    Set sheet = ThisWorkbook.Sheets("PRESTAGE DB")

    nextrow = sheet.Cells(Rows.Count, 1).End(xlUp).Row + 1

    sheet.Cells(nextrow, 1) = Me.cmbSchema
    sheet.Cells(nextrow, 2) = Me.cmbEnvironment
    sheet.Cells(nextrow, 3) = Me.cmbHost
    sheet.Cells(nextrow, 4) = Me.cmbIP
    sheet.Cells(nextrow, 5) = Me.cmbAccessible
    sheet.Cells(nextrow, 6) = Me.cmbLast
    sheet.Cells(nextrow, 7) = Me.cmbConfirmation
    sheet.Cells(nextrow, 8) = Me.cmbProjects

    MsgBox "Data Added!"

End Sub

Private Sub cmbClearFields_Click()

    cmbSchema.Text = ""
    cmbEnvironment.Text = ""
    cmbHost.Text = ""
    cmbIP.Text = ""
    cmbAccessible.Text = ""
    cmbLast.Text = ""
    cmbConfirmation.Text = ""
    cmbProjects.Text = ""
    cmbSearch.Text = ""

End Sub

Private Sub cmbSearch_Change()

Dim x As Long
Dim y As Long

x = Sheets("PRESTAGE DB").Range("A" & Rows.Count).End(xlUp).Row
For y = 2 To x

If Sheets("PRESTAGE DB").Cells(y, 1).Text = cmbSearch.Value Then
    cmbSchema.Text = Sheets("PRESTAGE DB").Cells(y, 1)
    cmbEnvironment.Text = Sheets("PRESTAGE DB").Cells(y, 2)
    cmbHost.Text = Sheets("PRESTAGE DB").Cells(y, 3)
    cmbIP.Text = Sheets("PRESTAGE DB").Cells(y, 4)
    cmbAccessible.Text = Sheets("PRESTAGE DB").Cells(y, 5)
    cmbLast.Text = Sheets("PRESTAGE DB").Cells(y, 6)
    cmbConfirmation.Text = Sheets("PRESTAGE DB").Cells(y, 7)
    cmbProjects.Text = Sheets("PRESTAGE DB").Cells(y, 8)

End If
Next y

End Sub

    Private Sub cmbUpdate_Click()

    Dim x As Long
    Dim y As Long

    x = Sheets("PRESTAGE DB").Range("A" & Rows.Count).End(xlUp).Row
    For y = 2 To x
    If Sheets("PRESTAGE DB").Cells(y, 1).Text = cmbSchema.Value Then
    Sheets("PRESTAGE DB").Cells(y, 2) = cmbEnvironment
    Sheets("PRESTAGE DB").Cells(y, 3) = cmbHost
    Sheets("PRESTAGE DB").Cells(y, 4) = cmbIP
    Sheets("PRESTAGE DB").Cells(y, 5) = cmbAccessible
    Sheets("PRESTAGE DB").Cells(y, 6) = cmbLast
    Sheets("PRESTAGE DB").Cells(y, 7) = cmbConfirmation
    Sheets("PRESTAGE DB").Cells(y, 8) = cmbProjects

    End If
    Next y

    End Sub

    Private Sub CommandButton5_Click()
        listHeader.RowSource = ""

    End Sub


Private Sub CommandButton7_Click()

End Sub

    Private Sub listHeader_Click()

        cmbSchema.Value = UserForm1.listHeader.Column(0)
        cmbEnvironment.Value = UserForm1.listHeader.Column(1)
        cmbHost.Value = UserForm1.listHeader.Column(2)
        cmbIP.Value = UserForm1.listHeader.Column(3)
        cmbAccessible.Value = UserForm1.listHeader.Column(4)
        cmbLast.Value = UserForm1.listHeader.Column(5)
        cmbConfirmation.Value = UserForm1.listHeader.Column(6)
        cmbProjects.Value = UserForm1.listHeader.Column(7)

    End Sub


    Private Sub UserForm_Initialize()

        cmbSearch.List = Sheets("PRESTAGE DB").Range("A4:A10000").Value

    End Sub

file: https://jmp.sh/cZj8e4q


Solution

  • Use the below code for Problem1

    Used Change event for combobox so if user changes any value the list box and combo box will be populated based on the value selected in the combo box.

    Private Sub ComboBox1_Change()
        x = Sheets("PRESTAGEDB").Range("A" & Rows.Count).End(xlUp).Row
        For y = 2 To x
            If Sheets("PRESTAGEDB").Cells(y, 1).Text = ComboBox1.Value Then
                cmbSchema.Text = Sheets("PRESTAGEDB").Cells(y, 1)
                cmbEnvironment.Text = Sheets("PRESTAGEDB").Cells(y, 2)
                cmbHost.Text = Sheets("PRESTAGEDB").Cells(y, 3)
                cmbIP.Text = Sheets("PRESTAGEDB").Cells(y, 4)
                cmbAccessible.Text = Sheets("PRESTAGEDB").Cells(y, 5)
                cmbLast.Text = Sheets("PRESTAGEDB").Cells(y, 6)
                cmbConfirmation.Text = Sheets("PRESTAGEDB").Cells(y, 7)
                cmbProjects.Text = Sheets("PRESTAGEDB").Cells(y, 8)
    
                UserForm1.ListBox1.RowSource = "A" + CStr(y) + ": H" + CStr(y)
                Exit For
            End If
        Next y
    End Sub
    

    Please accept if resolves your issue