Access:将子窗体导出到Excel,删除隐藏的列

时间:2017-03-21 10:40:03

标签: excel ms-access access-vba ms-access-2007

我在MS Access中有一个带有过滤子表单的表单,其中复选框控制隐藏列。我设法将过滤后的子窗体导出为ex​​cel,但隐藏的列保留并显示在excel文件中。

如何只将可见列导出到Excel文件?

我正在使用MS-Access 2007。 我到目前为止导出的代码:

Private Sub btn_export_Click()

On Error GoTo errHandler
Dim qdf As QueryDef
DoCmd.DeleteObject acQuery, "qryTemp"

Set qdf = CurrentDb.CreateQueryDef("qryTemp", Me.Results_Subform.Form.RecordSource)

DoCmd.OutputTo acOutputQuery, "qryTemp", acFormatXLS, "C:\TEST_Export.xls", True
exitHandler:
Exit Sub

errHandler:
If Err.Number = 7874 Then
    Resume Next
Else
    MsgBox Err.Number & " - " & Err.Description
    Resume exitHandler
End If
End Sub

1 个答案:

答案 0 :(得分:0)

像这样......

Dim x As Excel.Application
Dim r As Recordset
Dim w As Excel.Worksheet
Dim l As Long
Dim c As New Collection

Set r = Me.Recordset
Set x = New Excel.Application
x.Visible = 1

If x.Workbooks.Count = 0 Then x.Workbooks.Add

Set w = x.Worksheets(1)

For l = 0 To r.Fields.Count - 1
    If Me.Controls(r.Fields(l).Name).ColumnHidden Then
        c.Add l + 1
    End If
    w.Range("a1").Offset(0, l).Value = r.Fields(l).Name
Next l

w.Range("a2").CopyFromRecordset r

For Each i In c
    w.Columns(i).Hidden = True
Next i