【问题标题】:Search multiple ranges and return values to different cells搜索多个范围并将值返回到不同的单元格
【发布时间】:2022-10-13 01:09:13
【问题描述】:

我正在尝试创建一个显示/仪表板,列出一个月内销售的汽车,按销售员细分。

我有一个输入表,每周输入汽车。

我正在寻找一种方法来搜索每周销售的汽车列表并返回相应推销员下的值。

我使用了 IF AND 函数,但不相信这些函数适合我想要实现的目标。

每月显示

[每周输入]

【问题讨论】:

  • 试试index/match
  • 使用数据透视表?
  • 我认为索引/匹配是正确的,但是它更多的是让值返回到不同的单元格,即如果单元格 A1 是第一个可用的“模型”单元格但其中已经有一个值,那么下一个将在单元格 A5 中如果有意义的话,将是下一个可用的“模型”单元吗?
  • 请提供足够的代码,以便其他人可以更好地理解或重现该问题。
  • 过滤和查找公式听起来像是要走的路。不过,如果没有看到数据,几乎不可能说出来。您能否将您的数据图片添加到您的问题中? Edit 按钮就在您问题中的标签下方。

标签: excel vba


【解决方案1】:

如果我对您的理解正确,并且您不介意更改工作表显示中的“标题”...那么首先您需要复制原始工作簿并在复制的工作簿上测试以下子项。

首先,使工作表的标题显示如下:
每个名称由一列分隔。所以 Mark H 将在 L 列中,依此类推。

分步运行子程序,请不要点击“播放”来运行子程序-因为子程序在选择情况下不完整--->它只定义了DB和PR的oFill。

我没有写完整的子程序,但我希望这个示例子程序可以帮助您入门。

Sub test()
Dim sh1 As Worksheet: Dim sh2 As Worksheet
Dim arr1: Dim arr2: Dim arr3
Dim rg As Range: Dim cell As Range: Dim oFill As Range
Dim x As String: Dim y As String
Dim j As Long: Dim i As Long

'set the worksheet as sh1 and sh2 variable, and set the range of sh1 column A as rg variable
Set sh1 = Sheets("Weekly Input")
Set sh2 = Sheets("Display")
Set rg = sh1.Range("A2", sh1.Range("A" & Rows.Count).End(xlUp))

'this is the loop for 4 week in sheet Weekly Input
'where rg at the first iteration is column A
'2nd iteration is column L, and so on
For j = 1 To 4
    
    'this is the loop to each row of data value in rg (the N/U column)
    For Each cell In rg
        
        'join the name, model, reg and date with comma separated into variable x
        x = cell.Offset(0, 2).Value & "," & cell.Offset(0, 3).Value & "," & _
            cell.Offset(0, 1).Value & "," & cell.Offset(0, 9).Value
            
        'make x value into into array as arr1 variable
        arr1 = Split(x, ",")
        
        'join the prds, fin, px and discount with comma separated into variable y
        y = cell.Offset(0, 5).Value & "," & cell.Offset(0, 6).Value & "," & _
            cell.Offset(0, 7).Value & "," & cell.Offset(0, 8).Value
            
        'make y value into array as arr2 variable
        arr2 = Split(y, ",")
        
        'create arr3 variable by joining arr1 and arr2
        ReDim arr3(0 To 1, 0 To UBound(arr1))
            For i = 0 To UBound(arr3, 2)
                arr3(0, i) = arr1(i)
                arr3(1, i) = arr2(i)
            Next

    'check what is the value of the looped row,column S/C
    Select Case UCase(cell.Offset(0, 4).Value)
    
        'if the value is DB
        Case "DB"
            'check, if the looped cell value is u, set the range in sh2 to a blank cell of column B as oFill variable
            'other then "u" (meaning "n"), set the range in sh2 to a blank cell of column D as oFill variable
            If cell.Value = "u" Then Set oFill = sh2.Range("B" & Rows.Count).End(xlUp).Offset(1, 0) _
                Else Set oFill = sh2.Range("D" & Rows.Count).End(xlUp).Offset(1, 0)
        
        'same thing with PR
        'add a similar code for MH and MD pointing to the needed range
        Case "PR"
            If cell.Value = "u" Then Set oFill = sh2.Range("G" & Rows.Count).End(xlUp).Offset(1, 0) _
                Else Set oFill = sh2.Range("i" & Rows.Count).End(xlUp).Offset(1, 0)
        End Select
        
        'put the arr3 value into oFill
        oFill.Resize(4, 2).Value = Application.Transpose(arr3)

    'looped to the next row of column N/U in sh2
    Next cell

'set the rg for the next iteration of the week
Set rg = rg.Offset(0, 11)

Next j

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-28
    • 2017-08-25
    • 2022-06-13
    • 1970-01-01
    相关资源
    最近更新 更多