协调原始数据以注册

时间:2017-12-12 09:12:48

标签: excel vba excel-vba

我有2张床单 - 一张"注册"和一个"摘要" - 我需要一个VBA脚本,我可以将其附加到宏按钮,以匹配“注册”列中的每个单元格(唯一编号)和“摘要”表上的列D.

如果匹配,我希望它复制摘要表的同一行中的列L中的单元格,并将行绿色,将该单元格粘贴到“注册”表的第Q列 - 如果Q列中有一个值,然后将其添加到顶部(因为虽然Register工作表上的计划编号是唯一的,但摘要表上可能有多个条目)。

然后循环到O列中的下一个单元格,尝试找到下一个匹配。

1 个答案:

答案 0 :(得分:0)

这将满足您的要求:

Sub foo()
Dim RegisterLastRow As Long
Dim SummaryLastRow As Long
Dim UniqueLookUp As Variant
RegisterLastRow = Sheets("Register").Cells(Sheets("Register").Rows.Count, "A").End(xlUp).Row 'get the last row with data in Register
SummaryLastRow = Sheets("Summary").Cells(Sheets("Summary").Rows.Count, "A").End(xlUp).Row ' get the last row with data in Summary

For x = 2 To RegisterLastRow 'loop through Register from row 2 to end (excluding headers)
    UniqueLookUp = Sheets("Register").Cells(x, 15).Value 'get the unique value from column 15 = Column O
    For y = 2 To SummaryLastRow 'loop through Summary from row 2 to end (to exclude headers)
        If Sheets("Summary").Cells(y, 4).Value = UniqueLookUp Then 'if values match
            Sheets("Summary").Cells(y, 4).EntireRow.Interior.ColorIndex = 4 'color row in green
            Sheets("Register").Cells(x, 17).Value = Val(Sheets("Register").Cells(x, 17).Value) + Val(Sheets("Summary").Cells(y, 12).Value) 'add the values to column Q =17 on Register from Summary column L = 12
        End If
    Next y
Next x
End Sub