我正在尝试编写一个宏,该宏读取带有“ Ball,15mm”等输入的单元格,然后在A列具有“ Ball”和C列的输入时返回单独表的K列中的所有值输入为“ 15”。将有许多符合此条件的条目,因此我希望它列出所有匹配项。我发现很难弄清函数如何读取其中包含2条不同信息的单元格并匹配2个单独单元格中的单元格。有没有办法让Vlookup返回多个输入?
Sub RecordData()
MaxRows = Worksheets("Criteria").Cells(Rows.Count, "E").End(xlUp).Row
Worksheets("Criteria").Range("E" & MaxRows + 1) = WorksheetFunction.VLookup(Worksheets("Criteria").Range("B3"), Worksheets("Generate").Range("E:G"), 2, False)
Worksheets("Criteria").Range("F" & MaxRows + 1) = Worksheets("Criteria").Range("B4")
Worksheets("Criteria").Range("G" & MaxRows + 1) = Worksheets("Criteria").Range("B5")
Worksheets("Criteria").Range("H" & MaxRows + 1) = Worksheets("Criteria").Range("B6")
'Below is the line of code im trying to work on'
Worksheets("Criteria").Range("I" & MaxRows + 1) = WorksheetFunction.VLookup(Worksheets("Criteria").Range("E3"), Worksheets("BOM").Range("A:O"), 11, True)
Worksheets("Criteria").Range("G" & MaxRows + 2) = "Total"
Worksheets("Criteria").Range("H" & MaxRows + 2) = Application.Sum(Range(Worksheets("Criteria").Range("H2"), Worksheets("Criteria").Range("H" & MaxRows + 1)))
End Sub
答案 0 :(得分:0)
您可以使用Split()
函数从单个单元格内容中提取两个条件:
Dim criteria1 As String, criteria2 As String
With Worksheets("Criteria").Range("A1") ' change "Criteria" and "A1" to your actual sheet name and its range address where to find your 'two pieces of information'
criteria1 = Split(.Value, ",")(0)
criteria2 = Trim$(Split(.Value, ",")(1))
End With
Trim()
用于消除“ 15毫米”中的前导空间。另外,如果您确定所有夫妇都用逗号和空格分隔,则可以使用Split(.Value, ", ")
答案 1 :(得分:0)
使用DisplayName的答案,我会产生一些帮助您的信息:
让数据的结构如下表1所示:
尝试:
Option Explicit
Sub test()
Dim Criteria1 As String, Criteria2 As String, FullCriteria As String
Dim Lastrow As Long, Result As Double
Dim rng1 As Range, rng2 As Range, rng3 As Range
With ThisWorkbook.Worksheets("Sheet1")
FullCriteria = .Range("B1").Value
Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
Set rng1 = .Range("A3:A" & Lastrow)
Set rng2 = .Range("C3:C" & Lastrow)
Set rng3 = .Range("K3:K" & Lastrow)
Criteria1 = Split(FullCriteria, ",")(0)
Criteria2 = Trim$(Split(FullCriteria, ",")(1))
Result = Application.WorksheetFunction.SumIfs(rng3, rng1, "=" & Criteria1, rng2, "=" & Criteria2)
End With