你知道如何用外连接连接两个表吗?知道在 SQL 中执行此操作,但我现在需要 Excel。
我在一个列中有一个所有员工的列表我有一个每个员工的任务表。我想创建一个函数,该函数将用缺少的员工填充此表。
表 1 - 所有员工名单
费尔南多 赫克托 薇薇安 伊万
表 2 - 实际任务列表
费尔南多,任务 A,5 小时 Vivian,任务 B,8 小时
我想达到的结果
费尔南多,任务 A,5 小时 Vivian,任务 B,8 小时 赫克托, , 0 小时 伊万, , 0 小时
非常感谢您的任何想法。
你知道如何用外连接连接两个表吗?知道在 SQL 中执行此操作,但我现在需要 Excel。
我在一个列中有一个所有员工的列表我有一个每个员工的任务表。我想创建一个函数,该函数将用缺少的员工填充此表。
表 1 - 所有员工名单
费尔南多 赫克托 薇薇安 伊万
表 2 - 实际任务列表
费尔南多,任务 A,5 小时 Vivian,任务 B,8 小时
我想达到的结果
费尔南多,任务 A,5 小时 Vivian,任务 B,8 小时 赫克托, , 0 小时 伊万, , 0 小时
非常感谢您的任何想法。
如果您使用 ADO,Excel 中确实存在左连接之类的东西。
转到 VBA 编辑器 (Alt-F11) 并添加对“Microsoft ActiveX 数据对象 2.8 库”的引用(工具 > 引用)。创建一个新的普通模块(插入>模块)并添加以下代码:
Option Explicit
Sub get_employees()
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
' This is the Excel 97-2003 connection string. It should also work with
' Excel 2007 onwards worksheets as long as they have less than 65536
' rows
'With cn
' .Provider = "Microsoft.Jet.OLEDB.4.0"
' .ConnectionString = "Data Source=" & ThisWorkbook.FullName & ";" & _
' "Extended Properties=Excel 8.0;"
' .Open
'End With
With cn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=" & ThisWorkbook.FullName & ";" & _
"Extended Properties=""Excel 12.0 Macro;IMEX=1;HDR=YES"";"
.Open
End With
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
rs.Open "SELECT * FROM [Sheet1$] LEFT JOIN [Sheet2$] ON [Sheet1$].[EMPLOYEE] = " & _
"[Sheet2$].[EMPLOYEE]", cn
Dim fld As ADODB.Field
Dim i As Integer
With ThisWorkbook.Worksheets("Sheet3")
.UsedRange.ClearContents
i = 0
For Each fld In rs.Fields
i = i + 1
.Cells(1, i).Value = fld.Name
Next fld
.Cells(2, 1).CopyFromRecordset rs
.UsedRange.Columns.AutoFit
End With
rs.Close
cn.Close
End Sub
保存工作簿,然后运行代码,您应该会在 Sheet3 上获得一个左连接列表。您会看到 Employee 列是重复的,但是您可以通过适当地修改 SELECT 子句来对其进行排序。您还将有空白单元格,而不是没有匹配的 0 小时
编辑:我在代码注释中留下了 Excel 97-2003 连接字符串的详细信息,但已将代码更改为使用 Excel 2007 及更高版本的连接字符串。我还添加了代码来输出字段名称并在输出记录集后自动调整列
简单的方法(也许是唯一的方法?)将是中间单元:
在“结果”工作表中:
A, B, C D
fernando, vlookup(...), vlookup(...), =if(ISNA(B2),"<default-1>"), =if(ISNA(B2),"deafult2)
然后隐藏列 C 和 B
编辑:
实际上有一些接近的东西:Pivot Table。您可以以非引用单元格保持空白的方式组织数据。
但这是与公式不同的解决方案 - 它可能不适合,取决于您的使用情况。
This method does copy and pasting, filtering, and sorting to accomplish an outer join in Excel and good for just one offs. The idea is to use a VLOOKUP to find all matching records from the left to the right tables and right to the left tables. [Adding another record to table 2 to show outer join]
Table 1
Fernando
Hector
Vivian
Ivan
Table 2
Fernando, task A, 5 hours
Vivian, task B, 8 hours
Thomas, task A, 5 hours
Copy both tables into one table where table 1 will take up the first left columns and first rows and table 2 will take up the last right columns and last rows (The headers should be row 1 for both tables). Create a VLOOKUP function for the next two columns to find matching keys from the left to right tables and right to left tables.
Table 3
Name Name Task Hours Match 1 Match 2
Fernando =VLOOKUP(A2,B:B,1,FALSE) =VLOOKUP(B2,A:A,1,FALSE)
Hector =VLOOKUP(A3,B:B,1,FALSE) =VLOOKUP(B3,A:A,1,FALSE)
Vivian =VLOOKUP(A4,B:B,1,FALSE) =VLOOKUP(B4,A:A,1,FALSE)
Ivan =VLOOKUP(A5,B:B,1,FALSE) =VLOOKUP(B5,A:A,1,FALSE)
Fernando task A 5 hours =VLOOKUP(A6,B:B,1,FALSE) =VLOOKUP(B6,A:A,1,FALSE)
Vivian task B 8 hours =VLOOKUP(A7,B:B,1,FALSE) =VLOOKUP(B7,A:A,1,FALSE)
Thomas task B 8 hours =VLOOKUP(A8,B:B,1,FALSE) =VLOOKUP(B8,A:A,1,FALSE)
Table 3 Result
Name Name Task Hours Match 1 Match 2
Fernando Fernando N/A
Hector N/A N/A
Vivian Vivian N/A
Ivan N/A N/A
Fernando task A 5 hours N/A Fernando
Vivian task B 8 hours N/A Vivian
Thomas task B 8 hours N/A N/A
NOTE: For large data sets, the next step will take a very long time because of the VLOOKUP calculation that happens. Copy and paste over columns for match 1 and match 2 columns as values so that VLOOKUP doesn't recalculate during filtering.
Filter on Match 1 and Match 2 to only see all N/A results. Copy the main data to another sheet with headers.
Name Name Task Hours Match 1 Match 2
Hector N/A N/A
Ivan N/A N/A
Thomas task B 8 hours N/A N/A
Filter on Match 1 and Match 2 to not see N/A results. Sort on keys for both, so that when copy and pasting everything matches. Copy and paste Table 1 data into the new sheet below previously pasted data first. Then copy and paste Table 2 data to the right of the Table 1 data that was just pasted.
Name Name Task Hours Match 1 Match 2
Fernando Fernando N/A
Vivian Vivian N/A
Fernando task A 5 hours N/A Fernando
Vivian task B 8 hours N/A Vivian
The result is below and you can delete, sort, whatever to the outer joined data.
Name Name Task Hours
Hector
Ivan
Thomas task B 8 hours
Fernando Fernando task A 5 hours
Vivian Vivian task B 8 hours