1
我想加入2個不同的工作表中的2個獨立的列,使一個更長的列,然後我可以使用Vlookup。Excel VBA/SQL聯盟
Sheet 1中 A,B,C,d,E,F,G
Sheet 2中 A,B,C,d,E,F,G
欲加入(聯盟)色譜柱B從sheet1和C從sheet2一起找到新列表的Distinct值。我一直在爲此工作數週。
謝謝
我想加入2個不同的工作表中的2個獨立的列,使一個更長的列,然後我可以使用Vlookup。Excel VBA/SQL聯盟
Sheet 1中 A,B,C,d,E,F,G
Sheet 2中 A,B,C,d,E,F,G
欲加入(聯盟)色譜柱B從sheet1和C從sheet2一起找到新列表的Distinct值。我一直在爲此工作數週。
謝謝
您可以在Excel中使用ADO。
Dim cn As Object
Dim rs As Object
Dim strFile As String
Dim strCon As String
Dim strSQL As String
Dim s As String
Dim i As Integer, j As Integer
''This is not the best way to refer to the workbook
''you want, but it is very conveient for notes
''It is probably best to use the name of the workbook.
strFile = ActiveWorkbook.FullName
''Note that if HDR=No, F1,F2 etc are used for column names,
''if HDR=Yes, the names in the first row of the range
''can be used.
''This is the Jet 4 connection string, you can get more
''here : http://www.connectionstrings.com/excel
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
& ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
''Late binding, so no reference is needed
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open strCon
''A sample query
strSQL = "SELECT Distinct A, B C FROM (" _
& "SELECT A, B, C " _
& "FROM [Sheet1$] " _
& "UNION ALL " _
& "SELECT A, B, C " _
& "FROM [Sheet2$]) As J "
''Open the recordset for more processing
''Cursor Type: 3, adOpenStatic
''Lock Type: 3, adLockOptimistic
''Not everything can be done with every cirsor type and
''lock type. See http://www.w3schools.com/ado/met_rs_open.asp
rs.Open strSQL, cn, 3, 3
''Write out the data to an empty sheet (no headers)
Worksheets("Sheet3").Cells(2, 1).CopyFromRecordset rss
完美的作品。還有一件事。在Sheet1上,前兩行是標題信息,而Sheet2前四個是標題信息。有關如何排除這些的任何建議? – Edge 2010-06-15 14:53:17
HDR =是在連接字符串中將允許一個標題行,這可能不適合,所以也許更改爲HDR =否,並向表單中添加一個範圍,例如「FROM [Sheet1 $ A3:C102]」。當HDR = No時,您必須使用F1,F2 ... Fn – Fionnuala 2010-06-15 15:39:04
PERFECT參考列。非常感謝你 – Edge 2010-06-15 15:51:12