从多个条件返回多个值

时间:2018-10-24 14:37:12

标签: excel vba

我正在尝试修改此链接上此站点上找到的代码。

Return multiple rows of data in vba

我需要查看A和B列中的信息,然后从C列到H提取基于A和B的匹配记录。

由于工作在多个日期进行,因此将有多个匹配项。

要学习和理解正在发生的事情,我试图一次构建一列代码。到目前为止,我没有错误,但是我收到的只是J2中的值1000。我没有收到4172633414。而且,我不知道J2是否具有来自单元格A2或C2的值。仅给出一个1000的值,而不是遍历A和B列中的所有条目。在现实生活中,A和B列具有124条记录,C列至H列有8,673个条目可供选择。

我希望我与编码尝试的距离不会太远。发布电子表格信息是一个挑战,因此我希望它是可读的。预先谢谢你。

列A和C具有标题CNumber。 B和D是JobNumber。通过H分别是小时,单位,日期,分支。这是我必须使用的东西的简短缩写。

import pyspark.sql.functions as f

correctNegativeDiff = f.udf(lambda diff: 0 if diff < 0 else diff, LongType())

df = df.withColumn('time_diff_1', correctNegativeDiff(df.time_diff_1))\
       .withColumn('time_diff_2', correctNegativeDiff(df.time_diff_2))\
       .withColumn('time_diff_3', correctNegativeDiff(df.time_diff_3))

以下是我正在寻找的非常简短的结果。由于CNumber 1000在A和B列中的JobNumber 4172633414以上。他在C和D中具有2个匹配的条目,以及下面的相关信息。 CNumber 1002的JobNumber为1463149151,下面有3个匹配的条目。

CNumber JobNumber   CNumber JobNumber Hours Units   Date    Branch  
1000    4172633414  1000    1955126105  575 100 7/19/2018   3  
1002    1463149151  1000    1955126105  600 144 7/20/2018   3  
1004    1955126105  1000    1955126105  200 2.5 6/25/2018   3  
1005    90999997    1000    4172633414  575 675 7/9/2018    3  
1007    3965310303  1000    4172633414  100 5   7/10/2018   3  
1008    1463149151  1002    381134312   300 46  6/29/2018   3  
1011    3163689368  1002    382309308   575 88  8/22/2018   3  
1012    3965310303  1002    1013397112  600 139 9/21/2018   3  
1013    1955126105  1002    1463149151  300 71  6/29/2018   3  
1016    1463149151  1002    1463149151  575 60  7/2/2018    3  
1017    1463149151  1002    1463149151  375 77  7/5/2018    3  
1018    1463149151  1004    1955126105  575 7.25 6/25/2018  3  

CNumber JobNumber   Hours Units Date    Branch  
1000    4172633414  575 675 7/9/2018    3  
1000    4172633414  100 5   7/10/2018   3  
1002    1463149151  300 71  6/29/2018   3  
1002    1463149151  575 60  7/2/2018    3  
1002    1463149151  375 77  7/5/2018    3  
1004    1955126105  575 7.25 6/25/2018  3  
1004    1955126105  575 5   6/26/2018   3  
1005    90999997    575     6/25/2018   3  
1005    90999997    250     6/26/2018   3 

1 个答案:

答案 0 :(得分:0)

您错过了一个循环。另外,我更喜欢获取数据并在内存中使用它,而不是来回地传递到工作表中。试试看:

显式选项

Sub MultiLookup()

将arrClientJob设置为变体 Dim arrJobInfo作为变体 变暗arrResult作为变体

Dim lngLastRow如长 Dim intClient为整数 Dim intJobInfo为整数 将Dim intCol转换为整数

一切都在一张纸上

On Error Resume Next
lngLastRow = Sheets("TestJobs").Range("A:A").Find( _
    What:="*", After:=Sheets("TestJobs").Range("A1"), _
    MatchCase:=False, _
    LookAt:=xlPart, LookIn:=xlValues, _
    SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Row
If Err <> 0 Then
    lngLastRow = 0
    Err.Clear
End If

If lngLastRow > 1 Then
    arrClientJob = Sheets("TestJobs").Range("A2").Resize(lngLastRow - 1, 2).Value
Else
    MsgBox "No client data, exiting", vbOKOnly
End If

lngLastRow = Sheets("TestJobs").Range("C:C").Find( _
    What:="*", After:=Sheets("TestJobs").Range("C1"), _
    MatchCase:=False, _
    LookAt:=xlPart, LookIn:=xlValues, _
    SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Row
If Err <> 0 Then
    lngLastRow = 0
    Err.Clear
End If

If lngLastRow > 1 Then
    arrJobInfo = Sheets("TestJobs").Range("C2").Resize(lngLastRow - 1, 6).Value
Else
    MsgBox "No ClientJob info found, exiting", vbOKOnly
End If

ReDim arrResult(LBound(arrJobInfo, 2) To UBound(arrJobInfo, 2), 1 To 1)

For intClient = LBound(arrClientJob, 1) To UBound(arrClientJob, 1)
    For intJobInfo = LBound(arrJobInfo, 1) To UBound(arrJobInfo, 1)
        If arrClientJob(intClient, 1) = arrJobInfo(intJobInfo, 1) _
        And arrClientJob(intClient, 2) = arrJobInfo(intJobInfo, 2) Then
            If arrResult(LBound(arrResult, 1), LBound(arrResult, 2)) > "" Then
                ReDim Preserve arrResult(LBound(arrResult, 1) To UBound(arrResult, 1), _
                    LBound(arrResult, 2) To UBound(arrResult, 2) + 1)
            End If
            For intCol = LBound(arrJobInfo, 2) To UBound(arrJobInfo, 2)
                arrResult(intCol, UBound(arrResult, 2)) = arrJobInfo(intJobInfo, intCol)
            Next intCol
        End If
    Next intJobInfo
Next intClient

Sheets("TestJobs").Range("J1").Resize(UBound(arrResult, 2) - LBound(arrResult, 2) + 1, _
    UBound(arrResult, 1) - LBound(arrResult, 1) + 1).Value = WorksheetFunction.Transpose(arrResult)

结束子

相关问题