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. 【GoLang】golang 闭包 closure 参数传递的蹊跷!

    结论: 闭包函数可以直接引用外层代码定义的变量, 但是,注意,闭包函数里面引用的是变量的地址, 当goroutine被调度时,改地址的值才会被传递给goroutine 函数. 介绍 go的闭包是一个很 ...

  2. POI导出excel日期格式

    参考帖子: [1]http://www.ithao123.cn/content-2028409.html [2]http://javacrazyer.iteye.com/blog/894850 再读本 ...

  3. struts2 自定义校验规则

    自定义校验规则:(了解) 在Struts2自定义校验规则: 1.实现一个Validator 接口. 2.一般开发中继承ValidatorSupport 或者 FieldValidatorSupport ...

  4. asp.net mvc 多级文件夹

    ASP.NET MVC - 定制属于你自己的ViewEngine 标签: asp.netmvcstring引擎razorapplication 2012-07-26 14:17 5365人阅读 评论( ...

  5. .NET LINQ 数据分组

    数据分组      分组指将数据放入组中以便每个组中的元素共享公共特性的操作.   方法 方法名 说明 C# 查询表达式语法 Visual Basic 查询表达式语法 更多信息 GroupBy 对共享 ...

  6. 低功耗蓝牙BLE之连接事件、连接参数和更新方法

    转自:http://blog.csdn.net/zzfenglin/article/details/51304084 连接事件 在一个连接当中,主设备会在每个连接事件里向从设备发送数据包.一个连接事件 ...

  7. MyBatis学习(二)

    mybatis开发dao的方法 作用范围 SqlSessionFactoryBuilder是以工具类方式来使用,需要创建sqlSessionFactory就new一个SqlSessionFactory ...

  8. VS2015 自动添加头部注释

    让VS自动生成类的头部注释,只需修改两个文集即可,一下两个路径下个有一个 Class.cs文件 D:\Program Files (x86)\Microsoft Visual Studio 14.0\ ...

  9. Python学习笔记(一)——环境搭建

    一.安装包下载: 国内镜像:32位:http://pan.baidu.com/s/1jI4q4lS        64位:http://pan.baidu.com/s/1eRPhpRW 版本更迭速度很 ...

  10. HDU 2082 找单词 (多重背包)

    题意:假设有x1个字母A, x2个字母B,..... x26个字母Z,同时假设字母A的价值为1,字母B的价值为2,..... 字母Z的价值为26.那么,对于给定的字母,可以找到多少价值<=50的 ...