Private Sub 命令5_Click()
On Error GoTo errit
Dim oExcel As Object
Dim oBook As Object
Dim i As Integer
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Add()
Me.子对象2.Form.Recordset.MoveFirst
For i = 0 To Me.子对象2.Form.Recordset.Fields.count - 1
oBook.Worksheets(1).Cells(1, i + 1).Value = Me.子对象2.Form.Recordset.Fields(i).name
Next
oBook.Worksheets(1).Range("A2").CopyFromRecordset Me.子对象2.Form.Recordset
oBook.SaveAs ("D:\myBB\XLS\员工名单.XLS")
MsgBox "导出成功"
errexit: oBook.Close False
oExcel.quit
Set oBook = Nothing
Set oExcel = Nothing
Exit Sub
errit:
MsgBox "错误号为" & err.Number & " 错误说明:" & err.Description
Resume errexit
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
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