Set up the Workbook

In this example, there are two tables -- Raw Materials and Packaging -- and each table is on a separate worksheet.

The Raw Materials table is on the sheet named Materials, and the Packaging table is on the sheet named Packaging.

The third sheet in the workbook is named Combined, and this is where the query results will be stored.

With Microsoft Query, you can create a list that combines each item in one table, with all the items in the other table -- a Cartesian join, also called a cross join. You can read more about the different join types on the Microsoft website: Description of the usage of joins in Microsoft Query

Open MS Query

To create the Cartesian (cross) join, you'll use MS Query.

  1. On the Excel Ribbon, click the Data tab
  2. In the Get External Data group, click From Other Sources, then click From Microsoft Query

  3. In the Choose Data Source window, click on Excel Files*, and click OK

  4. In the Select Workbook window, locate and select the current workbook, and click OK.

  5. In the Query Wizard, if you don't see the sheet names listed, click Options, and add a check mark to System Tables

  6. Click Materials$, and click the arrow to put the Raw Materials column in the query.
  7. Click Packaging$, and click the arrow to put the Packaging column in the query

  8. Click Cancel, to close the Query Wizard, and click Yes when prompted.

  9. In Microsoft Query, double-click on Raw Materials in the Materials$ table, to add it to the query grid.
  10. Then, double-click on Packaging, to add it to the query grid.

  11. Click the Return Data button, to send the data to Excel.

Create a Worksheet Table

It might take a few seconds, but then the Import Data window will open.

  1. In the Import Data window, select Table
  2. Select the cell on the worksheet where you want to place the query results, and click OK.

  3. A table is created, and shows all the items from each table, in all possible combinations.

Add Formulas to the Table

You can add formulas to the table, in a new column. The formulas will automatically adjust if the source tables are changed.

  1. Type a new heading in cell C1 -- MatPack -- and press Enter
  2. A new column is automatically included in the table.
  3. In cell C2, type a formula to combine the text in columns A and B, with a space character between them:

    =[@[Raw Materials]] & " " & [@Packaging]

  4. Then, copy the formula down to the last row of data in the table.

Update the Table

You can update the source tables, and then update the query results table, to show the revised data.

  1. Add a new item to each of the source tables. In this example, Boxes was added to the Packaging list, and Cream was added to the Raw Materials list.

  2. On the Excel Ribbon, click the Data tab, and click Refresh All.

  3. The new items are shown in the updated query results table.

Manually Update the Workbook Name

If you change the workbook name, the query will need to be updated, before it will run. To manually update the query:

  1. Right-click a cell in the results table, and click Refresh
  2. When the Login Failed message appears, click OK

  3. In the Select Workbook window, locate and select the new workbook, and click OK.

Update the Query Connection with VBA

If the file name or location will change frequently, you can use programming to automatically change the file location in the connection.

Paste the following code -- FixQueryConnection -- into a regular module in the workbook, and then run the code when the workbook opens, by adding a Workbook_Open event.

This code was tested in Excel 2010 (32-bit), and might need to be adjusted for other versions of Excel.

Sub FixQueryConnection()
'
Dim strFile As String
Dim strPath As String
Dim strQry As String
Dim strCmd As String
Dim strConn As String strPath = ActiveWorkbook.Path & "\"
strFile = ActiveWorkbook.Name
strQry = "Query from Excel Files"
strCmd = "SELECT `Materials$`.`Raw Materials`, `Packaging$`.Packaging "
strCmd = strCmd & "FROM `Materials$` `Materials$`, `Packaging$` `Packaging$`"
strConn = "ODBC;DSN=Excel Files;DBQ=" & strPath & strFile
strConn = strConn & ";DefaultDir=" & strPath
strConn = strConn & ";DriverId=790;MaxBufferSize=2048;PageTimeout=5;"
'
With ActiveWorkbook.Connections(strQry).ODBCConnection
.BackgroundQuery = True
.CommandText = strCmd
.CommandType = xlCmdSql
.Connection = strConn
.RefreshOnFileOpen = False
.SavePassword = False
.SourceConnectionFile = ""
.SourceDataFile = ""
.ServerCredentialsMethod = xlCredentialsMethodIntegrated
.AlwaysUseConnectionFile = False
End With
With ActiveWorkbook.Connections(strQry)
.Name = strQry
.Description = ""
End With
ActiveWorkbook.Connections(strQry).Refresh ActiveWorkbook.RefreshAll
End Sub
'====================================

Put this code into the ThisWorkbook module:

Private Sub Workbook_Open()
FixQueryConnection
End Sub
'====================================
Search Contextures Sites

 Get Excel News

Name:

 
Email:

 
 

Copyright © Contextures Inc. 2016

Cross join in excel --- Copy from Internet的更多相关文章

  1. SQL中inner join、outer join和cross join的区别

    对于SQL中inner join.outer join和cross join的区别简介:现有两张表,Table A 是左边的表.Table B 是右边的表.其各有四条记录,其中有两条记录name是相同 ...

  2. CROSS JOIN连接用于生成两张表的笛卡尔集

    将两张表的情况全部列举出来 结果表: 列= 原表列数相加 行= 原表行数相乘     CROSS JOIN连接用于生成两张表的笛卡尔集. 在sql中cross join的使用: 1.返回的记录数为两个 ...

  3. left join ,right join ,inner join ,cross join 区别

    left join ,right join ,inner join ,cross join 区别(参考:http://zhidao.baidu.com/link?url=gpOl9HXZR0OrQuy ...

  4. MySQL的几种连接 join/inner join/cross join/逗号/left join/right join/natural join

    转载请注明出处!! 之前数据表连接操作多使用逗号或者join,对几种连接的概念一直浑浑噩噩,最近研究了一波,把这些连接的区别搞明白了.   连接:A xjoin B(主表 操作 关联表)  selec ...

  5. 用实例展示left Join,right join,inner join,join,cross join,union 的区别

    1.向TI,T2插入数据: T1  7条 ID Field2 Field3 Field41 1 3 542 1 3 543 1 3 544 2 3 545 3 3 546 4 3 547 5 3 54 ...

  6. Linq表连接大全(INNER JOIN、LEFT OUTER JOIN、RIGHT OUTER JOIN、FULL OUTER JOIN、CROSS JOIN)

    我们知道在SQL中一共有五种JOIN操作:INNER JOIN.LEFT OUTER JOIN.RIGHT OUTER JOIN.FULL OUTER JOIN.CROSS JOIN 1>先创建 ...

  7. FULL JOIN 与 CROSS JOIN

    FULL JOIN 只要其中某个表存在匹配,FULL JOIN 关键字就会返回行.(返回JOIN 两端表的所有数据,无论其与另一张表有没有匹配.显示左连接.右连接和内连接的并集) FULL JOIN ...

  8. SQL表连接查询(inner join(join)、full join、left join、right join、cross join)

    下面列出了您可以使用的 JOIN 类型,以及它们之间的差异. JOIN: 如果表中有至少一个匹配,则返回行(join=inner join) LEFT JOIN: 即使右表中没有匹配,也从左表返回所有 ...

  9. CROSS JOIN,NATURAL JOIN

    CROSS JOIN:笛卡尔积 NATURAL JOIN:

随机推荐

  1. [教程] 【玩转终端1:apt-get】

    进来工作比较清闲,所以写点东西,给喜欢折腾的朋友.本文及后面将要介绍的一些终端命令,其实对于玩过linux的人来说,是很基础的东西,我可能是班门弄斧了(拍砖的请轻点,有愿意补充/纠正的,本人求知不得) ...

  2. Httpsqs队列基本操作

    一,安装 ulimit -SHn 65535 wget http://httpsqs.googlecode.com/files/libevent-2.0.12-stable.tar.gz tar zx ...

  3. vim 用法

    据说,会使用vim之后,你就不想用其他文本编辑器了...反正我是还没有达到那水平 = = 好啦~不扯蛋,进入正题!(以后这个vim 会不断补充资料的 > <) 简单介绍下~ 一.安装vim ...

  4. [Android Pro] PullToRefreshListView怎么设置各个item之间的间距

    reference to :  http://blog.csdn.net/qq_25943493/article/details/50923895 要设置第三方的上拉下载listView的item之间 ...

  5. malloc与new的区别

    1.new是运算符,而malloc是库函数 2.new可以重载,可以自定义内存分配策略,甚至不做内存分配,甚至分配到非内存设备上.而malloc不能. 3.new在用于定义一个新的非内部对象的时候,默 ...

  6. JAVA Day8

    1. 引用数据类型需要new 2. 字符串使用的3种方式 String s = "hello world"; String s = new String(); String s = ...

  7. cdn

    cdn内容分发网络: 1. 内容缓存Web cache技术,反向代理 2. 集群服务与负载均衡技术 LVS(四层) 与 nginx(七层) 3. 全局负载均衡工作原理:基于DNS解析的GSLB实现机制 ...

  8. sublime text3点击ctrl+B无法运行Python程序?

    1.打开sublime text 3 ,选择 tools-->Build System-->New Build System.... 2.将下面代码块复制进新文件中,并命名为Python. ...

  9. CSS3绘制404页面

    标题有点噱了...最近在做一个交通有关的项目, 想做一个类似标志牌的404, 所以就有了这个. 只用的CSS3中的旋转, 效果如下 上代码: <!DOCTYPE html> <htm ...

  10. Leetcode N-Queens

    The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...