使用通配符创建搜索

问题描述 投票:0回答:1

我希望能够使用通配符搜索两个搜索字段。

例如:我有一个搜索字段,我可以搜索水果。我希望能够输入“app”或“Ch”,它会为我提取结果。

我见过的大多数代码只有在你知道要搜索的内容时才有效,但我的字符串总会改变。

以下是搜索水果但与之匹配的代码。

sheet.Select
finalrow = Cells(Rows.Count, 1).End(xlUp).Row


For i = 1 To finalrow
  If IIf(fruit <> "", Cells(i, 1) = fruit, True) Then
    Range(Cells(i, 1), Cells(i, 8)).Copy
    ssheet.Range("A100").End(xlUp).Offset(1, 0).Resize(1, 8).Value = 
    Range(Cells(i, 1), Cells(i, 8)).Value

    dsheet.Select

  End If
Next i
excel vba excel-vba
1个回答
0
投票

只做一个你不需要你上一个问题的IIF。

您不需要复制行。并避免选择。

finalrow = dsheet.Cells(Rows.Count, 1).End(xlUp).Row


For i = 1 To finalrow
    If fruit = "" Or InStr(dsheet.Cells(i, 1),fruit) > 0 Then
        ssheet.Range("A100").End(xlUp).Offset(1, 0).Resize(1, 8).Value = dsheet.Range(dsheet.Cells(i, 1), dsheet.Cells(i, 8)).Value
    End If
Next i

如果你想要IIF,因为你为这个问题简化了它:

If IIf(fruit <> "", InStr(dsheet.Cells(i, 1),fruit) > 0, True) Then
© www.soinside.com 2019 - 2024. All rights reserved.