excel - 是否可以将 InStr() 与 Application.Vlookup() 结合使用?

标签 excel vba formula vlookup

我目前有一个 With 语句,它将一个单元格的值与一系列单元格进行比较,并且 InStr() 返回 true,然后将 cell.Value 标记为“是”,如果不是,则“否”

这是场景。假设我正在检查 A2 的值,这是 111。我将 111 与一系列其他单元格进行比较,应匹配的字段是 111, 222, 333,因为它包含 111。我下面的第一组代码适用于此,但速度很慢。我希望有一种更快的方法,我想我可以用公式来做到这一点,但在 vlookup 中使用 True 而不是 False 并没有像我想象的那样工作。

这是代码:

With inv_twcg_ws
    For Each cell In .Range("A2:A" & .Cells(Rows.Count, "A").End(xlUp).Row)
        For Each cellx In report_rng
            If InStr(cellx.Value, cell.Value) Then
                cell.Offset(0, 6).Value = "Yes"
                Exit For
            End If
                cell.Offset(0, 6).Value = "No"
        Next cellx
    Next cell
End With

问题是它有点慢。我的应用程序运行大约需要 5 分钟。我希望以某种方式将 InStr()Application.Vlookup() 结合起来,尝试加快公式速度。这可能吗?

这是我当前的 Vlookup,但使用 True 似乎无法满足我的需求...

With inv_twcg_ws
    For Each cell In .Range("G2:G" & .Cells(Rows.Count, "A").End(xlUp).Row)
        cell.Value = Application.WorksheetFunction.IfError(Application.VLookup(CStr(cell.Value), report_rng, 1, True), "No")
    Next cell
End With

最佳答案

使用变量数组:

Dim report_ws As Worksheet
Set report_ws = report_wb.Worksheets(1)

Dim report_rng As Variant
report_rng = report_ws.Range("B2:B" & last_row)

Dim inv_twcg_ws As Worksheet
Set inv_twcg_ws = Worksheets("Sheet1") ' change to your sheet




With inv_twcg_ws
    Dim lkup_rng As Variant
    lkup_rng = .Range("A2:A" & .Cells(Rows.Count, "A").End(xlUp).Row).Value

    Dim otpt As Variant
    ReDim otpt(1 To UBound(lkup_rng, 1), 1 To 1) As Variant

    Dim i As Long
    For i = LBound(lkup_rng, 1) To UBound(lkup_rng, 1)
        otpt(i,1) = "No"
        Dim j As Long
        For j = LBound(report_rng, 1) To UBound(report_rng, 1)
            If InStr(report_rng(j, 1), lkup_rng(i, 1)) Then
                otpt(i,1) = "Yes"
                Exit For
            End If
        Next j
    Next i

    .Range("G2").Resize(UBound(otpt, 1)).Value = otpt
End With

关于excel - 是否可以将 InStr() 与 Application.Vlookup() 结合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52372838/

相关文章:

visual-studio-2010 - Windows 7 下的 ClickOnce/Excel-VSTO

excel - 在组合框/列表框中启用鼠标滚轮滚动

excel - 根据三个表中的值从两个单独的表中查找值的公式

vba - 获取 Collection 对象上项目的键

excel - 如果不能在 Excel 中工作,求和

c++ - 我的弹射器游戏产生的距离不正确

internet-explorer - spring AbstractExcelView 和 IE

excel - VBA循环结合Lastrow并查找空白值

vba - 在vba中UnNest无限数量的嵌套对象

azure - 此 Azure Batch Autoscale 公式的含义是什么?