|
- Sub test1() ' 999帖 灌水
-
- Dim f As String
-
- ' With Application.FileDialog(msoFileDialogOpen)
- ' .InitialFileName = ThisWorkbook.Path
- ' With .Filters
- ' .Clear
- ' .Add "Excel File(s)", "*.xls*"
- ' End With
- ' .AllowMultiSelect = False
- ' If .Show Then f = .SelectedItems(1) Else Exit Sub
- ' End With
-
- f = ThisWorkbook.Path & "\一月账单.xlsx"
- If Dir(f) = "" Then MsgBox f & " 不存在!", 64: Exit Sub
-
- Application.ScreenUpdating = False
-
- 'Dim Conn As New ADODB.Connection, rs As New ADODB.Recordset
- Dim Conn As Object, rs As Object
- Dim strConn As String, SQL As String, s As String
- Dim A As String, B As String, C As String
- Dim ar, br, i As Long, j As Long, k As Long, n As Double
-
- Set Conn = CreateObject("ADODB.Connection")
- Set rs = CreateObject("ADODB.Recordset")
-
- s = "Excel 12.0;HDR=YES;Database="
- If Application.Version < 12 Then
- s = Replace(s, "12.0", "8.0")
- strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source="
- Else
- strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source="
- End If
- Conn.Open strConn & ThisWorkbook.FullName
-
- A = "SELECT * FROM [" & s & f & "].[Sheet1$A1:G] WHERE LEN(订单号) "
- With Sheet1
- i = .Cells(.Rows.Count, 1).End(xlUp).Row
- B = "SELECT * FROM [" & .Name & "$A1:G" & i & "]"
- C = "SELECT 订单号 FROM [" & s & f & "].[Sheet1$A1:A] WHERE LEN(订单号) UNION SELECT 订单号 FROM [" & .Name & "$A1:A" & i & "]"
- End With
-
- SQL = "SELECT a.*,b.* FROM ((" & C & ") c LEFT JOIN (" & A & ") a ON a.订单号=c.订单号) LEFT JOIN (" & B & ") b ON b.订单号=c.订单号"
-
- 'rs.Open SQL, Conn, adOpenKeyset, adLockOptimistic
- rs.Open SQL, Conn, 1, 3
-
- With Sheet2.Range("A1")
- .Resize(Rows.Count - .Row, 20).Clear
- For j = 0 To rs.Fields.Count - 1
- If j > 6 Then
- .Offset(0, j) = Replace(rs.Fields(j).Name, ".", "_")
- Else
- .Offset(0, j) = Mid(rs.Fields(j).Name, 3)
- End If
- Next
- .Offset(1).CopyFromRecordset rs
-
- With .CurrentRegion
- C = .Parent.Name & "$" & .Address(0, 0)
- ar = Application.Rept(.Rows(1).Offset(, 0).Resize(, 7).Value, 1)
- A = "SELECT `" & Join(ar, "`,`") & "` FROM [" & C & "] WHERE `" & .Cells(8).Value & "` IS NULL"
- br = Application.Rept(.Rows(1).Offset(, 7).Resize(, 7).Value, 1)
- B = "SELECT `" & Join(br, "`,`") & "` FROM [" & C & "] WHERE `" & .Cells(1).Value & "` IS NULL"
- ar = .Value
- End With
- End With
-
- k = 1
- ar(k, 8) = "增减"
- br = Split("增加 ,减少 ", ",")
- For i = 2 To UBound(ar)
- If Trim(ar(i, 1)) = Trim(ar(i, 8)) Then
- If Val(ar(i, 7)) - Val(ar(i, 14)) <> 0 Then
- k = k + 1
- ar(k, 1) = ar(i, 1)
- ar(k, 8) = vbNullString
- For j = 2 To 6
- ar(k, j) = ar(i, j + 7)
- n = Val(ar(i, j)) - Val(ar(i, j + 7))
- If n Then ar(k, 8) = ar(k, 8) & ar(1, j) & br(-CInt((n > 0)))
- Next
- ar(k, j) = ar(i, j + 7)
- End If
- End If
- Next
-
- With Sheet4
- .UsedRange.Offset(1).ClearContents
- .Range("A2").CopyFromRecordset Conn.Execute(A)
- End With
-
- With Sheet3
- .UsedRange.Offset(1).ClearContents
- .Range("A2").CopyFromRecordset Conn.Execute(B)
- End With
-
- With Sheet2
- .UsedRange.Offset(0).ClearContents
- .Range("A1").Resize(k, 8) = ar
- End With
-
- rs.Close
- Set rs = Nothing
- Conn.Close
- Set Conn = Nothing
-
- Application.ScreenUpdating = True
- Beep
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|