98.7% Of all customers recommend us, we're so confident about our results we publish all reviews and stats
View Live Stats View ReviewsForum home » Delegate support and help forum » Microsoft Excel VBA Training and help » GetOpenFilename changing cell formats.
GetOpenFilename changing cell formats.
Resolved · Medium Priority · Version 2016
Daniel has attended:
Excel VBA Introduction course
GetOpenFilename changing cell formats.
I have a really weird formatting change occurring when opening a file using Application.GetOpenFilename
I have a csv file with a column of Date/Times (dd/mm/yyyy hh:mm:ss)
When I open this file directly from a folder, the dates are recognised as General format and I can format them however I like without issue. However, When I try to access this file as part of a macro, some of these dates are ready a Custom formatted as above and other remain as General but will no longer respond to any date formatting.
The File open code is standard
Dim fpath as sring
fpath = Application.GetOpenFilename(Title:="Please select Report File.")
If fpath = "False" Then
MsgBox ("You have not selected a file to load." & vbCrLf & "No changes have been made.")
Exit Sub
End If
Workbooks.Open Filename:=fpath
Is there some way to prevent the change of format when opening via Macro?
Thanks
Dan
RE: GetOpenFilename changing cell formats.
Hi Daniel,
Thank you for the forum question.
Dates from external files/sources have always been a problem in Excel.
If you always have the dates in the same column add this to your code (this code assume the dates are in column A starting from A2):
Range(range("A2"),Range("a2").End(xldown)).Select
Selection.TextToColumns Destination:=Range("A2"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 4), TrailingMinusNumbers:=True
This should give you the date format you want.
Please let me know if it is not working with your data.
Kind regards
Jens Bonde
Microsoft Office Specialist Trainer
Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us
London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector
RE: GetOpenFilename changing cell formats.
Hi Jens,
I've done a little more digging, and the issue that CSV file I'm loading holds days and months as a single digit if it is Jan to Sept or 1st to 9th.
The entries that are single digits are then read on opening as date but american format (mm/dd/yyyy) while the others remain as strings of text.
So I cannot use text to columns because the data is already corrupted as the file is opened.
However downloading the original file as an XLSX right away circumvents this problem,
Thanks for you help.
Dan
Training information:
Welcome. Please choose your application (eg. Excel) and then post your question. Our Microsoft Qualified trainers will then respond within 24 hours (working days). Frequently Asked Questions
Any suggestions, questions or comments? Please post in the Improve the forum thread. |
Excel tip:How to apply the same formatting and data to multiple sheets at the same time in Excel 2010When you want to format more than one sheet in a worksheet exactly the same way, Ctrl-click the tabs of the sheets you want to group together and they will all turn white. While they are grouped, anything you enter in one sheet gets entered into the others. |