oxicottin
03-21-2013, 11:44 AM
Hello, I have the code below that works great but during the export I want it to sort column B ascending. The worksheet has headers.
What would I add to the "HERE" area in code to get this to work? Thanks!!
Sub ExportToExcelXLS()
'************************************************************************** ******
' Exports a file named [OracleExport-MM-dd-yyyy.xls] to the desktop.
'************************************************************************** ******
Dim outputFileName As String
Dim XL As Object
Dim blRet As Boolean
outputFileName = "C:\Documents and Settings\" & Environ("username") & "\Desktop\OracleExport_" & Format(Date, "MM-dd-yyyy") & ".xls"
If Len(Dir$(outputFileName)) > 0 Then
Kill outputFileName
End If
blRet = PositionFormRelativeToControl("frm_ExportingDataPopUp", Forms!frm_Switchboard.imgExportToExcel, 1) 'Opens and positions Popup ontop of Control
Forms!frm_ExportingDataPopUp.lblMessage.Caption = "Transfering New Data...."
Pause (2)
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "qry_ExportToExcel", outputFileName, True
Set XL = CreateObject("Excel.Application")
XL.Workbooks.Open outputFileName
XL.Visible = False
Forms!frm_ExportingDataPopUp.lblMessage.Caption = "Formating New Data...."
Pause (2)
With XL
.Range("A1:F1").Font.Bold = True
.Range("A1:F1").Font.Name = "Segoe UI Light"
.Range("A1:F1").Font.Size = 12
.Range("A1:F1").Interior.ColorIndex = 44 'http://dmcritchie.mvps.org/excel/colors.htm
.Range("A2:F500").Font.Name = "Segoe UI Light"
.Range("A2:F500").Font.Size = 10
.Columns("A:F").EntireColumn.AutoFit 'Auto fits colums to the largest text
.Sort Ascending "HERE"
.ErrorCheckingOptions.NumberAsText = False 'Clears the green error arrows
Forms!frm_ExportingDataPopUp.lblMessage.Caption = "Saving Excel Workbook...."
Pause (2)
End With
XL.ActiveWorkbook.Save
XL.Application.Quit
Set XL = Nothing
Forms!frm_ExportingDataPopUp.lblMessage.Caption = "Export To Excel Complete...."
Pause (2)
DoEvents
DoCmd.Close acForm, "frm_ExportingDataPopUp"
End Sub
What would I add to the "HERE" area in code to get this to work? Thanks!!
Sub ExportToExcelXLS()
'************************************************************************** ******
' Exports a file named [OracleExport-MM-dd-yyyy.xls] to the desktop.
'************************************************************************** ******
Dim outputFileName As String
Dim XL As Object
Dim blRet As Boolean
outputFileName = "C:\Documents and Settings\" & Environ("username") & "\Desktop\OracleExport_" & Format(Date, "MM-dd-yyyy") & ".xls"
If Len(Dir$(outputFileName)) > 0 Then
Kill outputFileName
End If
blRet = PositionFormRelativeToControl("frm_ExportingDataPopUp", Forms!frm_Switchboard.imgExportToExcel, 1) 'Opens and positions Popup ontop of Control
Forms!frm_ExportingDataPopUp.lblMessage.Caption = "Transfering New Data...."
Pause (2)
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "qry_ExportToExcel", outputFileName, True
Set XL = CreateObject("Excel.Application")
XL.Workbooks.Open outputFileName
XL.Visible = False
Forms!frm_ExportingDataPopUp.lblMessage.Caption = "Formating New Data...."
Pause (2)
With XL
.Range("A1:F1").Font.Bold = True
.Range("A1:F1").Font.Name = "Segoe UI Light"
.Range("A1:F1").Font.Size = 12
.Range("A1:F1").Interior.ColorIndex = 44 'http://dmcritchie.mvps.org/excel/colors.htm
.Range("A2:F500").Font.Name = "Segoe UI Light"
.Range("A2:F500").Font.Size = 10
.Columns("A:F").EntireColumn.AutoFit 'Auto fits colums to the largest text
.Sort Ascending "HERE"
.ErrorCheckingOptions.NumberAsText = False 'Clears the green error arrows
Forms!frm_ExportingDataPopUp.lblMessage.Caption = "Saving Excel Workbook...."
Pause (2)
End With
XL.ActiveWorkbook.Save
XL.Application.Quit
Set XL = Nothing
Forms!frm_ExportingDataPopUp.lblMessage.Caption = "Export To Excel Complete...."
Pause (2)
DoEvents
DoCmd.Close acForm, "frm_ExportingDataPopUp"
End Sub