【问题标题】:Merge data from one sheet to another based on criteria and only populate specific cells根据标准将数据从一张表合并到另一张表,并仅填充特定单元格
【发布时间】:2020-08-11 18:54:37
【问题描述】:

我希望合并两个数据源。

表 1

Sheet1 上的数据源包含 2 列:AccountID 和 Cost。源位于单元格 A3:B3(数据行从 A4 开始)中,用于 1 到 N 条记录。辅助单元格包含要在此源中使用的所有记录的年份值。它在 D1 单元格中。

表 2

Sheet2 上的数据源包含 6 列,范围为 A1:F1(数据从第 2 行开始):年份、帐户 ID、位置、电话号码、电子邮件类型和成本。数据持续 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”在 sheet1 和 Sheet2 上。 Sheet2 上的记录的年份值为 2019。Sheet1 上的记录具有与之关联的 2020 年(Sheet1!D1 单元格值)。最终结果将在 Sheet2 中包含两行 AccountID 为“1234”的数据,一行用于 2019 年的记录,另一行用于 2020 年的记录,如 Sheet1 数据源中所示。后者不包括 Location、Phone# 或 Email 类型值,因为它们未指定

Sheet1

Sheet2

Sheet2 once Sheet1 data is appended

【问题讨论】:

  • 大卫,这里没有免费的代码编写服务。如果您编写了代码并遇到问题,请围绕该想法重新表述您的问题。

标签: excel vba merge append criteria


【解决方案1】:

也许是这样的?

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

【讨论】:

  • 谢谢卡玛!有用。我看到的唯一挑战是,在 c.Offset(1, 0).EntireRow.Insert '在找到的单元格下方插入整行 行中,它替换了 sheet2 中已经存在的记录。例如,如果 sheet2 有 2 条 AccountID 记录,我假设这一行在第一行中找到第一条记录,然后覆盖第二行。如果您能帮助我如何避免这种情况,那就太好了。否则我会四处寻找解决方案。再次感谢您!
  • @DavidD,你的意思是在 Sheet2 列 ID 中,有可能有两个或多个相同的 ID 吗?如果是,那么您要在哪个 ID 上插入新年和费用?首先 ?第二 ?第三 ?依此类推(取决于那里有多少相同的 ID)
  • 是的。我在那行代码之后添加了 Shift:-xlDown,它当前正在运行。希望它能完成这项工作。再次感谢您的及时回复。我希望它添加到添加到 A 列的最新年份。因此,如果该 accountID 已经存在 2019 年,那么我在下面添加的新行将在 2019 年之后添加。
  • @DavidID,你写了“我假设这一行在第一行找到第一条记录,然后覆盖第二行”......我不明白,因为在我这边,它确实不要覆盖找到的单元格下方的行,而是将找到的单元格下方的行移动到下一行。 c.Offset(1, 0).EntireRow.Insert 行是在找到的单元格正下方插入一个空白行,而之前找到的单元格下方的所有行都将移动到下一行。顺便说一句,我添加了排序代码。也许您可以尝试一下,希望结果如您所愿。
  • 抱歉,我在添加一行时出错了。我将 Offset(1,0) 更改为 Offset(5,0) 所以显示奇怪的结果。再次感谢您提供的排序功能,它非常有用!
猜你喜欢
  • 2021-12-02
  • 1970-01-01
  • 1970-01-01
  • 2015-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-31
相关资源
最近更新 更多