'下面是我当前的代码,它将正确导出到excel工作簿和工作表。唯一的问题是我需要限制在月末日期范围内导出的数据(例如:2017年1月31日至2017年4月30日)以及工厂编号(例如:“4101”)感谢任何帮助,非常感谢。 Public Function InventoryXport_4100()
Dim appXL As Object
Dim wb As Object
Dim wks As Object
Dim xlf As String
Dim rs As DAO.Recordset
Dim fld As Field
Dim intColCount As Integer
xlf = "Z:\COST ACCOUNTING INFO\Inventory Reports\MyFile.xlsx"
Set rs = CurrentDb.OpenRecordset("(QS)_Inventory")
Set appXL = CreateObject("Excel.Application")
Set wb = appXL.Workbooks.Open(xlf)
Set wks = wb.Sheets("Inventory Xport") 'Sheet name
If rs.EOF = True Then
MsgBox "No data", vbOKOnly
Exit Function
End If
With appXL
.Application.worksheets("Inventory Xport").SELECT
.Application.columns("A:AQ").SELECT
.Application.columns.Clear
End With
intColCount = 1
For Each fld In rs.Fields
wks.Cells(1, intColCount).Value = fld.Name
intColCount = intColCount + 1
Next fld
appXL.displayalerts = False
wks.Range("A2").CopyFromRecordset rs
appXL.Visible = True
With appXL
.Application.worksheets("Inventory Xport").SELECT
.Application.columns("A:AQ").SELECT
.Application.columns.AutoFit
.Application.Range("A2").SELECT
.Application.ActiveWindow.FreezePanes = True
End With
wb.Save
wb.Close
appXL.Quit
Set wb = Nothing
rs.Close
Set rs = Nothing
End Function
答案 0 :(得分:1)
您可以使用:
Dim Date1 As Date
Dim Date2 As Date
Dim PlantNr As String
Dim Sql As String
Date1 = #1/31/2017#
Date2 = #4/30/2017#
PlantNr = "4101"
Sql = "Select * From [(QS)_Inventory] Where YourDateField Between #" & Format(Date1, "yyyy\/mm\/dd") & "# And #" & Format(Date2, "yyyy\/mm\/dd") & "# And [Plant Number] = '" & PlantNr & "'"
Set rs = CurrentDb.OpenRecordset(Sql)