How to search on worksheet by VBA Code?

前端 未结 2 568
借酒劲吻你
借酒劲吻你 2020-12-10 00:33

I have a worksheet with 2 columns \"Key\" and \"Value\". by VBA code, i want search Input_key on Key columns, if not exist, I will add new row [input-key]-[input-value]. How

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-10 01:06

    You can use following simple code with InStr command.

    Private Sub CommandButton1_Click()
    Dim RowNum As Long
    
    RowNum = 1
    
    
    Do Until Sheets("Data").Cells(RowNum, 1).Value = ""
    
    If InStr(1, Sheets("Data").Cells(RowNum, 2).Value, TextBox1.Value, vbTextCompare) > 0 Then
    On erro GoTo next1
    ListBox1.AddItem Sheets("Data").Cells(RowNum, 1).Value
    ListBox1.List(ListBox1.ListCount - 1, 1) = Sheets("Data").Cells(RowNum, 2).Value
    End If
    next1:
    RowNum = RowNum + 1
    Loop
    End Sub
    

    you can read more and download file

提交回复
热议问题