|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub a()
Dim cnn As New cConnection, sql$, s$, bt$
Dim rs As New cRecordset, i%, j%
If [A2] = "" Then Call b
Dim arr
arr = [A1].CurrentRegion
For i = 1 To UBound(arr, 2)
s = s & "[" & arr(1, i) & "],"
Next
s = s & " primary key([采购单创建日期],[请购/委外单号],[单号])"
sql = "CREATE TABLE T1(" & s & ")"
cnn.CreateNewDB ":memory:"
cnn.Execute sql
cnn.BeginTrans
s = ""
For i = 2 To UBound(arr)
For j = 1 To UBound(arr, 2)
s = s & MYV(arr(i, j)) & ","
Next
s = Left(s, Len(s) - 1)
sql = "INSERT INTO T1 VALUES(" & s & ")"
cnn.Execute sql
s = ""
Next
arr = Sheet1.[A1].CurrentRegion
For j = 1 To UBound(arr, 2)
bt = bt & "[" & arr(1, j) & "],"
Next
bt = Left(bt, Len(bt) - 1)
s = ""
For i = 2 To UBound(arr)
sql = "SELECT * FROM T1 WHERE [采购单创建日期]=" & MYV(arr(i, 1)) _
& " AND [请购/委外单号]=" & MYV(arr(i, 2)) _
& " AND [单号]=" & MYV(arr(i, 3))
rs.OpenRecordset sql, cnn
If rs.RecordCount = 0 Then
s = ""
For j = 1 To UBound(arr, 2)
s = s & MYV(arr(i, j)) & ","
Next
s = Left(s, Len(s) - 1)
sql = "REPLACE INTO T1(" & bt & ") VALUES(" & s & ")"
cnn.Execute sql
s = ""
Else
s = ""
For j = 4 To UBound(arr, 2)
s = s & "[" & arr(1, j) & "]=" & MYV(arr(i, j)) & ","
Next
s = Left(s, Len(s) - 1)
sql = ""
sql = "UPDATE T1 SET " & s
cnn.Execute sql
End If
Next
cnn.CommitTrans
[A2:Y9999] = ""
sql = "SELECT * FROM T1"
rs.OpenRecordset sql, cnn
[A2].CopyFromRecordset rs.DataSource
Set rs = Nothing
Set cnn = Nothing
End Sub
Sub b()
Dim r
r = Sheet1.[a99999].End(3).Row
Sheet1.Range("a2:v" & r).Copy [A2]
End Sub
Public Function MYV(myt)
If Trim(Len(myt)) = 0 Then
MYV = "''"
ElseIf TypeName(myt) = "String" Then
MYV = "'" & myt & "'"
ElseIf IsDate(myt) Then
MYV = "'" & Format(myt, "YYYY-MM-DD") & "'"
Else
MYV = myt
End If
End Function
|
评分
-
1
查看全部评分
-
|