将我的 Access table 导出到 Excel,但将列中的不同值拆分到不同的工作表中
Export my Access table to Excel, but split different value in a column into different worksheets
我正在使用 Access VBA 为同事将 table 导出到 Excel,如果可以将输出拆分为同一工作簿中的不同工作表,那将非常方便取决于第 1 列中的值并以其命名。
这是我目前用于将整个 table 导出到 Excel 中的新工作簿的代码:
Private Sub export_Click()
If IsNull(DLookup("Name", "MSysObjects", "Name='tbl_found_playingtimes'")) Then
MsgBox ("No records to export.")
Else
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim rs1 As DAO.Recordset
DoCmd.Hourglass (True)
Set rs1 = CurrentDb.OpenRecordset("tbl_found_playingtimes")
Set xlApp = Excel.Application
xlApp.Visible = False
Set xlBook = xlApp.Workbooks.Add
Set xlsheet = xlBook.Worksheets(1)
With xlsheet
.Name = "test"
.Columns("I").NumberFormat = "0,00"
.Range("A2").CopyFromRecordset rs1
For cols = 0 To rs1.Fields.Count - 1
.Cells(1, cols + 1).Value = rs1.Fields(cols).Name
Next
End With
End If
SubExit:
On Error Resume Next
DoCmd.Hourglass False
xlApp.Visible = True
Exit Sub
End Sub
这很好用,除了一些障碍 - 数字列未导出 - 但我主要关心的是我是否可以将它拆分。每个标签编号将位于其自己的以标签编号命名的工作表中。
您需要做的是拥有一个包含唯一标签编号列表的 "outer" 记录集,然后遍历它,将过滤后的数据输出到每个工作表。像这样的事情应该让你开始:
Sub sExportExcel()
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim db As DAO.Database
Dim rsLabel As DAO.Recordset
Dim rsData As DAO.Recordset
Dim strSQL As String
Dim lngLoop1 As Long
Dim lngCount As Long
Set db = DBEngine(0)(0)
strSQL = "SELECT DISTINCT [label no] FROM tbl_found_playing_times ORDER BY [label no] ASC;"
Set rsLabel = db.OpenRecordset(strSQL)
If Not (rsLabel.BOF And rsLabel.EOF) Then
Set xlBook = xlApp.Workbooks.Add
Do
Set xlSheet = xlBook.Worksheets.Add(After:=xlBook.Worksheets(xlBook.Worksheets.Count))
xlSheet.name = rsLabel("label no")
strSQL = "SELECT * FROM tbl_found_playing_times WHERE [label no]=" & rsLabel("label no")
Set rsData = db.OpenRecordset(strSQL)
If Not (rsData.BOF And rsData.EOF) Then
xlSheet.Range("A2").CopyFromRecordset rsData
End If
rsLabel.MoveNext
Loop Until rsLabel.EOF
lngCount = xlBook.Worksheets.Count
For lngLoop1 = lngCount To 1 Step -1
If Left(xlBook.Worksheets(lngLoop1).name, 5) = "Sheet" Then
xlBook.Worksheets(lngLoop1).Delete
End If
Next lngLoop1
xlBook.Worksheets(1).Select
xlApp.Visible = True
End If
End Sub
此致,
我正在使用 Access VBA 为同事将 table 导出到 Excel,如果可以将输出拆分为同一工作簿中的不同工作表,那将非常方便取决于第 1 列中的值并以其命名。
这是我目前用于将整个 table 导出到 Excel 中的新工作簿的代码:
Private Sub export_Click()
If IsNull(DLookup("Name", "MSysObjects", "Name='tbl_found_playingtimes'")) Then
MsgBox ("No records to export.")
Else
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim rs1 As DAO.Recordset
DoCmd.Hourglass (True)
Set rs1 = CurrentDb.OpenRecordset("tbl_found_playingtimes")
Set xlApp = Excel.Application
xlApp.Visible = False
Set xlBook = xlApp.Workbooks.Add
Set xlsheet = xlBook.Worksheets(1)
With xlsheet
.Name = "test"
.Columns("I").NumberFormat = "0,00"
.Range("A2").CopyFromRecordset rs1
For cols = 0 To rs1.Fields.Count - 1
.Cells(1, cols + 1).Value = rs1.Fields(cols).Name
Next
End With
End If
SubExit:
On Error Resume Next
DoCmd.Hourglass False
xlApp.Visible = True
Exit Sub
End Sub
这很好用,除了一些障碍 - 数字列未导出 - 但我主要关心的是我是否可以将它拆分。每个标签编号将位于其自己的以标签编号命名的工作表中。
您需要做的是拥有一个包含唯一标签编号列表的 "outer" 记录集,然后遍历它,将过滤后的数据输出到每个工作表。像这样的事情应该让你开始:
Sub sExportExcel()
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim db As DAO.Database
Dim rsLabel As DAO.Recordset
Dim rsData As DAO.Recordset
Dim strSQL As String
Dim lngLoop1 As Long
Dim lngCount As Long
Set db = DBEngine(0)(0)
strSQL = "SELECT DISTINCT [label no] FROM tbl_found_playing_times ORDER BY [label no] ASC;"
Set rsLabel = db.OpenRecordset(strSQL)
If Not (rsLabel.BOF And rsLabel.EOF) Then
Set xlBook = xlApp.Workbooks.Add
Do
Set xlSheet = xlBook.Worksheets.Add(After:=xlBook.Worksheets(xlBook.Worksheets.Count))
xlSheet.name = rsLabel("label no")
strSQL = "SELECT * FROM tbl_found_playing_times WHERE [label no]=" & rsLabel("label no")
Set rsData = db.OpenRecordset(strSQL)
If Not (rsData.BOF And rsData.EOF) Then
xlSheet.Range("A2").CopyFromRecordset rsData
End If
rsLabel.MoveNext
Loop Until rsLabel.EOF
lngCount = xlBook.Worksheets.Count
For lngLoop1 = lngCount To 1 Step -1
If Left(xlBook.Worksheets(lngLoop1).name, 5) = "Sheet" Then
xlBook.Worksheets(lngLoop1).Delete
End If
Next lngLoop1
xlBook.Worksheets(1).Select
xlApp.Visible = True
End If
End Sub
此致,