|
weekly.rar
(19.74 KB, 下载次数: 10)
Option Explicit
Sub A()
Dim cnn As Object
Dim Sql As String, ARR, i%
ARR = Sheet1.[A4].CurrentRegion
Set cnn = CreateObject("ADODB.CONNECTION")
cnn.Open "Provider=Microsoft.ACE.OleDb.12.0;Extended Properties='Excel 12.0;HDR=YES'; Data Source=" & ThisWorkbook.FullName
cnn.BeginTrans
For i = 2 To UBound(ARR)
Sql = "select count(*) from [Sheet2$a1:f] where Week='" & Sheet1.[b2] & "' and Name='" & ARR(i, 1) & "'"
If cnn.Execute(Sql)(0) = 0 Then
Sql = "INSERT INTO [Sheet2$] (Week,Name,Market,[Out],[IN],Stock) " _
& "VALUES('" & Sheet1.[b2] & "','" _
& "" & ARR(i, 1) & "'," _
& "'" & ARR(i, 2) & "'," _
& "'" & ARR(i, 5) & "'," _
& "'" & ARR(i, 8) & "'," _
& "'" & ARR(i, 10) & "')"
cnn.Execute Sql
Else
Sql = "UPDATE [Sheet2$] " _
& "SET Week='" & Sheet1.[b2] & "'," _
& "Name='" & ARR(i, 1) & "'," _
& "Market='" & ARR(i, 2) & "'," _
& "[Out]='" & ARR(i, 5) & "'," _
& "[IN]='" & ARR(i, 8) & "'," _
& "Stock='" & ARR(i, 10) & "'" _
& " where Week='" & Sheet1.[b2] & "' AND Name='" & ARR(i, 1) & "'"
cnn.Execute Sql
End If
Next
cnn.CommitTrans
Sheet2.Columns("D:D").TextToColumns Destination:=Range("D1"), DataType:=xlDelimited, TrailingMinusNumbers:=True
cnn.Close: Set cnn = Nothing
End Sub
|
|