excel - 使 Excel 查找 marco 灵活以涵盖不同的引用长度

标签 excel vlookup vba

我有一个宏,它正在 3 个工作表中搜索 invoice number用户可能输入的信息(总共超过 260 万条记录)。

这些数字出现在一个单元格中,其中也有一个查找引用。表格:invoicenumber, reference_letter .

最初,这很好,因为 invoice numbers是 10 位数。现在它们可以是任何东西,但最后总是有一个逗号,在单个字符引用之前。

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address <> "$A$5" Then Exit Sub
If Target.Value = "" Then Exit Sub

Application.EnableEvents = False

Range("B5") = ""

For Each sh In Sheets
    If sh.Name = ActiveSheet.Name Then GoTo 111
    sh.Range("B1").FormulaArray = "=IFERROR(MATCH(Main!A5,LEFT(A:A,10),0),"""")"
    If sh.Range("B1") <> "" Then
        x = sh.Range("B1")
        Range("B5") = Right(sh.Range("A" & x), 1)
        Exit For
    End If
111
Next sh

Application.EnableEvents = True

If x = "" Then MsgBox "Not Found!"

End Sub

我知道这 10 个字符的限制在第 8 行,我尝试用 FIND 替换,但我认为我做的不对(基于我无法让它工作!)。

我将不胜感激帮助解决这个问题。

我还有一个 vLookup,它采用最后一个字符并从单独的工作表中返回文本。

最佳答案

尝试这个

Private Sub Worksheet_Change(ByVal Target As Range)
Dim InvLen As Integer
If Target.Address <> "$A$5" Then Exit Sub
If Target.Value = "" Then Exit Sub
Application.EnableEvents = False
Range("B5") = ""
    For Each sh In Sheets
        If sh.Name = ActiveSheet.Name Then GoTo 111
        InvLen = Len(Worksheets("Main").Range("A5").value)
        sh.Range("B1").FormulaArray = "=IFERROR(MATCH(Main!A5,LEFT(A:A," & InvLen & "),0),"""")"
        If sh.Range("B1") <> "" Then
            x = sh.Range("B1")
            Range("B5") = Right(sh.Range("A" & x), 1)
            Exit For
        End If
111
    Next sh
Application.EnableEvents = True
If x = "" Then MsgBox "Not Found!"
End Sub

关于excel - 使 Excel 查找 marco 灵活以涵盖不同的引用长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32567224/

相关文章:

Python 解决方案 - 使用 Python 将 Microsoft BI 工作表导出到 Excel

excel - VLOOKUP 返回空白为 1/0/1990,而不是没有任何可见的内容

Excel - vlookup 值 - 来自另一个工作表的 2 列(无连接)的组合

Excel - 跨多列或具有多个值的一列进行多值搜索

mysql - 无法重新连接到链接表 ODBC VBA

vba - Excel:如何将部分单元格的内容复制到相应行的其他相邻单元格中

Excel VBA 将图表轴设置为对数

python - 如何使用 xlrd、xlwt 和 xlutils 将 "existing"工作表添加到工作簿

java - JXL - 多张纸上的垂直和水平卡住

Excel VBA : Return first unhidden column from the left?