My code exports a report to excel, and then formats some cells, The cells in column D need to be formatted as a number with 6 characters. When I run the query it looks fine, but when this is exported it is exported as text.
My code is enclosed. I have tried NumberFormat in the code but they don't seem to work What am I doing wrong ?
My code is enclosed. I have tried NumberFormat in the code but they don't seem to work What am I doing wrong ?
Code:
Sub FormatandQuitExcel(ExcelFile As String, LastCell As Integer, FreezeColumn As Integer)
On Error GoTo Handler
Dim xlApp As Object
Dim wb As Object
Dim ws As Object
Dim i As Integer
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
xlApp.DisplayAlerts = False
Set wb = xlApp.Workbooks.Open(ExcelFile)
Set ws = wb.Sheets(1)
'wb.ActiveWindow.WindowState = xlMaximized
ws.cells.Font.Name = "Calibri"
ws.cells.Font.Size = 9
ws.EnableAutoFilter = False
'ws.Cells.AutoFilter (1)
ws.Rows(1).EntireRow.insert
ws.Rows(2).Font.Bold = True
ws.Columns.AutoFit
For i = 1 To LastCell
ws.cells(2, i).Value = StrConv(ws.cells(2, i).Value, vbProperCase)
ws.cells(2, i).WrapText = True
Next i
DoEvents
ws.cells(1, 1).Value = "Version"
ws.cells(1, 2).Value = "1"
ws.cells(2, 24).Clear
ws.cells(2, 25).Clear
ws.cells(2, 26).Clear
ws.cells(2, 27).Clear
ws.cells(2, 28).Clear
ws.cells(2, 29).Clear
ws.cells(2, 30).Clear
ws.cells(2, 31).Clear
ws.Columns(4).select ' Column is selected correctly here. Need this to be formatted as a number to 6 places ie 000000.
wb.Save
xlApp.Quit
Exit Sub
Handler:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, ""
Resume Next
End Sub