将缺失的条目从一张表填充到另一张表

将缺失的条目从一张表填充到另一张表

我在 Excel 中有两个工作表。在本例中,工作表 1 和工作表 2。

第 1 页:100 条记录,姓名地点性别

第 2 页:100 条记录,姓名地点年龄

数据源不同步,因此,我有 Sheet 1 中存在但 Sheet 2 中不存在的数据,也有 Sheet 2 中存在但 Sheet 1 中不存在的数据,还有我同时在两个 Sheet 中都存在的数据。因此,Sheet 1 中可能存在 Sheet 2 中不存在的名称,反之亦然。

我正在寻找一种自动化方法,将两张工作表合并到第三张工作表中,并填充姓名和位置以及可用的年龄和性别。我知道这在 MS Access 中很容易做到,但由于我的受众,我需要将其保留在 Excel 中,而我对 VBA 完全是菜鸟 :S

我做了什么:在 Sheet 3 上,我创建了公式来从 Sheet 1 中提取数据,并在 Sheet 2 中添加了一个字段(“X”),如果 Sheet 1 中不存在该字段,则公式将用“1”标记该字段,如果 Sheet 1 中存在该字段,则用“0”标记该字段。我正在寻找一种方法来说明:“如果 Sheet 2 中的字段“X”=“1”,则提取该数据并将其附加到 Sheet 3,但我不确定如何做到这一点 :(

答案1

这需要一个不太困难的 VBA 脚本。

以下是伪代码

Create Array that contains All Names from Sheet B
For i = 0 to CellFromACount
 IF A.Name is not in ArrayB
   SheetB.AddLine (Item)
Next

为了编写代码,请按照Excel vba tutorial搜索引擎上的一些教程进行搜索。示例如下: VBA 教程

你需要的东西去做是:

  1. 字符串数组
  2. 从单元格获取价值
  3. For 循环
  4. If 条件
  5. 在表格底部添加新行

答案2

这应该可以。放入模块

Public Sub sheetmerge()
Dim wkb As Workbook
Dim wks, wks1, wks2 As Worksheet
Set wkb = ThisWorkbook
Set wks = wkb.Sheets(1)
Set wks1 = wkb.Sheets(2)
Set wks2 = wkb.Sheets(3)
emptyRow1 = False
i = 1
k = 1
' Loops while there is data on sheet 1 column 1
While emptyRow1 = False
    found = False
    ' extracts the name from column 1, row i
    name1 = wks.Cells(i, 1)
    If name1 <> "" Then
        'As it is not empty then loop through Sheet2 names
        emptyRow2 = False
        j = 1
        While emptyRow2 = False
            name2 = wks1.Cells(j, 1)
            If name2 <> "" Then
                'As name2 is not empty then compare name and name 2
                If name1 = name2 Then
                    'If name1 is the same as name2 then put the data on Sheet3
                    wks2.Cells(k, 1) = name1
                    wks2.Cells(k, 2) = wks.Cells(i, 2)
                    wks2.Cells(k, 3) = wks.Cells(i, 3)
                    wks2.Cells(k, 4) = wks1.Cells(j, 3)
                    found = True
                    k = k + 1
                    'stop looping sheet2 by setting emptyrow2 to True
                    emptyRow2 = True
                End If
            Else
                'name2 is empty, then stop looping sheet2
                emptyRow2 = True
            End If
            j = j + 1
        Wend
    Else
        'name1 is empty then stop looping Sheet1
        emptyRow1 = True
    End If
    'if after looping there was no coincidence of names then put the data from Sheet1
    If found = False Then
        If name1 <> "" Then
            wks2.Cells(k, 1) = name1
            wks2.Cells(k, 2) = wks.Cells(i, 2)
            wks2.Cells(k, 3) = wks.Cells(i, 3)
            k = k + 1
        End If
    End If
    i = i + 1
Wend
'This area loops through Sheet2 seeking for coincidences
emptyRow2 = False
j = 1
While emptyRow2 = False
    found = False
    name2 = wks1.Cells(j, 1)
    If name2 <> "" Then
        emptyRow1 = False
        i = 1
        While emptyRow1 = False
            name1 = wks.Cells(i, 1)
            If name1 <> "" Then
                If name2 = name1 Then
                    found = True
                    emptyRow1 = True
                End If
            Else
                emptyRow1 = True
            End If
            i = i + 1
        Wend
    Else
        emptyRow2 = True
    End If
    ' If there wasn't a coincidence it means this name is in sheet2 but is not on Sheet1 so, put it in Sheet3.
    If found = False Then
        wks2.Cells(k, 1) = name2
        wks2.Cells(k, 2) = wks1.Cells(j, 2)
        wks2.Cells(k, 4) = wks1.Cells(j, 3)
        k = k + 1
    End If
    j = j + 1
Wend
End Sub

它循环遍历工作表1穿上表3第四列是否与表2

之后循环表2穿上表3未找到行工作表1

相关内容