根据条件复制excel字段块

时间:2017-03-13 03:16:28

标签: excel excel-vba vba

我正在寻找一种基于以下条件填写某些字段的快速方法。 (见图)

我有一个包含3列的列表。我需要根据A列中的字母填写C列。当我转到C34时,我想自动搜索上面的行,并根据A列中的字母复制上面最新出现的11个名称。因此,在C34-C44中,C1-C11中的名称将被复制为块。

Excel中是否有可以执行此操作的功能?

Data Screenshot is here

1 个答案:

答案 0 :(得分:1)

您可以使用带有两个 FOR循环的简单VBA宏来解决您的问题:

Sub CompleteRows()
Dim lastrow As Long 

lastrow = Cells(Rows.Count, "A").End(xlUp).Row 'finds last row in column A

For x = 1 To lastrow 'loop that starts with value 1 and goes all the way to the value of lastrow
    If Cells(x, 3).Value = "" Then 'if value in column C is empty then continue on 
        For y = 1 To lastrow 'second loop that runs through the same range
            If Cells(y, 1).Value = Cells(x, 1).Value And Cells(y, 2).Value = Cells(x, 2).Value Then
            'If the value of the first column and the value of the second
            'column for both values match, then add value to column C 
                Cells(x, 3).Value = Cells(y, 3).Value
                Exit For 'Exit loop if value was found
            End If
        Next y
    End If
Next x
End Sub