gpt4 book ai didi

sql - Excel VBA/SQL 联合

转载 作者:行者123 更新时间:2023-12-02 08:58:28 25 4
gpt4 key购买 nike

我正在尝试从 2 个不同的工作表中加入 2 个单独的列以制作一个更长的列,然后我可以从中使用 Vlookup。

表 1
A、B、C、D、E、F、G

表 2
A、B、C、D、E、F、G

我想将 sheet1 中的 B 列和 sheet2 中的 C 列连接(联合)并找到新列表的不同值。我已经为此工作了好几个星期。

谢谢

最佳答案

您可以将 ADO 与 Excel 一起使用。

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

关于sql - Excel VBA/SQL 联合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3040508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com