Hi all,
I have quite a few queries that I transfer to excel
I want the column width to be set to Auto Fit
Below is the code and the module
Please could you advise if I am on the right track
I have quite a few queries that I transfer to excel
I want the column width to be set to Auto Fit
Below is the code and the module
Please could you advise if I am on the right track
Private Sub ExcelOpenItems_Click()
On Error GoTo ExcelOpenItems_Click_Err
Dim StrFileName As String
Dim StrQryName As String
Dim StrSaveFile As String
StrFileName = strGetFileFolderName("Open Items" & " - Registration -" & " " & Forms![CS Orders Detail - Main]![RegCBO], 2, "Excel")
'Debug.Print StrFileName
StrQryName = "Open Item QRY"
If Len(StrFileName & "") < 1 Then
StrFileName = Application.CurrentProject.Path & "\Open Items" & Format(Date, "yyyymmdd") & ".xls"
End If
Debug.Print "My File is:" & StrFileName
DoCmd.TransferSpreadsheet acExport, 10, StrQryName, StrFileName, True, , True
'Set column widths
DoCmd.OpenModule "AutoFitColumns"
ExcelOpenItems_Click_Exit:
Exit Sub
ExcelOpenItems_Click_Err:
MsgBox Error$
Resume ExcelOpenItems_Click_Exit
End Sub
Sub AutoFitColumns()
Dim sht As Worksheet
'AutoFit Every Worksheet Column in a Workbook
For Each sht In ThisWorkbook.Worksheets
sht.Cells.EntireColumn.AutoFit
Next sht
On Error GoTo 0
End Sub