简体   繁体   English

根据标准将数据从一张表合并到另一张表,并仅填充特定单元格

[英]Merge data from one sheet to another based on criteria and only populate specific cells

I have two data sources I wish to combine.我有两个想要合并的数据源。

Sheet 1表 1

Data source on Sheet1 contains 2 columns: AccountID and Cost. Sheet1 上的数据源包含 2 列:AccountID 和 Cost。 Source is in cells A3:B3 (data row starts in A4) for 1 to N records.源位于单元格 A3:B3(数据行从 A4 开始)中,用于 1 到 N 条记录。 A secondary cell contains the year value for all records to be used in this source.辅助单元格包含要在此源中使用的所有记录的年份值。 It is in cell D1.它位于单元格 D1 中。

Sheet 2表 2

Data source on Sheet2 contains 6 columns and in range A1:F1 (data starts on row 2): Year, AccountID, Location, Phone#, Email type, & Cost. Sheet2 上的数据源包含 6 列,范围为 A1:F1(数据从第 2 行开始):Year、AccountID、Location、Phone#、Email 类型和成本。 Data continues for 1 to N records.数据持续 1 到 N 条记录。

I wish to join the records of both tables based on the AccountID of each range of data (Sheet1 column A, Sheet2 column B), where the year (sourced on Sheet1,D1) is added to all records on the Sheet2 data.我希望根据每个数据范围(Sheet1 列 A,Sheet2 列 B)的 AccountID 加入两个表的记录,其中年份(来自 Sheet1,D1)被添加到 Sheet2 数据的所有记录中。 and records matching by AccountID.和由 AccountID 匹配的记录。 The data is appended /added to Sheet2 on last available row.数据被追加/添加到最后一个可用行的 Sheet2 中。

Example #1示例 #1

AccountID "1234" is on Sheet1 but not on Sheet2. AccountID“1234”在 Sheet1 上,但不在 Sheet2 上。 Year value (Sheet1.D1) contains 2020, The end result would have a new row added with the AccountID of "1234" into Sheet2, the associated Cost value.年份值 (Sheet1.D1) 包含 2020,最终结果将在 Sheet2 中添加一个新行,其中 AccountID 为“1234”,即关联的成本值。 and a year of 2020.和 2020 年。

Example #2示例#2

AccountID "1234" is on sheet1 and also on Sheet2. AccountID“1234”在 sheet1 和 Sheet2 上。 The record on Sheet2 has a year value of 2019. The record on Sheet1 has the year 2020 associated with it (Sheet1.D1 cell value), The end result would have two rows of data for AccountID of "1234" in Sheet2, one for the 2019 record and one for 2020 record. Sheet2 上的记录的年份值为 2019。Sheet1 上的记录具有与之关联的 2020 年(Sheet1.D1 单元格值),最终结果将在 Sheet2 中为 AccountID 的“1234”有两行数据,一个用于2019 年的记录和 2020 年的记录。 as found on Sheet1 data source, The latter would not include Location, Phone#, or Email type values as they are not specified如在 Sheet1 数据源中发现的那样,后者将不包括 Location、Phone# 或 Email 类型值,因为它们未指定

Sheet1表 1

Sheet2表 2

Sheet2 once Sheet1 data is appended附加 Sheet1 数据后的 Sheet2

Maybe something like this?也许是这样的?

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

I try to add it with a sort code, maybe the result as you want it我尝试使用排序代码添加它,也许是你想要的结果

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据另一个单元格中的条件将单元格从一个工作表复制到另一个工作表 - Copy cells from one sheet to another based on criteria in another cell 如果条件如何将特定单元格从一张纸复制到另一张纸 - How to copy specific cells from one sheet to another meting if criteria VBA 根据条件将数据从一个工作簿复制到另一个工作簿中的特定单元格 - VBA copying data from one workbook to specific cells in another, based on criteria Excel VBA - 根据 3 个条件(复杂的 IF AND 相关 VBA 与通配符)使用另一张工作表中的值填充一张工作表上的列 - Excel VBA - Populate a column on one sheet with values from another sheet based on 3 criteria (complicated IF AND related VBA with wildcards) 根据标准将数据从一张纸复制到另一张纸 - Copy data from one sheet to another based on criteria 根据另一列中的条件将一列中的单元格复制到另一张工作表 - Copy Cells in One Column, Based on Criteria in Another Column, to Another Sheet 用另一个工作表单元格上的数据填充一个工作表的多个单元格条目 - Populate multiple cell entries of one sheet with data on another sheet cells Excel VBA根据条件从一张纸复制到另一张纸的特定单元格 - Excel VBA copy from one sheet to other sheets specific cells based on criteria VBA-根据多个条件从另一张纸复制单元格 - VBA - copy cells from another sheet based on multiple criteria 根据特定条件将特定单元格值从一个工作表复制粘贴到另一个工作表 - Copy paste specific cell values from one work sheet to another based on a specific criteria
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM