Quotations in a saved name upon initial save? - excel

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.

Related

VBA ignore System Separators Setting on opening CSV file

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)"

How can I modify the name of a file originally defined in a recorded macro?

I recorded a macro that filters through and reformats data from a report. I would like to automatically save the file but the name of the report needs to change each time it is ran. The structure of the file name is "190509_Sample_Set1_TFB" where date, set#, and initials change.
As I originally recorded the macro I just highlighted the piece of information that I needed directly from the report and used it to save as the file name.
ActiveCell.FormulaR1C1 = "Working List Source: 190509_Sample_Set1_TFB"
ChDir "S:\shared\5. QC\QC Samples\2019"
ActiveWorkbook.SaveAs Filename:= _
"S:\shared\5. QC\QC Samples\2019\190509_Sample_Set1_QC_TFB.xlsx", _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
My work around has been to include a user prompt to manually enter the file name but it would be nice to have it totally automated.
You can set something like below and change "Your Initial" to a variable or Cell location like Range("A1")
"S:\shared\5. QC\QC Samples\" & Year(Now) & "\" & Format(Now, "yymmdd") & "_Sample_Set1_QC_" & "Your Initial" & ".xlsx"

is there an alternative SaveCopyAs property in VBA that can allow password protect?

I have a part of a my array script that moves the data into a template, works so quickly. Thing is, I have a SaveCopyAs property that doesn't support password protection.
Does anyone know of something similar that will allow for a password to be appended?
Wb.SaveCopyAs ThisWorkbook.Path & Application.PathSeparator & _
ValidFileName(Mgr & "_" & Last & "_" & "Realty Services Assessment.xlsx")
Ideally, I'd want everything the same, but just a password:= "_______" part in there, as well.
Use SaveAs instead. That will make the copy be the active workbook though, so you'll want to close that copy and re-open the original if you want to preserve the exact same behavior:
originalPath = wb.FullFileName
wb.SaveAs ...path...password...
wb.Close
Set wb = Workbooks.Open(originalPath)

Getting the entire path where a .mdb file is present by providing the database(.mdb) name in excel-vba

How can I get the entire path where a .mdb file is present by providing the database(.mdb) name in excel-vba.
When I install an application in my system, a database (.mdb file) will be created in the installation path. I want to take some data from that database(.mdb file) and use it in an excel file.
This would path would be different in different systems. I want my excel-vba code to automatically look for the database (.mdb file) and take the data from the database.
ActiveWorkbook.Path is where the current excel worksheet is found. assuming your database name does not change, then using this value & "MyDatabase.mdb" should find your database
May i suggest a different approach, as I understand the question in a the way, that it is necessary to find the .mdb file first - there is only the name provided.
In that case, you will find help in the following article - including some working code ;)
Microsoft KB185476
And for the part with the full-path:
Sub ShowFileAccessInfo(filespec)
Dim fs, d, f, s
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(filespec)
s = UCase(f.Path) & vbCrLf
s = s & "Created: " & f.DateCreated & vbCrLf
s = s & "Last Accessed: " & f.DateLastAccessed & vbCrLf
s = s & "Last Modified: " & f.DateLastModified
MsgBox s, 0, "File Access Info"
End Sub
*from excel-help (file-object)

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