匹配案例我不同的colums MS excel

时间:2016-04-01 11:14:31

标签: excel

example data

我有四列数据,其中两列是名称(A和D)。一个(B)是总工作时间,一个(E)是培训时间。

我可以编写一个执行此操作的函数: 在位置的C列中写入E列的值,即" 41"在第2行," 32.8"在第5行和" 24.6"在第8行。

谢谢你。

1 个答案:

答案 0 :(得分:0)

我只是快速将某些东西放在一起,但是它有效,您可能需要调整它以使用您的工作表名称等....

Private Sub FindNames()
Dim RngArr As Variant
Dim i As Long, j As Long
Dim Rws As Long
Dim FRw As Long

'Sheet1 here is not the tab name, but the CodeName (in VBA its the name not in brackets in project explorer)
RngArr = Sheet1.UsedRange.Value 'get range array

If Not IsArray(RngArr) Then Exit Sub    'either a single cell is used or something is wrong

FRw = Sheet1.UsedRange.Row
Rws = UBound(RngArr, 1) - 1           'get total rows in range minus 1

For i = FRw To FRw + Rws        'loop for the list in D:E
    If Not RngArr(i, 4) = vbnulstring Then
        For j = FRw To FRw + Rws    'loop for the list in A:B (C)
            'if ColD = ColA then ColC = ColE
            If RngArr(i, 4) = RngArr(j, 1) Then RngArr(j, 3) = RngArr(i, 5)
        Next j
    Else
        'you could exit the loop here if you list will never have empty spaces to save time although you wont notice
    End If
Next i

Sheet1.UsedRange.Value = RngArr 'since we are resizing the original used space we can just dump the results back

End Sub

希望这会有所帮助 保罗S。