如何使用从单独工作表导入的数据删除userform中的选定列表框值

时间:2016-12-14 10:53:56

标签: excel vba excel-vba import listbox

嘿,伙计们我一直有问题,但到目前为止,我们还没有找到解决方法。

这是:

当我让用户单击按钮打开用户窗体时,它会显示一个包含员工列表的列表框(empList)。通过工作簿中名为" Employees"的工作簿中的隐藏工作表检索数据。初始化userform时。我有一个"添加"按钮和"删除"用于在列表中添加或删除员工的按钮。添加按钮似乎通过将新员工值插入列#34; A"的另一个可用打开的插槽中,将值添加到列表末尾。在我的员工工作表中,刷新列表以显示新的更改。

据我所知,我知道如何通过定位列表本身来从列表框中删除选定的值,但是如何通过从导入数据的第二个表中删除它来删除列表中的选定值?

以下是添加按钮的代码:

Private Sub btnAdd_Click()
    'Declaring variable to store new name
    Dim Employee As String
    'Variable to store the length of rows in column.
    Dim lRow As Long
      'Gather User input of name
      Employee = Application.InputBox("Please Enter an Employee Name (No Numbers)", "Employee Name")
        'Add Employee to Employees sheet and refresh list
        Sheets("Employees").Range("A1").End(xlDown).Offset(1, 0).Value = Employee
        lRow = Sheets("Employees").UsedRange.Rows(Sheets("Employees").UsedRange.Rows.Count).Row
        empList.RowSource = "Employees!A1:A" & lRow
End Sub

这是我初始化用户窗体时的代码:

Private Sub UserForm_Initialize()
    Dim lRow As Long
    lRow =Sheets("Employees").UsedRange.Rows(Sheets("Employees").UsedRange.Rows.Count).Row
    empList.RowSource = "Employees!A1:A" & lRow
End Sub

1 个答案:

答案 0 :(得分:0)

如果您未设置使用“RowSource”。这是一种不同的方法。

Option Explicit

Private Sub btnClear_Click()

'Declaring variable to store new name
Dim Employee As String
Dim wsEmployee As Excel.Worksheet
'Variable to store the length of rows in column.
Dim lRow As Long, LastRow As Long
Dim oCell As Range

empList.RowSource = ""

Set wsEmployee = Sheets("Employees")

LastRow = wsEmployee.Range("A" & Rows.Count).End(xlUp).Row

For Each oCell In wsEmployee.Range("A1:A" & LastRow)
    If oCell.Value = empList.Value Then
        oCell.EntireRow.Delete
    End If
Next oCell

lRow = wsEmployee.Range("A" & Rows.Count).End(xlUp).Row

empList.Clear

For Each oCell In wsEmployee.Range("A1:A" & lRow)
    If oCell.Value <> "" Then
        empList.AddItem oCell.Value
    End If
Next oCell

CleanUp:

Set wsEmployee = Nothing

End Sub


Private Sub UserForm_Initialize()

Dim lRow As Long
Dim oCell As Range
Dim wsEmployee As Excel.Worksheet

Set wsEmployee = Sheets("Employees")

empList.RowSource = ""

lRow = wsEmployee.Range("A" & Rows.Count).End(xlUp).Row

For Each oCell In wsEmployee.Range("A1:A" & lRow)
    If oCell.Value <> "" Then
        empList.AddItem oCell.Value
    End If
Next oCell

CleanUp:

Set wsEmployee = Nothing

End Sub


Private Sub btnAdd_Click()
'Declaring variable to store new name
Dim Employee As String
Dim oCell As Range
Dim wsEmployee As Excel.Worksheet
'Variable to store the length of rows in column.
Dim lRow As Long, LastRow As Long

Set wsEmployee = Sheets("Employees")

LastRow = wsEmployee.Range("A" & Rows.Count).End(xlUp).Row

  'Gather User input of name
Employee = Application.InputBox("Please Enter an Employee Name (No Numbers)", "Employee Name")
    'Add Employee to Employees sheet and refresh list
wsEmployee.Range("A" & LastRow + 1).Value = Employee

lRow = wsEmployee.Range("A" & Rows.Count).End(xlUp).Row

empList.Clear

    For Each oCell In wsEmployee.Range("A1:A" & lRow)
        If oCell.Value <> "" Then
            empList.AddItem oCell.Value
        End If
    Next oCell

CleanUp:

Set wsEmployee = Nothing

End Sub

Private Sub btnEdit_Click()

'Declaring variable to store new name
Dim Employee As String
Dim wsEmployee As Excel.Worksheet
'Variable to store the length of rows in column.
Dim lRow As Long, LastRow As Long
Dim oCell As Range

empList.RowSource = ""

Set wsEmployee = Sheets("Employees")

LastRow = wsEmployee.Range("A" & Rows.Count).End(xlUp).Row

For Each oCell In wsEmployee.Range("A1:A" & LastRow)
If oCell.Value = empList.Value Then
    Employee = Application.InputBox("Please Edit an Employee Name (No Numbers)", "Employee Name")
    oCell.Value = Employee
End If
Next oCell

lRow = wsEmployee.Range("A" & Rows.Count).End(xlUp).Row

empList.Clear

For Each oCell In wsEmployee.Range("A1:A" & lRow)
If oCell.Value <> "" Then
    empList.AddItem oCell.Value
End If
Next oCell

CleanUp:

Set wsEmployee = Nothing

End Sub

我希望能帮到你。我现在在工作。如果您需要进一步的帮助,请告诉我,我会添加更好的笔记。我构建并测试它。它将删除整行。如果你想删除这个名字,请告诉我。

相关问题