根据条件将数据从一个 sheet 合并到另一个,并且只填充特定的单元格
Merge data from one sheet to another based on criteria and only populate specific cells
我有两个数据源想要合并。
Sheet 1
Sheet1 上的数据源包含 2 列:AccountID 和 Cost。源位于单元格 A3:B3 中(数据行从 A4 开始),用于 1 到 N 条记录。辅助单元格包含要在该来源中使用的所有记录的年份值。它在单元格 D1 中。
Sheet 2
Sheet2 上的数据源包含 6 列,范围 A1:F1(数据从第 2 行开始):年份、帐户 ID、位置、Phone#、电子邮件类型,&成本。数据继续 1 到 N 条记录。
我希望根据每个数据范围(Sheet1 A 列,Sheet2 B 列)的 AccountID 连接两个表的记录,其中年份(来源于 Sheet1!D1) 被添加到 Sheet2 数据上的所有记录中,并且通过 AccountID 匹配的记录。数据附加/添加到最后可用行的 Sheet2。
示例 #1
AccountID“1234”在 Sheet1 但不在 Sheet2。年份值 (Sheet1!D1) 包含 2020。最终结果将在 Sheet2 中添加一个新行,其中 AccountID 为“1234”,关联的成本值和 2020 年。
示例 #2
AccountID“1234”在工作表 1 和 Sheet2 上。 Sheet2 上的记录的年份值为 2019。Sheet1 上的记录的年份为 2020(Sheet1!D1 单元格值)。最终结果将在 Sheet2 中有两行 AccountID 为“1234”的数据,一行用于 2019 年的记录,另一行用于 2020 年的记录,如在 Sheet1 数据源中找到的那样。后者不包括位置、Phone# 或电子邮件类型值,因为它们未指定
Sheet1
Sheet2
Sheet2 once Sheet1 data is appended
也许是这样的?
Sub test()
Set rng_ID = Sheets("Sheet2").Range("B:B") 'set ID range on sheet2
oYear = Sheets("Sheet1").Range("D1").Value 'get year value on sheet1
Set Rng = Sheets("Sheet1").Range("A4", Sheets("Sheet1").Range("A4").End(xlDown)) 'set ID range on sheet1
With Sheets("Sheet2")
For Each cell In Rng 'loop to each cell in ID range on sheet1
oCost = cell.Offset(0, 1).Value 'set the cost value
Set c = rng_ID.Find(cell.Value, lookat:=xlWhole) 'find if the cell value is in ID range on sheet2
If Not c Is Nothing Then 'if found
Set xCopy = .Range(c, c.Offset(0, 4)) 'set the range found to be copied
c.Offset(1, 0).EntireRow.Insert 'insert entire row below the found cell
xCopy.Copy Destination:=c.Offset(1, 0) 'copy the range above then paste
c.Offset(1, -1).Value = oYear 'fill the year
c.Offset(1, 4).Value = oCost 'file the cost
Else 'if not found
Set oFill = Sheets("Sheet2").Range("B500000").End(xlUp).Offset(1, 0) 'set the last blank cell in column B sheet2
oFill.Offset(0, -1).Value = oYear 'fil the year
oFill.Offset(0, 4).Value = oCost 'fill the cost
oFill.Value = cell.Value 'fill the ID
End If
Next cell
End With
'unmark the line below which one you want
'Call SortByYearThenByID
'Call SortByIDThenByYear
End sub
我尝试添加排序代码,也许结果如你所愿
Sub SortByYearThenByID()
Set rngSort = Sheets("Sheet2").Range("A1", Range("F1").End(xlDown))
Sheets("Sheet2").Sort.SortFields.Clear
rngSort.Sort Key1:=rngSort.Columns(1), Key2:=rngSort.Columns(2), Order1:=xlAscending, Header:=yes
End Sub
Sub SortByIDThenByYear()
Set rngSort = Sheets("Sheet2").Range("A1", Range("F1").End(xlDown))
Sheets("Sheet2").Sort.SortFields.Clear
rngSort.Sort Key1:=rngSort.Columns(2), Key2:=rngSort.Columns(1), Order1:=xlAscending, Header:=yes
End Sub
我有两个数据源想要合并。
Sheet 1
Sheet1 上的数据源包含 2 列:AccountID 和 Cost。源位于单元格 A3:B3 中(数据行从 A4 开始),用于 1 到 N 条记录。辅助单元格包含要在该来源中使用的所有记录的年份值。它在单元格 D1 中。
Sheet 2
Sheet2 上的数据源包含 6 列,范围 A1:F1(数据从第 2 行开始):年份、帐户 ID、位置、Phone#、电子邮件类型,&成本。数据继续 1 到 N 条记录。
我希望根据每个数据范围(Sheet1 A 列,Sheet2 B 列)的 AccountID 连接两个表的记录,其中年份(来源于 Sheet1!D1) 被添加到 Sheet2 数据上的所有记录中,并且通过 AccountID 匹配的记录。数据附加/添加到最后可用行的 Sheet2。
示例 #1
AccountID“1234”在 Sheet1 但不在 Sheet2。年份值 (Sheet1!D1) 包含 2020。最终结果将在 Sheet2 中添加一个新行,其中 AccountID 为“1234”,关联的成本值和 2020 年。
示例 #2
AccountID“1234”在工作表 1 和 Sheet2 上。 Sheet2 上的记录的年份值为 2019。Sheet1 上的记录的年份为 2020(Sheet1!D1 单元格值)。最终结果将在 Sheet2 中有两行 AccountID 为“1234”的数据,一行用于 2019 年的记录,另一行用于 2020 年的记录,如在 Sheet1 数据源中找到的那样。后者不包括位置、Phone# 或电子邮件类型值,因为它们未指定
Sheet1
Sheet2
Sheet2 once Sheet1 data is appended
也许是这样的?
Sub test()
Set rng_ID = Sheets("Sheet2").Range("B:B") 'set ID range on sheet2
oYear = Sheets("Sheet1").Range("D1").Value 'get year value on sheet1
Set Rng = Sheets("Sheet1").Range("A4", Sheets("Sheet1").Range("A4").End(xlDown)) 'set ID range on sheet1
With Sheets("Sheet2")
For Each cell In Rng 'loop to each cell in ID range on sheet1
oCost = cell.Offset(0, 1).Value 'set the cost value
Set c = rng_ID.Find(cell.Value, lookat:=xlWhole) 'find if the cell value is in ID range on sheet2
If Not c Is Nothing Then 'if found
Set xCopy = .Range(c, c.Offset(0, 4)) 'set the range found to be copied
c.Offset(1, 0).EntireRow.Insert 'insert entire row below the found cell
xCopy.Copy Destination:=c.Offset(1, 0) 'copy the range above then paste
c.Offset(1, -1).Value = oYear 'fill the year
c.Offset(1, 4).Value = oCost 'file the cost
Else 'if not found
Set oFill = Sheets("Sheet2").Range("B500000").End(xlUp).Offset(1, 0) 'set the last blank cell in column B sheet2
oFill.Offset(0, -1).Value = oYear 'fil the year
oFill.Offset(0, 4).Value = oCost 'fill the cost
oFill.Value = cell.Value 'fill the ID
End If
Next cell
End With
'unmark the line below which one you want
'Call SortByYearThenByID
'Call SortByIDThenByYear
End sub
我尝试添加排序代码,也许结果如你所愿
Sub SortByYearThenByID()
Set rngSort = Sheets("Sheet2").Range("A1", Range("F1").End(xlDown))
Sheets("Sheet2").Sort.SortFields.Clear
rngSort.Sort Key1:=rngSort.Columns(1), Key2:=rngSort.Columns(2), Order1:=xlAscending, Header:=yes
End Sub
Sub SortByIDThenByYear()
Set rngSort = Sheets("Sheet2").Range("A1", Range("F1").End(xlDown))
Sheets("Sheet2").Sort.SortFields.Clear
rngSort.Sort Key1:=rngSort.Columns(2), Key2:=rngSort.Columns(1), Order1:=xlAscending, Header:=yes
End Sub