|
我程序中有部分写EXCEL的 看看能否有帮助 写得比较乱
Dim Str, Fname, Ftb, Ftb1, Enr As String
Dim TM, SS As Long
Dim oApp As Object
Dim oappwork As Excel.Workbook, oappwork_sub As Excel.Worksheet
Dim n1, n2 As String
Dim conn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim i, ii, iii, No As Integer
Dim SHR, XSFR, MDD As String
Dim SL, Rn As Integer
Dim Eii, EFR As Boolean
TM = Timer
Fname = CurrentProject.Path & "\DSTR.xls"
Set oApp = CreateObject("Excel.Application")
' oApp.Visible = True
oApp.Visible = False
Set oappwork = oApp.Workbooks.Open(Fname)
oApp.DisplayAlerts = False
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Ftb = "物量分析表"
i = oappwork.Sheets(Ftb).UsedRange.Rows.Count
Set conn = CurrentProject.Connection
Str = "SELECT IIf(IsNull([T_RI]![销售法人]),'OTHER',[T_RI]![销售法人]) AS 销售法人, T_RI.收货人, Sum(T_PI.数量) AS 数量, T_CI.目的地 FROM (T_RI LEFT JOIN T_PI ON T_RI.发票号码 = T_PI.发票号码) LEFT JOIN T_CI ON T_RI.收货人 = T_CI.收货人 GROUP BY IIf(IsNull([T_RI]![销售法人]),'OTHER',[T_RI]![销售法人]), T_RI.收货人, T_CI.目的地 ORDER BY IIf(IsNull([T_RI]![销售法人]),'OTHER',[T_RI]![销售法人]) DESC , T_RI.收货人;"
rst.Open Str, conn, adOpenKeyset, adLockOptimistic
Rn = CInt(Format(Date, "m")) * 2 + 3
SS = DSum("数量", "T_PI")
oappwork.Sheets(Ftb).Cells(6, Rn) = SS
If rst.RecordCount <> 0 Then
rst.MoveFirst
Do While Not rst.EOF
SHR = Trim(rst.Fields("收货人"))
SL = Trim(rst.Fields("数量"))
XSFR = Trim(rst.Fields("销售法人"))
MDD = Trim(IIf(IsNull(rst.Fields("目的地")), "", rst.Fields("目的地")))
If XSFR <> "OTHER" Then
For ii = 7 To i
If SHR = oappwork.Sheets(Ftb).Range("B" & ii) Then
oappwork.Sheets(Ftb).Cells(ii, Rn) = SL
oappwork.Sheets(Ftb).Cells(ii, Rn + 1) = SL / SS
Exit For
ElseIf ii = i And SHR <> oappwork.Sheets(Ftb).Range("B" & ii) Then
For iii = 7 To i
If XSFR = oappwork.Sheets(Ftb).Range("A" & iii) And oappwork.Sheets(Ftb).Range("B" & iii) = "Sub total" Then
oappwork.Sheets(Ftb).Rows(iii - 1 & ":" & iii - 1).Insert Shift:=xlDown
oappwork.Sheets(Ftb).Cells(iii - 1, 2) = SHR
oappwork.Sheets(Ftb).Cells(iii - 1, 3) = MDD
oappwork.Sheets(Ftb).Cells(iii - 1, Rn) = SL
oappwork.Sheets(Ftb).Cells(ii, Rn + 1) = SL / SS
i = oappwork.Sheets(Ftb).UsedRange.Rows.Count
Exit For
End If
Next iii
End If
Next ii
Else
oappwork.Sheets(Ftb).Cells(i + 1, 1) = XSFR
oappwork.Sheets(Ftb).Cells(i + 1, 2) = SHR
oappwork.Sheets(Ftb).Cells(i + 1, 3) = MDD
oappwork.Sheets(Ftb).Cells(i + 1, Rn) = SL
oappwork.Sheets(Ftb).Cells(ii, Rn + 1) = SL / SS
i = oappwork.Sheets(Ftb).UsedRange.Rows.Count
End If
rst.MoveNext
Loop
End If
oappwork.Close savechanges:=True
Set oappwork = Nothing
oApp.DisplayAlerts = True
MsgBox "文件生成成功,存放于" & vbNewLine & Fname & vbNewLine & "耗时 " & Format(Timer - TM, "0.000") & " 秒", 64, "提示"
oApp.Visible = True
Set oappwork = oApp.Workbooks.Open(Fname)
oApp.UserControl = False |
|