Excel to tsv automatic updation - excel

Is there any scrips or tooling that could convert all the excel changes / editions dynamically into the tsv file ? My requirement is to make the changes done in the excel sheet to get reflected on to the tsv file of the same dynamically, the moment we save the excel sheet, its tsv should be edited and should contained the modification.
Thanks in advance guys!!

This VBA macro is based on work over at Mr. Excel but extended to save in two formats at once.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
' break the save
Cancel = True
' perform the save wourself
Application.EnableEvents = False
Me.SaveAs Filename:="c:\tmp\x.xlsm", _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
Application.EnableEvents = True
' do something after the save
Me.SaveAs Filename:="c:\tmp\x.tsv", _
FileFormat:=xlCurrentPlatformText, CreateBackup:=False
MsgBox "Workbook is saved in xlsm and tsv"
End Sub

Related

Excel VBA force save as .xlsm

I am have limited experience in coding in VBA and have implemented some code into an excel workbook, for which I take no credit for and which I obtained via the web, so all credit to the original creator. The code forces the user to save the workbook as .xlsm. The code works fine, as shown, with a small niggle.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim FileNameVal As String
If SaveAsUI Then
FileNameVal = Application.GetSaveAsFilename(, "Excel Macro-Enabled Workbook (*.xlsm), *.xlsm")
MsgBox (FileNameVal)
Cancel = True
If FileNameVal = CStr(False) Then 'User pressed cancel
Exit Sub
End If
Application.EnableEvents = False
If Right(ThisWorkbook.Name, 5) <> ".xlsm" Then
ThisWorkbook.SaveAs Filename:=FileNameVal & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
Else
ThisWorkbook.SaveAs Filename:=FileNameVal, FileFormat:=xlOpenXMLWorkbookMacroEnabled
End If
Application.EnableEvents = True
End If
End Sub
The niggle is that in selecting File/SaveAs, the UI presents the SaveAs dialogue box in which I type the new filename. This then creates a further SaveAs dialogue box with input for the filename again, but the inserted filename is the original current filename not the new one entered in the previous step. It seems the new entry is not carried over to the following dialogue box. Not a big issue, just more of a niggle. Is there any way to correct this please?
I had the same need with a Word Document which needed to be forced to save as a .docm. This was achieved in a different manner and all credit goes to David Zemens as answered in this post Word VBA force save as .docm. This works really well for my need. I was wondering if this type of method could be used in Excel and if so what would need to change?
Any help would be appreciated, thanks in advance.
zump

How to run a macro in excel VBA only on "Save-as" but not on normal "Save"

I have a form for work that I update and email out daily. I open from one master form ("Passdown Report 3rd Shift"), and save-as individual copies separated by the date ("Passdown Report 3rd Shift 2022-01-19") before I leave each day.
The form is filled with formulas that auto-update based on the day and based off stimuli from the other worksheets in the workbook, so I added a macro to convert all formulas in the range to their values.
I want to run this macro before I save the form as the daily file, but not when I'm simply updating and saving the master file. Is that something I can do?
Run Macro Before Closing a Workbook
It is expected that you SaveAs as the backup.
Only when closing the backup, the IF(StrComp... will note a different file name, and your macro will run and the backup will be saved again before closing.
It is kind of clumsy, but it should ensure the safety of your original.
The problem with BeforeSave is that you could do If SaveAsUI = True Then but you could also accidentally do the SaveAs on the original and have it 'destroyed'. I consider it too risky.
Option Explicit
Private ClosingBackup As Boolean
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Const sFileName As String = "Passdown Report 3rd Shift.xlsm"
If Not ClosingBackup Then
With Me
' Compare if you're closing the original or the backup.
If StrComp(.Name, sFileName, vbTextCompare) <> 0 Then ' backup
MyMacro
ClosingBackup = True
' Here it will again call this sub but will exit because
' 'ClosingBackup' is set to True ('If Not ClosingBakup Then').
.Close SaveChanges:=True
'Else ' ClosingBackup = False i.e. closing the original; do nothing
End If
End With
'Else ' ClosingBackup = True; closing the backup which was saved; do nothing
End If
End Sub
You can use the function BeforeSave, on your ThisWorkbook.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI then
'Your code
end if
End Sub
In the microsoft documentation is written on the popup of the SaveAs, this code will be executed and the flag SaveAsUI will be true.
https://learn.microsoft.com/en-us/office/vba/api/excel.workbook.beforesave
Note: You will need to make it an .xlsm file if you want your macro te be saved...

Runtime error when using ActiveWorkbook.SaveCopyAs Filename

ActiveWorkbook.SaveCopyAs Filename: gives
Runtime error 1004
but hitting Debug>Run> Continue code runs as expected.
I have a macro enable Excel2016 spreadsheet. When updated and saved to PC I also wish to save a copy to my NAS. I have written code (see below) and used identical code, other than filename, for two other spreadsheets. These other two spreadsheets are saved as expected (i.e. to NAS and PC with no Runtime error))
Code as follows:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Saves the current file to a backup folder and the default folder
'Note that any backup is overwritten
Application.DisplayAlerts = False
ActiveWorkbook.SaveCopyAs Filename:="\\ReadyNasDuo\Dell\Excelbak\finance18_19.xlsm"
ActiveWorkbook.Save
Application.DisplayAlerts = True
End Sub
I would expect problem file to act like the other two. Anyone any ideas why it does not. Only differences are that problem file is a lot larger, is password protected, has link to another spreadsheet and has far more 'coding'(i.e. more macros and VBA)
You must also deactivate the events Application.EnableEvents = False otherwise your Save/SaveCopyAs will trigger another Workbook_BeforeSave event.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Saves the current file to a backup folder and the default folder
'Note that any backup is overwritten
Application.DisplayAlerts = False
Application.EnableEvents = False
ThisWorkbook.SaveCopyAs Filename:="\\ReadyNasDuo\Dell\Excelbak\finance18_19.xlsm"
'ActiveWorkbook.Save 'needed?
Application.EnableEvents = True
Application.DisplayAlerts = True
End Sub
Also I think you don't need ActiveWorkbook.Save because it will save at End Sub anyway as long as you don't set Cancel = True. The event is called BeforeSave not InsteadOfSave so the original save action will still happen when BeforeSave finished.
Note that ThisWorkbook and ActiveWorkbook are not the same. I assume that you meant to use ThisWorkbook which is the workbook this code is running in, while ActiveWorkbook is the one that has the focus (is on top) while the code is running.

Excel 2016 VBA force save to .xlsm - How to save the template?

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim fname As Variant, DateTime As String, myInitialFilename As String
On Error GoTo ErrorHandler
SaveAsUI = True
If SaveAsUI Then
Cancel = True 'Cancel the original SaveAs
DateTime = "_" & Format(Now(), "yyyy_mm_dd_hhmmss") '= " [yyyy_mm_dd]"
'DateTime = " [" & Format(Now(), "yyyy_mm_dd hhmm_ss") & "]" '= " [yyyy_mm_dd hhmm_ss]" (use instead if you want time in the name)
myInitialFilename = "Quote" 'EDIT THIS
'Get filename (with path) for saving
fname = Application.GetSaveAsFilename(InitialFileName:=myInitialFilename & DateTime, fileFilter:="Excel Marcro-Enabled Workbook (*.xlsm),*.xlsm")
If fname = False Then Exit Sub 'Exit if user hit Cancel
Application.EnableEvents = False 'Prevent this event from firing
ThisWorkbook.SaveAs Filename:=fname, FileFormat:=52
'52 = xlOpenXMLWorkbookMacroEnabled = xlsm (with macro's in 2007-2010)
Application.EnableEvents = True 'Re-enable events
End If
Exit Sub
ErrorHandler:
Application.EnableEvents = True
MsgBox "An error occured during save." & Err.Number, vbCritical, "Error"
End Sub`
I have written a bit of VBA code to force a save or save-as to be the file type .xlsm, which works fine. However, I can't seem to save the template file with the VBA code in it because of the code itself forcing the .xlsm save.
I have a template saved without the code, but as soon as I add the code, obviously I can no longer save as xltm, since the code pushes to save as xslm. Looking for a best practice solution to save my template!
Thanks,
Kathy B.
Any changes within the file will either make it always save as xltm, or always as xlsm. So you want a flag stored in a different file, which defaults to false. This way, when you want to edit your template, you can turn it on and save your changes, but in normal usage, it will save as .xlsm.
If you do not overwrite the SaveAsUI-Flag with true
you can save the file as Template when you work on it.
If you create a new File form the Template the
SaveAsUI-Flag will be true and then your code forces
to keep the macros.

Force Save as XLSM While Maintaining File Structure

So I am working with a XLTM file, and I want the user to make sure they save as XLSM. When they click "Save," this works fine, but I find when they click "Save As," the file is saved as "*.xlsm.xlsm". I am a little lost with how to make sure that the user saves as XLSM, while keeping the file name as "filename.xlsm" and not "filename.xlsm.xlsm".
'Action makes sure the user saves as XLSM file type.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim FileNameVal As String
If SaveAsUI Then
FileNameVal = Application.GetSaveAsFilename(, "Excel Macro-Enabled Workbook (*.xlsm), *.xlsm")
Cancel = True
If FileNameVal = "False" Then 'User pressed cancel
Exit Sub
End If
Application.EnableEvents = False
ThisWorkbook.SaveAs Filename:=FileNameVal & ".xlsm", FileFormat:=ThisWorkbook.FileFormat
Application.EnableEvents = True
End If
End Sub
I thought the problem may have been writing ".xlsm" in:
ThisWorkbook.SaveAs Filename:=FileNameVal & ".xlsm", FileFormat:=ThisWorkbook.FileFormat
However, without ".xlsm" written there, I find the file instead saves as a bad file suffix. (E.g., if my XLTM file is called Template(File001).xltm, and the user opens a new template file, it will save as Template(File001)1 (believing that "1)1" is the file type).
It may be the structure of my code, so I need direction in how to revise it.
The problem appeared to have existed because the template would name the file "Template(1)1" prior to it actually being saved initially. This changes the way that Excel saves the file, so the easiest way to contrast between this initial save and further saves (that already contain a file extension) was to use an if-then statement to judge whether an extension exists already.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim FileNameVal As String
If SaveAsUI Then
FileNameVal = Application.GetSaveAsFilename(, "Excel Macro-Enabled Workbook (*.xlsm), *.xlsm")
Cancel = True
If FileNameVal = CStr(False) Then 'User pressed cancel
Exit Sub
End If
Application.EnableEvents = False
If Right(ThisWorkbook.Name, 5) <> ".xlsm" Then
ThisWorkbook.SaveAs Filename:=FileNameVal & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
Else
ThisWorkbook.SaveAs Filename:=FileNameVal, FileFormat:=xlOpenXMLWorkbookMacroEnabled
End If
Application.EnableEvents = True
End If
End Sub

Resources