具有多个参数的 VLOOKUP?

VLOOKUP with multiple parameters?

这里是新手,还请多多包涵。我对 VLOOKUP 相当满意,但这就是我的专业知识的终点。

Sheet1 是我正在处理的活动 sheet。

Sheet2 是我的源数据。

列比我上面提到的要多得多,所以在我的活动 sheet 中,我已经使用 VLOOKUP 来传输我正在使用的 3 个主要数据列,它们是;

“贷款号”、“客户名称”、“贷方”

基于“贷款编号”的重复行也已删除。所以,现在每个客户只有 1 行。

我现在想做的是,在每个客户的唯一行中,使用原始源数据,将“$ 金额”填充到相应的列中,带有月份标题,也基于“类型”值

所以基本上

我知道可能有多种方法可以做到这一点,但我不想乱用 Pivots 或类似的东西,我也不想更改源数据的格式。首选基本公式,因为我正在使用书面公式等整合一些 VBA 函数...

如有任何帮助,我们将不胜感激!

西蒙

您需要在整个工作表中进行调整,但 SUMIFS 应该可以为您完成...

这是单元格 K2 中的公式 ...

=SUMIFS($F:$F,$A:$A,$H2,$D:$D,">=" & K,$D:$D,"<=" & EOMONTH(K,0))

本质上,它是根据多个条件对您的数据进行汇总。您可以根据需要添加其他条件。这里要注意的重要一点是日期 ...

K1 = 1/07/2021,格式为... mmmm ...必须是日期。

L1 = =EDATE(K1,1), 格式为... mmmm

确保填写 K1N1.

要过滤日期(即使用 >= 和 <=),所有字段都必须是有效日期。

日期很重要,如果它们不是日期,那么将日期(即 1/01/2021)与单词(即 January)进行比较将不起作用。

请尝试下一个代码。它应该很快,使用数组并且大部分处理都在内存中完成。它在另一个sheet(shDest)中return,我在下一个中测试到return:

Sub TransposeDataByLenderMonth()
  Dim sh As Worksheet, shDest As Worksheet, lastR As Long, minD As Date, maxD As Date, arrD
  Dim dict As Object, El, arr, arrFin, i As Long, k As Long, mtch, strD As String
  
  Set sh = ActiveSheet 'use here the sheet you need
  Set shDest = sh.Next 'use here the destination sheet (where the processing result to be returned)
                       'if next one is empty, the code can be used as it is
  lastR = sh.Range("A" & sh.rows.Count).End(xlUp).row
  minD = WorksheetFunction.min(sh.Range("D2:D" & lastR)) 'find first Date
  maxD = WorksheetFunction.Max(sh.Range("D2:D" & lastR)) 'find last date

  'create months arrays (one for months header and second to match the date):
  arrD = GetMonthsIntArraysl(minD, maxD)
  
  arr = sh.Range("A2:F" & lastR).Value  'place the whole range in an array, for faster iteration
  'Extract unique keys by LoanNo:
  Set dict = CreateObject("Scripting.Dictionary")
  For i = 1 To UBound(arr)
        dict(arr(i, 1)) = Empty
  Next i

  ReDim arrFin(1 To dict.Count + 1, 1 To UBound(arrD(1)) + 4): k = 1
  'Place the header in the final array:
  arrFin(1, 1) = "LoanNo": arrFin(1, 2) = "Client Name": arrFin(1, 3) = "Lender"
  For i = 1 To UBound(arrD(0))
        arrFin(1, 4 + i) = CStr(arrD(0)(i, 1))
  Next i
  k = 2  'reinitialize k to load values after the header
  For Each El In dict.Keys     'iterate between the unique elements:
    For i = 1 To UBound(arr)   'iterate between the main array rows:
        If arr(i, 1) = El Then 'when a match is found:
            arrFin(k, 1) = arr(i, 1): arrFin(k, 2) = arr(i, 2): arrFin(k, 3) = arr(i, 3)
            strD = CStr(Format(DateSerial(Year(arr(i, 4)), month(arr(i, 4)), 1), "dd/mm/yyyy"))
            mtch = Application.match(Replace(strD, ".", "/"), arrD(1), True)
            arrFin(k, 4 + mtch) = arr(i, 6) + arrFin(k, 4 + mtch) 'add the value of the appropriate month
        End If
    Next i
    k = k + 1
  Next El
  'Format a little and drop the processed array content:
  With sh.Range("J1").Resize(1, UBound(arrFin, 2))
        .NumberFormat = "@"
        .BorderAround 1, xlMedium
  End With
  With sh.Range("J1").Resize(UBound(arrFin), UBound(arrFin, 2))
        .Value = arrFin
        .EntireColumn.AutoFit
        .BorderAround 1, xlMedium
        .Borders(xlInsideVertical).Weight = xlThin
  End With
  MsgBox "Job done..."
End Sub

'The following function returns an array of two arrays. 
'One for header and the other for matching the date columns (in the final array)
Private Function GetMonthsIntArraysl(startDate As Date, endDate As Date) As Variant
   Dim monthsNo As Long, rows As String, monthsInt, dd As Long, arrStr, arrD
    monthsNo = DateDiff("m", startDate, endDate, vbMonday)
    rows = month(startDate) & ":" & monthsNo + month(startDate)

    arrStr = Evaluate("Text(Date(" & Year(startDate) & ",row(" & rows & "),1),""mmmm YYYY"")")
    arrD = Evaluate("Text(Date(" & Year(startDate) & ",row(" & rows & "),1),""dd/mm/yyyy"")")
    GetMonthsIntArraysl = Array(arrStr, arrD)
End Function

以上代码添加了属于相同 months/Client 的日期的所有值,如果是这样...

我把你的问题当成了一个挑战,但你必须了解,为了得到一个解决方案,你必须自己做一些研究并且给我们看一段代码。请帮个忙,下次从上述角度提出一个更好的问题,遵守社区规则。