Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing Excel cell Format 1

Status
Not open for further replies.

cbsm

Programmer
Oct 3, 2002
229
FR

I will need to import regurlarly some data (Excel Sheet) in an access table.
The problem I have is that the format (type) does not always match and can lead to errors (especially with dates).
So I decided to go through each column in excel and set the correct format like so (VBA in Access module):

Sub FormatFile(FileName As String)
Dim ExcelApp As New Excel.Application
Dim ExcelWST As New Excel.Worksheet
Dim ExcelRNG As Excel.Range
Dim myDB As Database
Dim recType As DAO.Recordset
Set myDB = CurrentDb
'open Excel file
ExcelApp.Workbooks.Open FileName
Set ExcelWST = ExcelApp.ActiveSheet
'in table ExcelFileFormat I stored the type of field for each row
Set recType = myDB.OpenRecordset("select * from Excelfileformat order by fieldorder")
If recType.EOF And recType.BOF Then
Else
'go through all columns and set the correct format
While Not recType.EOF

ExcelWST.Columns(recType!fieldorder).NumberFormat = recType!FieldType

recType.MoveNext
Wend
End If

ExcelApp.Workbooks.Close
ExcelApp.Quit
Set recType = Nothing
Set ExcelWST = Nothing
Set ExcelApp = Nothing
Set myDB = Nothing
End Sub

The procedure seems fine, no errors etc...
BUT when I open my excel file, some fields are OK, but not all : Dates : I have a column with date that are in format : dd-mon-yy (it is actually in the "general" format, but looks like dd-mon-yy for example 10-SEP-04)
After I ran the procedure the cell format is "dd/mm/yyyy" as desired, but it still looks like "dd-mon-yy")
Problem is I can't import this column in a date field in access ...

Any suggestions ?




 
Formatting dates AFTER they have been entered as an incorrect data type won't do anything I'm afraid - formatting relies on the correct data type being underneath the format.

However, excel can be quite clever so rather than induce a lot of pain, your best bet may be to coerce excel into thinking that these bits of text are in fact dates

The way to do this is to multiply them by 1 and the easiest way to do that is to put a 1 in a blank cell, copy it and then use Pastespecial>Values with an OPERATION of MULTIPLY on the dataset. This should force excel to accept them as dates and then they can be properly formatted

Rgds, Geoff

"Three things are certain: Death, taxes and lost data. Guess which has occurred"

Please read FAQ222-2244 before you ask a question
 
Thanks xlbo,

Worked just fine !

Happy New Year !
 
Good stuff - happy new year to you too

Rgds, Geoff

"Three things are certain: Death, taxes and lost data. Guess which has occurred"

Please read FAQ222-2244 before you ask a question
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top