【问题标题】:Search numbers and words vba sc搜索数字和单词 vba sc
【发布时间】:2020-12-11 20:53:24
【问题描述】:

我是 VBA 的新手,想写一段代码,可以在 Excel 表中找到数字和单词

在这段代码中我可以搜索数字但不能搜索单词

Private Sub CommandButton5_Click()
Dim product_id As String
product_id = Trim(TextBox1.Text)
lastrow = Worksheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To lastrow
If Worksheets("sheet1").Cells(i, 1).Value = product_id Then
TextBox2.Text = Worksheets("sheet1").Cells(i, 2).Value
TextBox3.Text = Worksheets("sheet1").Cells(i, 3).Value
TextBox4.Text = Worksheets("sheet1").Cells(i, 4).Value
End If
Next


End Sub

【问题讨论】:

  • 您的代码仅搜索第一列 (A)。这应该适用于该列中的任何值(数字或字符串)。您可以检查 lastrow 变量以确保它是您所期望的:Debug.Print(lastrow)
  • 是的,此代码仅搜索第一列 (D)。我不知道如何使用 Debug.Print(lastrow)

标签: excel vba if-statement userform


【解决方案1】:

好吧,这对我有用!

Private Sub CommandButton3_Click()
Dim M As Long
Me.ListBox1.Clear
Me.ListBox1.AddItem
For a = 1 To 5
Me.ListBox1.List(0, a - 1) = Sheet1.Cells(1, a)

Next a
Me.ListBox1.Selected(0) = True

For M = 2 To Sheet1.Range("a1000").End(xlUp).Offset(1, 0).Row
For s = 1 To 5
g = Application.WorksheetFunction.CountIf(Sheet1.Range("a" & 2, "E" & M), Sheet1.Cells(M, s))
If g = 1 And LCase(Sheet1.Cells(M, s)) = LCase(Me.TextBox1) Or g = 1 And Sheet1.Cells(M, s) = Val(TextBox1) Then
Me.ListBox1.AddItem
For x = 1 To 5
Me.ListBox1.List(ListBox1.ListCount - 1, x - 1) = Sheet1.Cells(M, x)
Next x
End If
Next s
Next M
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多