Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-05-2017, 01:47 AM
ArviLaanemets ArviLaanemets is offline How to open text (or csv) file from Excel VBA without Excel messing up with dates Windows 8 How to open text (or csv) file from Excel VBA without Excel messing up with dates Office 2016
Expert
How to open text (or csv) file from Excel VBA without Excel messing up with dates
 
Join Date: May 2017
Posts: 873
ArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud of
Default How to open text (or csv) file from Excel VBA without Excel messing up with dates

I'm cerating an Excel app which currently reads info from various format Excel files (xls, xlsx, xlsm etc). I want to add text (and csv) type files to this list.

To open a selected file an UDF is used:
----------------------------------------------------------------
Function strOpenFile(parPath, parExt, parTitle) As String
Dim strCurrDir As String
Dim strFile As String
Dim oWB As Workbook

strCurrDir = CurrDir

ChDrive parPath
DhDir parPath

strFile = Application.GetOpenFileName(FileFilter:=parFilter, Title:=parTitle)

If strFile <> "" Then
strFile = Right(strFile,Len(strFile)-InStrRev(strFile,"\",,1))
If Application.Workbooks(strFile) Is Nothing Then
Set oWB = Nothing
Set oWB = Workbooks.Open(strFile)
Else
Workbooks(strFile).Activate
End If
End If

ChDrive strCurrDir
ChDir strCurrDir

strOpenFile = strFile
End Function
----------------------------------------------------------------

The problem with text files (and with csv's probably too) is, that when International settings are used, then Excel messes up with dates.

I (and many of users in Europe) have dates in format dd.mm.yyyy. In text file the date string columns may have different formats - in my test file I have a column of dates as text in format dd-mm-yyyy (without any quotes). The formats in text files must remain as they are.

Now when I open the test text file from Excel manually, the dialog window is opening to initialize delimitters, column formats, etc. I don't change anyting, click on Finish, and the text file is opened. All is OK. Date column is converted to dates in my Windows Date Format dd.mm.yyyy.

When I open the same text file from my App, the file is simply opened, but:
a) the dates where day number is > 12 are displayed as date stings in format dd-mm-yyyy. It is no problem, as my procedure can handle them - CDate() function converts them to right dates;
b) the dates where day number is <= 12 are converted to dates, but day and month are switched - i.e. 01-05-2017 is converted to 05.01.2017 (instaead 1st of May I get 5th of January).

Of course the user can convert the text file manually to Excel file, and read in the Excel file instead of text one, but I'm trying to minimize manual work here.

So is there some way to force VBA to open text file without converting dates at all, using my current strOpenFile() function? I googled a bit, but the solutions I did find were very different.
Reply With Quote
  #2  
Old 06-13-2017, 11:31 PM
ArviLaanemets ArviLaanemets is offline How to open text (or csv) file from Excel VBA without Excel messing up with dates Windows 8 How to open text (or csv) file from Excel VBA without Excel messing up with dates Office 2016
Expert
How to open text (or csv) file from Excel VBA without Excel messing up with dates
 
Join Date: May 2017
Posts: 873
ArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud of
Default

I decided for a workaround here, as I couldn't find a way to stop excel trying to convert datestrings to dates.

I have a client setup table in my app, where for every client it's forecast file structure is described. I added a column into this table with value TRUE when months and days are switched when the client file is opened from VBA, and FALSE or empty otherwise. When the dates are read in from source file, the last step is to check, is the check value for this client TRUE and the day number less than 13 - when yes, then the reverse switching is made.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
When i open excel one always the same excel file open(empty XLstart folder) marcin4991 Excel 2 08-29-2016 02:36 AM
How to open text (or csv) file from Excel VBA without Excel messing up with dates Cannot open Excel file sportster Excel 8 09-18-2014 02:54 AM
How to open text (or csv) file from Excel VBA without Excel messing up with dates excel cannot open the file .xlsx because the file format or file extension is not val teddysika Excel 1 11-22-2012 06:06 AM
Cannot open the file in excel Shaikhu Excel 0 02-21-2012 04:49 AM
How to open text (or csv) file from Excel VBA without Excel messing up with dates Some Excel File Will Not Open cgoodlein Excel 5 05-19-2009 04:46 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:23 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft