为 3 个电子表格中不匹配的特定行 ID 创建 table 输出
Create table output for specific row ID's that don't match in 3 spreadsheets
我想创建一个 VBA 脚本,该脚本将输出一个电子表格,其中包含所有 3 个 table 中的列填充的行,其中每个电子表格 table 中的指定行 ID彼此不匹配(所有 3 个)。因此,如果名字和姓氏是 ID 的
Table 1
Name | Surname | Age | Date | Bank Account No.
John | Marley | 23 | 21/07/19 | 25511581125
Simon | Harvey | 22 | 04/03/19 | 25513321125
Table 2
Name | Surname | Age | Date | Gender
John | Marley | 23 | 21/07/19 | Male
Simon | Harvey | 22 | 04/03/19 | Male
Table 3
Name | Surname | Age | Date | Height
John | Marley | 23 | 21/07/19 | 5ft
Simon | John | 18 | 21/07/19 | 2ft
Output
Name | Surname | Age | Date | Bank Account No.
Simon | Harvey | 22 | 04/03/19 | 25513321125
Name | Surname | Age | Date | Gender
Simon | Harvey | 22 | 04/03/19 | Male
Name | Surname | Age | Date | Height
Simon | John | 18 | 21/07/19 | 2ft
所以我想要的是每个 table 的每一行的输出,其中行中指定的 ID 在所有 3 个 table 中都找不到。我希望这是有道理的。指定的 ID 必须恰好出现在所有 3 table 中。有一个输出将保留一个列,其中所有 3 table 的确切名称是相同的将是理想的但不需要。
我将 ADODB 添加为标签的原因是我知道这将是一个更简单、更有效的解决方案?
我知道删除与所有 3 个匹配的那些可能会更容易?或突出显示它们或其他内容。
如果有人对我应该如何完成这个有任何理论,我很想听听。谢谢!
尝试:
Option Explicit
Sub test()
Dim rng1 As Range, rng2 As Range, rng3 As Range, cell1 As Range, cell2 As Range, cell3 As Range
Dim ID As String
Dim LastRowG As Long, Times As Long
With ThisWorkbook.Worksheets("Sheet1")
Set rng1 = .Range("F3:F5")
Set rng2 = .Range("F9:F11")
Set rng3 = .Range("F15:F17")
Times = 0
For Each cell1 In rng1
ID = cell1.Value
If Application.WorksheetFunction.CountIf(rng2, ID) + Application.WorksheetFunction.CountIf(rng3, ID) < 2 Then
Times = Times + 1
LastRowG = .Cells(.Rows.Count, "H").End(xlUp).Row
If Times = 1 Then
.Range("H" & LastRowG + 1).Value = "Name"
.Range("I" & LastRowG + 1).Value = "Surname"
.Range("J" & LastRowG + 1).Value = "Age"
.Range("K" & LastRowG + 1).Value = "Date"
.Range("L" & LastRowG + 1).Value = "Bank Account No."
.Range("A" & cell1.Row & ":E" & cell1.Row).Copy .Range("H" & LastRowG + 2)
Else
.Range("A" & cell1.Row & ":E" & cell1.Row).Copy .Range("H" & LastRowG + 1)
End If
End If
Next cell1
Times = 0
For Each cell2 In rng2
ID = cell2.Value
If Application.WorksheetFunction.CountIf(rng1, ID) + Application.WorksheetFunction.CountIf(rng3, ID) < 2 Then
Times = Times + 1
LastRowG = .Cells(.Rows.Count, "H").End(xlUp).Row
If Times = 1 Then
.Range("H" & LastRowG + 2).Value = "Name"
.Range("I" & LastRowG + 2).Value = "Surname"
.Range("J" & LastRowG + 2).Value = "Age"
.Range("K" & LastRowG + 2).Value = "Date"
.Range("L" & LastRowG + 2).Value = "Gender"
.Range("A" & cell2.Row & ":E" & cell2.Row).Copy .Range("H" & LastRowG + 3)
Else
.Range("A" & cell2.Row & ":E" & cell2.Row).Copy .Range("H" & LastRowG + 1)
End If
End If
Next cell2
Times = 0
For Each cell3 In rng3
ID = cell3.Value
If Application.WorksheetFunction.CountIf(rng2, ID) + Application.WorksheetFunction.CountIf(rng1, ID) < 2 Then
Times = Times + 1
LastRowG = .Cells(.Rows.Count, "H").End(xlUp).Row
If Times = 1 Then
.Range("H" & LastRowG + 2).Value = "Name"
.Range("I" & LastRowG + 2).Value = "Surname"
.Range("J" & LastRowG + 2).Value = "Age"
.Range("K" & LastRowG + 2).Value = "Date"
.Range("L" & LastRowG + 2).Value = "Height"
.Range("A" & cell3.Row & ":E" & cell3.Row).Copy .Range("H" & LastRowG + 3)
Else
.Range("A" & cell3.Row & ":E" & cell3.Row).Copy .Range("H" & LastRowG + 1)
End If
End If
Next cell3
End With
End Sub
结果:
我想创建一个 VBA 脚本,该脚本将输出一个电子表格,其中包含所有 3 个 table 中的列填充的行,其中每个电子表格 table 中的指定行 ID彼此不匹配(所有 3 个)。因此,如果名字和姓氏是 ID 的
Table 1
Name | Surname | Age | Date | Bank Account No.
John | Marley | 23 | 21/07/19 | 25511581125
Simon | Harvey | 22 | 04/03/19 | 25513321125
Table 2
Name | Surname | Age | Date | Gender
John | Marley | 23 | 21/07/19 | Male
Simon | Harvey | 22 | 04/03/19 | Male
Table 3
Name | Surname | Age | Date | Height
John | Marley | 23 | 21/07/19 | 5ft
Simon | John | 18 | 21/07/19 | 2ft
Output
Name | Surname | Age | Date | Bank Account No.
Simon | Harvey | 22 | 04/03/19 | 25513321125
Name | Surname | Age | Date | Gender
Simon | Harvey | 22 | 04/03/19 | Male
Name | Surname | Age | Date | Height
Simon | John | 18 | 21/07/19 | 2ft
所以我想要的是每个 table 的每一行的输出,其中行中指定的 ID 在所有 3 个 table 中都找不到。我希望这是有道理的。指定的 ID 必须恰好出现在所有 3 table 中。有一个输出将保留一个列,其中所有 3 table 的确切名称是相同的将是理想的但不需要。
我将 ADODB 添加为标签的原因是我知道这将是一个更简单、更有效的解决方案?
我知道删除与所有 3 个匹配的那些可能会更容易?或突出显示它们或其他内容。
如果有人对我应该如何完成这个有任何理论,我很想听听。谢谢!
尝试:
Option Explicit
Sub test()
Dim rng1 As Range, rng2 As Range, rng3 As Range, cell1 As Range, cell2 As Range, cell3 As Range
Dim ID As String
Dim LastRowG As Long, Times As Long
With ThisWorkbook.Worksheets("Sheet1")
Set rng1 = .Range("F3:F5")
Set rng2 = .Range("F9:F11")
Set rng3 = .Range("F15:F17")
Times = 0
For Each cell1 In rng1
ID = cell1.Value
If Application.WorksheetFunction.CountIf(rng2, ID) + Application.WorksheetFunction.CountIf(rng3, ID) < 2 Then
Times = Times + 1
LastRowG = .Cells(.Rows.Count, "H").End(xlUp).Row
If Times = 1 Then
.Range("H" & LastRowG + 1).Value = "Name"
.Range("I" & LastRowG + 1).Value = "Surname"
.Range("J" & LastRowG + 1).Value = "Age"
.Range("K" & LastRowG + 1).Value = "Date"
.Range("L" & LastRowG + 1).Value = "Bank Account No."
.Range("A" & cell1.Row & ":E" & cell1.Row).Copy .Range("H" & LastRowG + 2)
Else
.Range("A" & cell1.Row & ":E" & cell1.Row).Copy .Range("H" & LastRowG + 1)
End If
End If
Next cell1
Times = 0
For Each cell2 In rng2
ID = cell2.Value
If Application.WorksheetFunction.CountIf(rng1, ID) + Application.WorksheetFunction.CountIf(rng3, ID) < 2 Then
Times = Times + 1
LastRowG = .Cells(.Rows.Count, "H").End(xlUp).Row
If Times = 1 Then
.Range("H" & LastRowG + 2).Value = "Name"
.Range("I" & LastRowG + 2).Value = "Surname"
.Range("J" & LastRowG + 2).Value = "Age"
.Range("K" & LastRowG + 2).Value = "Date"
.Range("L" & LastRowG + 2).Value = "Gender"
.Range("A" & cell2.Row & ":E" & cell2.Row).Copy .Range("H" & LastRowG + 3)
Else
.Range("A" & cell2.Row & ":E" & cell2.Row).Copy .Range("H" & LastRowG + 1)
End If
End If
Next cell2
Times = 0
For Each cell3 In rng3
ID = cell3.Value
If Application.WorksheetFunction.CountIf(rng2, ID) + Application.WorksheetFunction.CountIf(rng1, ID) < 2 Then
Times = Times + 1
LastRowG = .Cells(.Rows.Count, "H").End(xlUp).Row
If Times = 1 Then
.Range("H" & LastRowG + 2).Value = "Name"
.Range("I" & LastRowG + 2).Value = "Surname"
.Range("J" & LastRowG + 2).Value = "Age"
.Range("K" & LastRowG + 2).Value = "Date"
.Range("L" & LastRowG + 2).Value = "Height"
.Range("A" & cell3.Row & ":E" & cell3.Row).Copy .Range("H" & LastRowG + 3)
Else
.Range("A" & cell3.Row & ":E" & cell3.Row).Copy .Range("H" & LastRowG + 1)
End If
End If
Next cell3
End With
End Sub
结果: