VBA ignore System Separators Setting on opening CSV file - excel

In my Windows OS, I set my "Date, time or number formats" with the following separators:
Decimal: ","
Milesimal: "."
List: ";"
In excel, I set in File > Options > Advanced > Use System Separators (ticked)
And when I open my CSV file manually, it goes according to the System Separators (data hidden due to confidential data). In the picture below, each data is in one Column
PROBLEM 1:
But when I asked my VBA macro to Open this CSV file, it totally ignored the System Separators. In the picture below, all data are in the Column A
The command I used in VBA to open the file was:
Workbooks.Open (MacroFile.Range("B" & i).Value & "\" & MacroFile.Range("C" & i).Value), UpdateLinks:=False
Note: It gets the Path + FileName through Cell Content
SOLUTION FOR PROBLEM 1 (credit to #FoxfireAndBurnsAndBurns):
Added property Local:=True to Workbooks.Open (MacroFile.Range("B" & i).Value & "\" & MacroFile.Range("C" & i).Value), UpdateLinks:=False, Local:=True
PROBLEM 2:
When I asked my VBA macro to Close+Save this CSV file, it totally ignored the System Separators. It replaced all the ";" with "," list delimiters
The command I used in VBA to Close+Save the file was:
Workbooks(MacroFile.Range("C" & i).Value).Close SaveChanges:=True
PROBLEM 3:
I've noticed too that when I tried to use the Formula command below, it didn't work, even having Separator as ";"
File_CSV.Range("Z1").Formula = "=SUMIF(F:F;""C"";G:G)"
I had to replace for:
File_CSV.Range("Z1").Formula = "=SUMIF(F:F,""C"",G:G)"
SOLUTION FOR PROBLEM 3 (Credit for TimWilliams):
Used .FormulaLocal in File_CSV.Range("Z1").FormulaLocal = "=SUMIF(F:F;""C"";G:G)"

Related

How to validate a tab-delimited file to see that it matches the original sheet?

There has been some issues when saving some excel files into tab delimited txt files where the columns have been shifted and also sometimes some data has been erroneous. Is there some good way of validating the tab-delimited txt file to check that each column matches the columns in my workbook sheet?
This is how the file is saved
FileExtStrInput = ".txt": FileFormatNumInput = 20
xWorkSheet.Copy
xFile = FolderName2 & "\" & "saved_txt " & Format(Now(), "dd-mm-yy hh-mm-ss") & FileExtStrInput
Application.ActiveWorkbook.SaveAs xFile, FileFormat:=FileFormatNumInput
Application.ActiveWorkbook.Close False
I would like to make a sub which I can call and it checks the column values one column at a time to make sure that it matches my workbook sheet. Then just post a MsgBox stating that validation was done successfully or if it failed.

Error when determining whether excel file is already open

I was trying to use the code from the following post to determine whether a different excel file is open:
Detect whether Excel workbook is already open
but my code was errorring out every time. After a bit of testing, I determined it appears to be because I have spaces in the filename/path. Is there a way to allow for the file path to include spaces?
You need to wrap the whole path and filename in double quotes if you have spaces in your file name.
So if you have a vba path & file name of:
WBPath = WB.Path & Application.PathSeparator & WBName
You need to add double quotes to the beginning and end of the string.
WBPath = """" & WB.Path & Application.PathSeparator & WBName & """"
What is """"? (The string starts with a " then the next two " " resolve to be a single double quote of text, then the last " ends the string.)
So you are in essence concatenating a Double Quote to the front of the path\file name and to the end.
Hope that helps. :)

StringBuilder Append replacing data- issue

From a very large Excel file, We loop and store the values from 3 of the columns into variables( uName(Row), mgrName(Row), title(Row) ). From this excel, we also get the number of rows.
The issue comes when I am trying to use StringBuilder to create a separate .xls file to be used later in the application. The code I have looks like this:
Dim XLstring As System.Text.StringBuilder = New System.Text.StringBuilder
Dim newfile As System.IO.StreamWriter
Dim fileTitle
XLstring.Append("Name,Manager,Title" & vbCrLf)
For X As Integer = 2 To countrows
If X = countrows Then
MsgBox(countrows)
MsgBox(uName(X) & "," & mgrName(X) & "," & title(X))
End If
XLstring.Append(uName(X) & "," & mgrName(X) & "," & title(X) & vbCrLf)
Next
fileTitle = System.DateTime.Now.ToString("yy-MM-dd hh-mm-ss") & ".xls"
filePath2 = "myPath" & fileTitle
newfile = File.CreateText(filePath2)
newfile.WriteLine(XLstring)
This works for the most part. I can see that I am grabbing the correct number of rows as well as the correct information in the last row using MsgBox. When I open the resulting .xls file however, there are entries missing at the end of the file. In addition, if I were to change
XLstring.Append("Name,Manager,Title" & vbCrLf)
to something like
XLstring.Append("Name,Manager" & vbCrLf)
the exact number of characters I removed from the Append line will now successfully appear at the end of the file where the information is missing.
Is there some weird functionality that I am not understanding using these functions? I am completey lost and don't understand this behavior.
You need to close the file when you're done:
newfile.Close()
On a side note, your file is being saved with the XLS extension, but since you are exporting a list of comma-separated values, I would suggest saving in CSV format instead.

Quotations in a saved name upon initial save?

This code works, but something odd comes out of it, when I attempt to save the file, it shows up at "Open Order Log - 10-4-12.xlsx" instead of Open Order Log - 10-4-12.xlsx is there a reason for that?
NewFile = Application.GetSaveAsFilename(InitialFileName:="Open Order Log - " & Format(Date, "dd-mm-yyyy") & ".xlsx")
Solved it:
wbBK2.SaveAs Dir & Application.PathSeparator & "Open Order Report -" & Format(Date, "mm-dd-yyyy") & ".xlsx"
It's a little bit of a twist, but it works.
to remove the quotes, you have to use a filter.
NewFile=Application.GetSaveAsFilename(InitialFileName:="StackOverflow.xlsx", _
FileFilter:="Excel Files (*.xlsx), *.xlsx")
note for Mac:
Be aware that there is a long time bug with
getsaveasfilename: ­ if the user navigates to the file using the disclosure
arrows, an incorrect path will be returned. However, if the directory nodes
are instead double clicked, the path will be correct.

Excel 2003: Save unicode spreadsheet as normal spreadsheet to retain formatting (VBA)

My code opens a unicode .xls file exported from SAP, does some filtering and color-coding of cells, then attempts this:
.SaveAs Filename:=srcpath & Format(Date, "mm-dd-yyyy") & "file.xls", FileFormat:=xlNormal
Also tried
.SaveAs Filename:=srcpath & Format(Date, "mm-dd-yyyy") & "file.xls", FileFormat:=xlWorkbookNormal
Also tried
.SaveAs Filename:=srcpath & Format(Date, "mm-dd-yyyy") & "file.xls", FileFormatNum:=-4143
I know the string "srcpath" and dating bit are okay since the save happens, but I get no formatting because it's still a unicode file! This is in 2003 btw. Any ideas will be much appreciated!
The error was elsewhere in my code, one of those situations where the debugger misleads you because there's an if statement or something. For the record, this works just fine in 2003:
ActiveWorkbook.SaveAs Filename:=srcpath & c & " file.xls", FileFormat:=xlWorkbookNormal
where "c" is some string you want in the filename; in my case today's date.

Resources