I have .csv file which is a time series of daily data, with several data points associated with each date.
When I manually open the file, the dates open correctly, as the date format dd/mm/yyyy.
When I open the file programmatically, the dates up to the 12th of each month are opened as mm/dd/yyyy, although the format remains dd/mm/yyyy (e.g. the 1st of July 1983 (1/7/1983), would be opened as the 7th of January 1983 (7/1/1983) - this isn't just a formatting issue, the Julian Date (days since 1 Jan 1901) associated with these dates also changes), and the dates after the 12th of each month are opened correctly, although as text rather than a date.
The data coming in as text is not an issue, however, the dates changing as soon as the file is opened is problematic. I could try to import the entire .csv file as comma delimited text rather than opening the file, however, it would be easier and faster if I could stop the dates from changing when I open the file.
Flder = InputBox("Copy and Paste Folder path here:")
Set FSO = CreateObject("Scripting.FileSystemObject")
Set SourcePath = FSO.GetFolder(Flder)
For Each File In SourcePath.Files
Workbooks.Open (File)
FlNm = File.Name
StrtCol = Workbooks(FlNm).Worksheets(1).Range(Cells(4, 1), Cells(4, 30)).Find ("Mean").Column
Workbooks(FlNm).Worksheets(1).Range(Cells(1, 1), Cells(60000, 1)).Copy (Workbooks("Find Water Years V2.xls").Worksheets(1).Range("A3"))
Workbooks(FlNm).Worksheets(1).Range(Cells(1, StrtCol), Cells(60000, StrtCol + 1)).Copy (Workbooks("Find Water Years V2.xls").Worksheets(1).Range("B3"))
Workbooks(FlNm).Close
Next
The problem seems to occur at the line Workbooks.Open(File).
Since the question has already been answered by the OP in the comments but not posted as an official answer I'll put it here in case someone else misses it like I did.
workbook = workbooks.Open(filename, Local:= true)
By setting Local = true uses the local machines date format rather than assuming mdy so in locales such as Australia (dmy) it'll change the behavior of Open()
I experienced this problem with Office 365.
With importing an csv file with Excel some dates dd/mm/jjjj (European) where imported as American dates mm/dd/jjjj. The csv file doesn't have any formatting of dates closed in.
By opening as follows:
Set xla = CreateObject("Excel.Application")
Set xlb = xla.Workbooks.Open(sFilePath, ReadOnly:=False, Local:=True)
The local settings where used, and problem solved :-)
Other options with opening a file are:
expression.Open (FileName, UpdateLinks, ReadOnly,
Format, Password, WriteResPassword,
IgnoreReadOnlyRecommended, Origin,
Delimiter, Editable, Notify, Converter,
AddToMru, Local, CorruptLoad)
I could try to import the entire .csv file as comma delimited text rather than actually opening the file, however, it would be easier and faster if I could stop the dates from changing when I open the file.
It is still fast if you open it as CDT. You simply use .OpenText instead of .Open. Rest of the code remains same :)
Try recording a macro and you will see that the code looks somewhat like this.
Workbooks.OpenText Filename:= File, _
Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
Comma:=True, Space:=False, Other:=False, FieldInfo:=Array(1, 1), _
TrailingMinusNumbers:=True
I found that user2981639 had the right idea, but I got a syntax error with 'Local: true' so
I used 'local:=True' instead
This problem was driving me nuts because I also had 2 memo fields in the csv file.
If I tried to format the dates in any way the memo fields would not import correctly, they would either be truncated to 255 characters or any embedded CRLF characters would cut the record up into pieces depending on how many there were.
Thanks guys for posting
Here, you see a working code with example. This problem frustrated me as well for a while. I was opening a .txt file in excel and the date I had use for was converting from dd-mm-yyyy format to mm-dd-yyyy format. Below you see a solution. It lay in the last command (,Local := True). See below. Hope it helps.
Note that while trasferring .txt file to excel use File Origin as "xlWindows:
Workbooks.OpenText Filename:= _
ThisWorkbook.Worksheets("Reporting").Cells(3, 6), Origin:=xlWindows, _
StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(49 _
, 1), Array(50, 1), Array(67, 1), Array(80, 1), Array(93, 1), Array(106, 1), Array(119, 1)) _
, DecimalSeparator:=",", ThousandsSeparator:=".", TrailingMinusNumbers:= _
True, Local:=True
NOTE: rest above settings for array and decimal separator are my work specific - may not apply to you.
I am also struggling to find an answer. However what resolves this problem is.:-
Local should be true.
and workbook close save changes must be false. That should work.
Just had this problem myself and found the above solution
workbook = workbooks.Open(filename, Local:= true)
to be problematic in that it generates an error message. The MS documentation doesn't mention brackets or Workbook =, so I used this:-
Workbooks.Open Path, local:=True
Worked form me, hope it helps you.
Related
I'm exporting from an excel range with frozen pane at the top to a PDF file through VBA.
In my Page Layout, Width is set to 1 page, and Height is on Automatic.
For some reason though, the two first rows of my frozen pane get cropped in all pages of PDF but the first one.
First page:
Other pages:
Here is the code used to export (pretty straightforward):
lastRow = getLastRow(targetWksht) 'targetWksht is well defined elsewhere
lastCol = getLastCol(targetWksht)
With targetWksht.Range(targetWksht.Cells(1, 1), targetWksht.Cells(lastRow, lastCol))
.ExportAsFixedFormat Type:=xlTypePDF, _
fileName:=Application.ActiveWorkbook.Path & "\" & fileName, _ 'fileName is defined elsewhere
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=True, _
OpenAfterPublish:=True
End With
It might be something stupid, but do you know where it may come from and how to fix that?
Try $worksheet.PageSetup.FitToPagesTall = 9999 instead of automatic or set your margins with code.
I found where it came from.
The frozen pane was selected as page title, but for some reason not all rows in this title were supposed to be repeated in following pages.
Therefore, the title appeared complete in the first page, but only partial in other ones.
To solve the problem, if you encounter it, follow this:
In Excel, go to Page Layout > Page Setup > Print Titles. You will have Print Area, which is the title, and below, Rows to repeat at top.
Make sure that all rows from your title are selected. For some reason my default parameter started at 3 instead of 1, setting it at 1 solved the problem.
NB: It also works for columns at left.
I have some file.csv. If I open it by double-click, it opens in Excel with the proper setup (there are no semicolons and each "line" of data is correctly showing up in the expected columns).
Example result:
However, in VBA:
'Workbooks.OpenText Filename:=f, StartRow:=2, DataType:=xlDelimited, Semicolon:=True, ConsecutiveDelimiter:=True, TextQualifier:=xlTextQualifierNone
'Workbooks.OpenText Filename:=f, DataType:=xlDelimited, Semicolon:=True
'Workbooks.OpenText Filename:=f, DataType:=xlDelimited
'Workbooks.Open Filename:=f, Format:=xlDelimited, Delimiter:=Chr(34)
'Workbooks.Open Filename:=f, Format:=xlDelimited, Delimiter:=";"
Workbooks.Open Filename:=f, Format:=xlDelimited
Dim s As String
s = """" & ";" & """"
Workbooks.Open Filename:=f, Format:=xlDelimited, Delimiter:=s
'Workbooks.OpenText Filename:=f, DataType:=xlDelimited, OtherChar:=s
'Workbooks.OpenText Filename:=f, DataType:=xlDelimited, OtherChar:=";"
I've tried the lines above (and a number of others before I started keeping all my attempts) as well as without any arguments at all, but no matter what I do, the file will open without the text being split as it does when I double-click the file.
Example result:
So in summary; double-clicking the file parses the file correctly, Open and OpenText does not. Surprisingly, the worksheet looks identical regardless of using Open or OpenText.
A similar problem is described in this question, though it remains without a proper answer. The difference between our cases is that Excel won't parse anything for me, where it seems like it does parse large parts of the file for OP.
I just want to open the workbook and iterate over one of the columns, then close it. I have a couple of workarounds in mind, so I will be able to solve it one unnecessarily roundabout way or another.
So far I have found these ideas:
Renaming .csv to .txt and using OpenText
Using QueryTable import
Iterating over the borked file as it appears using OpenText and using LEFT/MID/RIGHT to get at the desired column(s)
Binary Open and Split each line into an array
Desired answers:
How to achieve this solution using Workbooks.OpenText or similar native function.
Explanations (or maybe even ideas) as to why Workbooks.OpenText and/or the most appropriate native function similar to it cannot achieve the desired result when Excel itself does seem to be capable of it.
Other workarounds.
If you are going to use VBA to import the file, just use VBA to parse the records:
Sub ImportFile()
Dim j As Long, k As Long
Close #1
Open "C:\TestFolder\whatever.csv" For Input As #1
j = 1
Do While Not EOF(1)
Line Input #1, TextLine
ary = Split(TextLine, ";")
k = 1
For Each a In ary
Cells(k, j) = a
k = k + 1
Next a
j = j + 1
Loop
Close #1
End Sub
Doing it this way stops VBA from assigning special meaning to the .csv extension.
Once the core code is working to your satisfaction, you can decorate it with other features like double-quote encapsulation, etc.
Thanks to #GarysStudent for inadvertently tipping me off as to the one attribute I had not been smart enough to try. They said "assigning special meaning to the .csv extension".
Turns out that localization impacts this "special meaning" in the .csv context. I am using a localized version of Excel and setting Local:=True enables both Open and OpenText to parse the file correctly, even without specifying ; as a delimiter.
I have a CSV with semicolon seperators that I would like to convert to a regular Excel sheet. I managed to do this with the code below, but I must have made a mistake because numbers with decimals in the original file that don't start with a zero are shown in Excel as number without the decimal separator. When I open the CSV manually in Excel the result will be fine, so it must be a side-effect of doing it with a script.
For example:
In the CSV there is a line:
2013-03-10 17:00:15; idle; 2,272298;; 0,121860
In the Excel sheet this becomes:
2013-03-10 17:00 | idle | 2.272.298| | 0,121860
Opened manually in excel gives:
2013-03-10 17:00 | idle | 2,272298| | 0,121860
Could somebody please tell me what I could/should change to keep the decimals as decimals in Excel? Possibly a way to tell Excel which symbol represents the decimal separator or an argument to force it into using European formats?
Kind regards, Nico
This is the script I currently have, where csvFile is a string with the full path to the original file and excelFile is a string with the full path to the location where I want to store the new excel sheet.
Set objExcel = CreateObject("Excel.Application") 'use excel
objExcel.Visible = true 'visible
objExcel.displayalerts=false 'no warnings
objExcel.Workbooks.Open(csvFile) 'open the file
objExcel.ActiveWorkbook.SaveAs excelFile, -4143, , , False, False 'save as xls
objExcel.Quit 'close excel
Create a schema.ini file in the folder your csvFile lives in and describe it according to the rules given here.
Further reading: import, text files
There are several approaches possible, I will cover one that I favor:
Start Recording a macro
Create a new workbook
From that workbook go to Data > From Text and there you select the CSV file, then you can do all the required settings regarding Value separators, Decimal separators, Thousands separators. Also the specific data type can be selected for each column.
When the CSV content is added go to Data > Connections and Remove
the connection. The data will stay in the worksheet, but there is no
longer an active connection.
Save the workbook under the xls name
Stop the Recording
Now tweak the script a bit to your liking.
In general Excel honors the system's regional settings. The CSV import, however, sometimes has its own mind about the "correct" format, particularly when the imported file has the extension .csv.
I'd try the following. Rename the file to .txt or .tsv and import it like this:
objExcel.Workbooks.OpenText csvFile, , , 1, 1, False, False, True
I made a work around. I now create a copy of the CSV file where I replace all commas followed by a number by points. While not very effective it does give Excel what it wants and it is simple enough for an inexperienced programmer like me to use.
When doing so a college asked me to also remove white spaces and entries with duplicate values in the first column (the timestamp in this case).
The result was this script
'csvFile is a string with the full path to the file. e.g. "C:\\Program Files\\Program\\data.csv"
'tempFile is a string with the full path to the file. e.g. "C:\\Temp\\temp.csv"
'excelfile is a string with the full path to the file. e.g. "D:\\Data\\sheet.xls"
Set fs=CreateObject("Scripting.FileSystemObject")
Set writeFile = fs.CreateTextFile(tempFile,True)
Set readFile = fs.OpenTextFile(csvFile)
' regular expression to remove leading whitespaces
Set regular_expression = New RegExp
regular_expression.Pattern = "^\s*"
regular_expression.Multiline = False
' regular expression to change the decimal seperator into a point
Set regular_expression2 = New RegExp
regular_expression2.Global = True
regular_expression2.Pattern = ",(?=\d)"
regular_expression2.Multiline = False
'copy the original file to the temp file and apply the changes
Do Until readFile.AtEndOfStream
strLine= readFile.ReadLine
If (StrComp(current_timestamp,Mid(strLine, 1, InStr(strLine,";")),1)<>0) Then
If (Len(previous_line) > 2) Then
previous_line = regular_expression2.replace(previous_line,".")
writeFile.Write regular_expression.Replace(previous_line, "") & vbCrLf
End if
End if
current_timestamp = Mid(strLine, 1, InStr(strLine,";"))
previous_line = strLine
Loop
readFile.Close
writeFile.Close
Set objExcel = CreateObject("Excel.Application") ' use excel
objExcel.Visible = true ' visible
objExcel.displayalerts=false ' no warning pop-ups
objExcel.Workbooks.Open(tempFile) ' open the file
objExcel.ActiveWorkbook.SaveAs excelfile, -4143, , , False, False 'save as excelfile
fs.DeleteFile tempFile ' clean up the temp file
I hope this will also be useful for someone else.
I'm trying to write a macro that downloads data into a file, where the file name contains yesterdays date.
"TEXT;http://www.mydomainname.co.uk/price_spiders_competitors_prices_gb/GBPS AEG " & Format(Today() - 1, "yyyy-mm-dd") & " higher_than.csv" _
, Destination:=Range("$A$1"))
This isn't working
"TEXT;http://www.mydomainname.co.uk/price_spiders_competitors_prices_gb/GBPS AEG 2013-02-06 higher_than.csv" _
, Destination:=Range("$A$1"))
This does!
Any ideas, I have a feeling the dashes are causing an issue but it also doesn't seem to like my Today -1!
Thanks for any help
Try function 'Now()' instead of 'Today()' to get the current date and time.
As far as i know, excel doesn't have the function 'today()' in VBA. You can use 'today()' in a formula but not in VBA.
I created VBA code in Excel 2007/2010 to import data from a CSV file. Unfortunately, when I open the file programmatically, the data is split into two columns (A and B) for certain rows of data.
When I open the CSV File manually, everything displays fine!
Generally the CSV data looks like this (example header row):
TBWAKT;"TBWAKO";"TBSAIS";"TBSKU9 ";"TBSMOD";"TBLETT";"TBKBNR
";"TBBEZ2 ";"TBFAR2
";"TBSUGC";"TBSOGC";"TBEINK ";"TBKBGR ";"TBKBGF
";"TBVKPE ";"TBVKPR ";"TBEKPE
";"TBAUAN";"TBFAAN";"TBREAN";"TBSTAN";"TBRUAN";"TBKPAG";"TBERDT
";"TBDATV ";"TBDATB "
The data that causes problems includes a comma in the text. Here is an example:
JEAN 5 POCHES EXTENSIBLE+1,60M
Here is the code:
Private Sub OpenCSV(x As Integer, wkbDashboard As String, wkbCsvImport As String, wksDestination As Worksheet)
' Opens CSV and copies data to current workbook
Dim wkbCsvImportName As String
Dim r As Range
Workbooks(wkbDashboard).Activate
' Open and read CSV
Workbooks.Open Filename:=wkbCsvImport, Format:=xlDelimited, Delimiter:=";"
wkbCsvImportName = ActiveWorkbook.Name
Screenshot of the problem. The stuff in red is in column B after opening the file.
Add Local:=True as argument in Workbooks.Open
Hope this might help!
I still suspect it's because the extension is CSV. What happens if you rename the file as a .txt?
In order to import data with a separator that is not a comma, you should set the Format attribute to 6 in order to be able to define your delimiter, as described here.
It should also work if you directly set Format to 4
I think when you do it manually Excel is reading the delimiter as ";" and not just ;.
Try this:
Workbooks.Open Filename:=wkbCsvImport, Format:=xlDelimited, Delimiter:=""";"""
EDIT:
the only way I can get this to work is by changing the file extension from csv to txt and then run this code:
Workbooks.OpenText Filename:=wkbCsvImport, _
DataType:=xlDelimited, semicolon:=True
I know two possible workarounds:
1) Change the extension from .csv to for example .xxx and open it like this:
Workbooks.OpenText fileName:="file.xxx", _
Origin:=xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:=1, _
ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=True, _
Comma:=False, Space:=False, Other:=False, OtherChar:="", _
TrailingMinusNumbers:=True, Local:=True
If you use .csv or .xls, then the excel overrides the settings by it's default values from the OS.
2) In Windows 10, change your locale setting from English - United States to English - United Kingdom. It's strange that it helps, it doesn't matter what the delimiter setting in advanced date/time is. In Windows 7 I think the delimiter setting worked.
Change the cell format to text.
e.g. Cells(1,1).NumberFormat = "#"