我有一个宏,它正在 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/