【问题标题】:Listbox search and delete列表框搜索和删除
【发布时间】:2022-01-23 15:43:31
【问题描述】:

我的数据表:
工作表(“数据库”) - A1 到 L10000(条目)
A1到L1中每一列的标题

我尝试使用用户表单搜索工作表。我使用了 ComboBox,因为总是有相同的物质(25 种不同的物质)。

对于输出,我使用了一个列表框。

我想从我的工作表(“数据库”)中的搜索功能中删除选定的输出。

例如
搜索“物质 1”[ComboBox]

搜索后:

列表框
列头为空(搜索后如何显示列头?)
第一行:物质 1
第二行:物质 1
...

我选择第二行并按下“删除”按钮。
现在它会删除一个不同的条目而不是选定的条目。

如何删除包含所选信息的正确行?

VBA Excel 代码:

列表框 - 数据库表条目

Private Sub Refresh_data()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Database")

Dim lr As Long
lr = Sheets("Database").Range("A" & Rows.Count).End(xlUp).Row

If lr = 1 Then lr = 2 
    With Me.ListBox1
        .ColumnCount = 13
        .ColumnHeads = True
        .RowSource = "Database!A2:L" & lr
    End With
End Sub

在工作表“数据库”中搜索

Private Sub CommandButton7_Click()  'Search Button

Dim sh1 As Worksheet
Dim row As Long

Worksheets("Database").Activate

UserForm2.ListBox1.RowSource = "" 

Set sh1 = ThisWorkbook.Sheets("Database")

For row = 2 To sh1.Cells(Rows.Count, 2).End(xlUp).Row

    If InStr(1, sh1.Cells(row, 2).Value, Me.ComboBox.Value) <> 0 Then

        Me.ListBox1.AddItem sh1.Cells(row, 1).Value                               
        Me.ListBox1.List(Me.ListBox1.ListCount - 1, 1) = sh1.Cells(row, 2).Value  
        Me.ListBox1.List(Me.ListBox1.ListCount - 1, 2) = sh1.Cells(row, 3).Value  
        Me.ListBox1.List(Me.ListBox1.ListCount - 1, 3) = sh1.Cells(row, 4).Value  
        Me.ListBox1.List(Me.ListBox1.ListCount - 1, 4) = sh1.Cells(row, 5).Value  
        Me.ListBox1.List(Me.ListBox1.ListCount - 1, 5) = sh1.Cells(row, 6).Value 
        Me.ListBox1.List(Me.ListBox1.ListCount - 1, 6) = sh1.Cells(row, 7).Value  
        Me.ListBox1.List(Me.ListBox1.ListCount - 1, 7) = sh1.Cells(row, 8).Value 
        Me.ListBox1.List(Me.ListBox1.ListCount - 1, 8) = sh1.Cells(row, 9).Value  
        Me.ListBox1.List(Me.ListBox1.ListCount - 1, 9) = sh1.Cells(row, 10).Value 

    End If

Next Zeile

End Sub

删除条目

Private Sub CommandButton6_Click() 'Delete Button

Worksheets("Database").Activate

Dim a As String
Dim i As Long

For i = 0 To ListBox1.ListCount - 1             
    If ListBox1.Selected(i) = True Then         
    Rows(i + 2).EntireRow.Delete            
    End If
Next

Call Refresh_data 

End Sub

【问题讨论】:

  • 由于您有选择地加载列表框,它的索引不再对应于工作表行。您的代码还应该将行号放在列表中,并在尝试删除时使用它。您的代码仅加载 B:B 列中代码找到 Combo 值的行。这不是您在加载组合时想要/需要的吗?
  • 感谢 FaneDure 的快速回答,但我该怎么做呢?
  • 我将 ComboBox(CB) 与 B 列进行比较,因为 B 列中列出的所有物质。搜索后,我找到了我用 CB 选择的物质。例如“combobox=sub1”,我找到了所有 sub1 物质。但是我无法删除存储信息的正确行。我使用了 CB,因为它始终是相同的 25 种物质,而且更容易搜索,因为您只需要选择物质而不需要小费。其他列包含有关物质的信息,如测量日期等。所以我想删除包含所有信息的整行。
  • B:B 列中“sub1”的出现次数是否更多?您要删除工作表上的行,还是从列表框中删除选定的行?如果出现更多并且想要删除工作表行,您会决定删除哪一行,查看所选行上的其他参数吗?
  • 是的,B:B 中有更多的 sub1。它是实验室的数据库。我想删除工作表上的行以及列表框中的行。其他参数是关于批次、测量日期、校准斜率的信息。我有更多的 sub 1,因为在我更改测量机器(LC/MS)上的某些东西后,我必须校准 sub1。其他参数也显示在列表框中,帮助我决定是否要从工作表中删除数据。例如,我在测量日期 22.01.22 的物质信息有误,因此我可以将其从数据库中删除。

标签: excel vba search listbox


【解决方案1】:

将行号添加到搜索结果中。如果列表框显示完整数据,您只能从搜索结果中删除。我将结果涂成黄色以显示差异。

Option Explicit

Private Sub Refresh_data()
    Dim sh As Worksheet
    Set sh = ThisWorkbook.Sheets("Database")
    
    Dim lr As Long
    lr = Sheets("Database").Range("A" & Rows.Count).End(xlUp).row
    
     If lr = 1 Then lr = 2
     With Me.ListBox1
        .BackColor = vbWhite
        .ColumnWidths = ""
        .ColumnCount = 12
        .ColumnHeads = True
        .RowSource = "Database!A2:L" & lr
     End With
 End Sub

Private Sub CommandButton7_Click()  'Search Button

    Dim sh1 As Worksheet, c As Long, data, s As String
    Dim LastRow As Long, row As Long, i As Long
    
    Set sh1 = ThisWorkbook.Sheets("Database")
    LastRow = sh1.Cells(Rows.Count, 2).End(xlUp).row
    data = sh1.Range("A1:L" & LastRow).Value
    s = Me.ComboBox.Value
    
    With Me.ListBox1
        .RowSource = ""
        .ColumnCount = 10
        .ColumnWidths = "0" ' hide row numbers
        .BackColor = RGB(240, 240, 200)
        
        For row = 2 To LastRow
            If InStr(1, data(row, 2), s) <> 0 Then
                .AddItem row
                i = .ListCount - 1
                For c = 1 To 9
                   .List(i, c) = data(row, c)
                Next
            End If
        Next
        
    End With

End Sub

Private Sub CommandButton6_Click() 'Delete Button
       
    Dim sh1 As Worksheet, i As Long, r As Long
    Set sh1 = ThisWorkbook.Sheets("Database")
    
    With ListBox1
        ' only delete from filtered list
        If .ColumnCount > 10 Then Exit Sub
        For i = 0 To .ListCount - 1
            If .Selected(i) = True Then
                r = .List(i, 0) ' row in col 0
                sh1.Rows(r).EntireRow.Delete
            End If
        Next
    End With
    Call CommandButton7_Click

End Sub

【讨论】:

  • 芜湖,完美。非常感谢!!!!!
猜你喜欢
  • 2010-11-26
  • 1970-01-01
  • 2010-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-21
  • 2016-06-19
相关资源
最近更新 更多