我有一张工作表BF_UPLOAD
,其中有行。 我需要将其他两个工作表Diamond
和Variations
中的数据添加到BF_UpLoad 。需要连接两个输入工作表,两个工作表都没有所有需要的数据。
在将数据移至[Diamond$].name
工作表之前,可能更容易向Variations
工作表添加列BF_UpLoad
。
表格Diamond
和Variations
有一个共同密钥[Diamond$].Code
相当于[Varations$].sku
我认为这是正确的JOIN声明。
FROM [Diamond$] INNER JOIN [Variations$] ON [DIamond$].code = [Varations$].sku
要在BF_UpLoad
底部添加记录,我相信我需要使用:
INSERT INTO statement
我正在处理的代码包含在此帖子的底部。该代码适用于sku,regular_price
和sale_price
字段。
我找不到从两张纸上选择数据的方法,更不用说让JOIN工作了。
我在下面列出了源数据以及所需输出的样本。
工作表:
BF_UpLoad 包含以下列:
sku, post_title, regular_price, sale_price
变体包含以下列,这些列不是连续的:
sku, regular_price, sale Price
Diamond 包含以下列,这些列不是连续的:
Code, Name
在钻石表CODE
上等于其他工作表上的sku列。
纸张中的期望输出:BF_UpLoad
工作表:BF_UpLoad:列不连续
sku post_title regular_price sale_price
ABT00201 BioCare, Fly Trap 11.81 10.69
ABT00204 BioCare,Moth Trap 4.95 9.06
ABT00415 BioCare,Fruit Fly Trap 6.9 12.63
AB00017 Acrobird Playland 20" 59.81 56.41
AB00018 Acrobird Playland 24" 79.78 73.24
AB00021 Acrobird Playland 14" 30.84 24.35
输入工作表
工作表:钻石:列不连续
sku Name
AB00017 Acrobird Playland 20"
AB00018 Acrobird Playland 24"
AB00021 Acrobird Toddler Playland 14"
工作表:变体:列不连续
sku, regular_price, sale_price
AB00017 59.81 56.41
AB00018 79.78 73.24
AB00021 30.84 24.35
这是我正在尝试的代码:
Private Sub cmbVariations2BF_UpLoad_Click()
Dim objConnection As ADODB.Connection
Dim objRecordSet As ADODB.Recordset
Dim i As Integer
Dim strSQL As String
Dim strConn As String
Dim strTarget As String
Dim hdrName As Variant
Dim wksName As Worksheet
Dim wksOutput As Worksheet
Dim cnn As ADODB.Connection
Dim strConnectionString As String
Dim sh As Worksheet
Dim strInsert As String
Dim lngRow As Long
Dim LastUsedCell As Integer
Set sh = Sheets("Variations")
strTarget = "BF_UpLoad"
pubBF_UpLoadRows = Sheets(strTarget).Cells(Rows.Count, 1).End(xlUp).Row
Set objConnection = New ADODB.Connection
Set objRecordSet = New ADODB.Recordset
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & ActiveWorkbook.FullName & ";" & _
"Extended Properties=""Excel 12.0;HDR=Yes;"";"
objConnection.Open strConn
objConnection.Execute "Insert Into [BF_UpLoad$] (sku, regular_price, Sale_price) SELECT sku, regular_price, Sale_price from [Variations$]"
End Sub
感谢您的投入,CraigM
答案 0 :(得分:1)
根据您的描述,SQl将是这样的:
objConnection.Execute "Insert Into [BF_UpLoad$] (sku, post_title, regular_price, Sale_price) SELECT V.sku, D.[Name], V.regular_price, V.Sale_price from [Variations$] V, [Diamond$] D WHERE D.[Code] = V.[sku]"