|
Sub 生成对账单()
t = Time
Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet, i, j, cnstr, m, n
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cnstr = "provider=microsoft.ace.oledb.12.0;extended properties='excel 12.0;HDR=yes';data source=" & ThisWorkbook.FullName
cn.Open cnstr
Set ws1 = ThisWorkbook.Sheets("汇总")
Set ws2 = ThisWorkbook.Sheets("发货明细")
Set ws3 = ThisWorkbook.Sheets("对账单模板")
arr = ws1.Range("A1:E" & ws1.Cells(Rows.Count, "a").End(xlUp).Row)
For i = LBound(arr) + 1 To UBound(arr)
m = m + 1
Application.StatusBar = "一共需要生成" & UBound(arr) - 1 & "份对账单,正在处理第" & m & "份,处理进度:" & Format(m / (UBound(arr) - 1), "0%")
If Len(Trim(arr(i, 1))) = 0 Then GoTo 100
Sql = "select 日期,提货单号,合同编号,产品名称,规格型号,计量单位,数量,单价,金额 from [" & ws2.Name & "$] where 售达客户名称='" & arr(i, 1) & "';"
' Debug.Print Sql
rs.Open Sql, cn, 1, adLockPessimistic
n = rs.Clone.RecordCount
ws3.Copy after:=Sheets(Sheets.Count)
With ActiveSheet
.Name = arr(i, 1)
.[b3] = "致:" & arr(i, 1)
.[d9] = arr(i, 2)
.[d10] = arr(i, 4)
.[I10] = arr(i, 5)
.[I9] = arr(i, 3)
If n > 12 Then .Range("14:14").Resize(n - 12 + 1).Insert xlShiftDown
.[b13].CopyFromRecordset rs
rs.Close
End With
100:
Next i
Set rs = Nothing
cn.Close
Set cn = Nothing
MsgBox "处理完毕!用时" & Format(Time - t, "ss") & "秒!"
Application.StatusBar = ""
End Sub |
|