I have this Code in Macro that i use to save files in csv. Is it possible now that i save this file in .bat Format ?
This is the Code i use :
Public Sub Export_File_as_CSV()
Dim wbkExport As Workbook
Dim shtToExport As Worksheet
Set shtToExport = ThisWorkbook.Worksheets("Export")
Set wbkExport = Application.Workbooks.Add
shtToExport.Copy Before:=wbkExport.Worksheets(wbkExport.Worksheets.Count)
Application.DisplayAlerts = False
wbkExport.SaveAs Filename:="C:\Users\" & Environ("Username") & "\Desktop\User_Tableau.csv", FileFormat:=xlCSV, Local:=True
Application.DisplayAlerts = True
wbkExport.Close SaveChanges:=False
Sheets("Export").Select
Range("A1").Select
End Sub
Otherwise i have to save it in Excel and then again to convert in .txt file and then in .bat file. I try to automate all this steps with macro and save it .bat file.
Your help is much appriciated !
Thank you
Just if someone Needs it in the future, i found the solution ! Apparently i was really close.
This is the changes i made and it worked.
Code :
Public Sub Export_File_as_BAT()
Dim wbkExport As Workbook
Dim shtToExport As Worksheet
'Call Artifficial_Groups
'Call filterout_for_export
'Call RemoveDuplicates
'Call Allocate_BEZ
Set shtToExport = ThisWorkbook.Worksheets("Export")
Set wbkExport = Application.Workbooks.Add
shtToExport.Copy Before:=wbkExport.Worksheets(wbkExport.Worksheets.Count)
Application.DisplayAlerts = False
wbkExport.SaveAs Filename:="C:\Users\" & Environ("Username") & "\Desktop\User_Tableau.bat", FileFormat:=xlTextPrinter, Local:=True
Application.DisplayAlerts = True
wbkExport.Close SaveChanges:=False
Sheets("Export").Select
Range("A1").Select
End Sub
Related
I have the following problem: After I start opening a workbook that I have just created, the code stops running with no error. Have anyone a solution how the rest of the code can be excuted?
Thanks in Advance!
Private Sub CommandButton3_Click()
On Error GoTo Eingabefehler
Application.DisplayAlerts = False
ActiveSheet.Select
Range("A1").Select
ActiveSheet.Paste
ThisWorkbook.Save
Dim strPath As String
Dim strName As String
UserForm1.Show
strPath = "C:\Users\Desktop\"
strName = Application.GetSaveAsFilename(InitialFileName:=strPath & ".xlsx", FileFilter:="XLSX-File (*.xlsx), *.xlsx")
ActiveWorkbook.SaveAs Filename:=strName, FileFormat:=xlOpenXMLWorkbook
Dim WbDatei1 As Workbook
Dim WbDatei3 As Workbook
Set WbDatei1 = ThisWorkbook
Set WbDatei3 = Workbooks.Open(strName) 'VBA Code stops working from here
For Each shp In WbDatei3.Sheets(1).Shapes
shp.Delete
Next
WbDatei3.Save
WbDatei1.Sheets(1).Select
Selection.Columns("F:F").EntireColumn.AutoFit
Range("G2").Select
ActiveCell.FormulaR1C1 = _
"=IFERROR(TIME(HOUR(RC[-2]-RC[-5]),MINUTE(RC[-2]-RC[-5]),SECOND(RC[-2]-RC[-5])),"""")"
[...]
i am trying to export a sheet from my Excel file as a csv.
I am getting the error
Method SaveAs of Object Workbook Failed`
on my SaveAs line.
I notice as this code creates a new workbook, it has several blank default sheet tabs, would this be causing the issue?
Public Sub ExportWorksheetAndSaveAsCSV()
Dim wbkExport As Workbook
Dim shtToExport As Worksheet
Set shtToExport = ThisWorkbook.Worksheets("Load check data") 'Sheet to export as CSV
Set wbkExport = Application.Workbooks.Add
shtToExport.Copy Before:=wbkExport.Worksheets(wbkExport.Worksheets.Count)
Application.DisplayAlerts = False 'Possibly overwrite without asking
Path = ThisWorkbook.Sheets("Input").Range("B15") & "estload_" & ThisWorkbook.Sheets("Input").Range("F1") & ".csv"
Debug.Print Path
wbkExport.SaveAs Filename:=Path, FileFormat:=xlCSV, CreateBackup:=True
Application.DisplayAlerts = True
wbkExport.Close SaveChanges:=False
End Sub
Edit to exclude WbkExport variable, replace with ThisWorkbook and ActiveWorkbook.
Public Sub ExportWorksheetAndSaveAsCSV()
Dim wbkExport As Workbook
Dim shtToExport As Worksheet
Dim Path As String
Set shtToExport = ThisWorkbook.Worksheets("Load check data") 'Sheet to export as CSV
'Set wbkExport = Application.Workbooks.Add
shtToExport.Copy Before:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
Application.DisplayAlerts = False 'Possibly overwrite without asking
Path = ThisWorkbook.Sheets("Input").Range("B15") & "estload_" & ThisWorkbook.Sheets("Input").Range("F1") & ".csv"
Debug.Print Path
ActiveSheet.SaveAs Filename:=Path, FileFormat:=xlCSV, CreateBackup:=True
Application.DisplayAlerts = True
'wbkExport.Close SaveChanges:=False
The previous comments are correct. A simple procedure would be:
Public Sub ExportWorksheetAndSaveAsCSV()
'Simple copy of sheet content as csv file
Dim NameSheet As String
Dim PathNameCsv As Variant
'Change names & Output Path
NameSheet = "MySheet"
PathNameCsv = "D:\Documents\MyCsv"
Set shtToExport = ThisWorkbook.Worksheets(NameSheet) 'Sheet to export as CSV
Application.DisplayAlerts = False 'Possibly overwrite without asking
shtToExport.SaveAs Filename:=PathNameCsv, FileFormat:=xlCSV, CreateBackup:=True
Application.DisplayAlerts = True
'When saving the sheet as csv, _
'by default its name is changed to the name of the csv.
'Here you restore your name
With shtToExport
.Name = NameSheet '
End With
End Sub
I have a question, I would like to save all my worksheets to seperate csv files and after it is finished the original excel file should be used. I've found some questions related to this on stackoverflow BUT I cannot combine them to make it work :(
Here is what I have at the moment which works:
Sub SaveSheetsAsCsv()
Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws In ActiveWorkbook.Worksheets
ws.SaveAs ActiveWorkbook.Path & "\" & ws.Name & ".csv", xlCSV, Local:=True
Next
Application.DisplayAlerts = True
End Sub
The result, I have all my worksheets saved to the same folder BUT then my workbook is named as my last worksheet and if I want to close it says I have to save it ... but instead I would like to have my original excel file active.
Any idea how can I do that?
I've tried to implement this: Keep the same excel but I always get an error :(
Any advice and help would be appreciated.
The adjustment below copies the sheet to a new book, saves it as CSV and closes.
Sub SaveSheetsAsCsv()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ThisWorkbook
Application.DisplayAlerts = False
For Each ws In ActiveWorkbook.Worksheets
ws.Copy
ActiveWorkbook.SaveAs wb.Path & "\" & ws.Name & ".csv", xlCSV, Local:=True
ActiveWorkbook.Close
Next
Application.DisplayAlerts = True
End Sub
I'm new to VBA and stuck on an issue. I'm trying to copy a table to a .csv file and I want the end result to contain the original .xlsm name & the table name and date/time. I've successfully pieced together code to export the table to .csv with the table name and date/time but I'm struggling to get the file mane in there. I get the following error "Method 'SaveAs' of object'_Workbook' failed"
Below is what I have, any help would be great!
Sub ExportTableBanquetEarnings()
Application.ScreenUpdating = False
Sheets("BanquetEarnings").Visible = True
Sheets("BanquetEarnings").Select
Dim wb As Workbook, wbNew As Workbook
Dim ws As Worksheet, wsNew As Worksheet
Dim wbNewName As String
Dim wbCurrent As String
wbCurrent = ThisWorkbook.FullName
Set wb = ThisWorkbook
Set ws = ActiveSheet
Set wbNew = Workbooks.Add
With wbNew
Set wsNew = wbNew.Sheets("Sheet1")
wbNewName = ws.ListObjects(1).Name
ws.ListObjects(1).Range.Copy
wsNew.Range("A1").PasteSpecial Paste:=xlPasteAll
.SaveAs Filename:="F:\admin\Report Databases\BanquetTipouts" & "\" &
wbCurrent & wbNewName & Format(Now, "yyyymmdd_hhmm") & ".csv", _
FileFormat:=xlCSVMSDOS, CreateBackup:=False
End With
Application.DisplayAlerts = False
ActiveWorkbook.Close
Application.DisplayAlerts = True
ActiveWorkbook.RefreshAll
Sheets("BanquetEarnings").Visible = False
Sheets("Blank Cost Sheet").Select
Workbooks.Open "F:\Function Agreements\Cost Sheets M\Payroll Report -
V2.xlsm"
End Sub
The below code will show the path of that Workbook
wbCurrent = ThisWorkbook.FullName
I think what you meant to use is...
wbCurrent = ThisWorkbook.Name
Also, I think another issue might be the Now method, it should be as Now()
I have the following code to save my worksheets as csv files to the folder the workbook is saved in. How do I modify this to bring up a 'save as' dialog box to let me choose where I'd like to save?
To be more specific, I want to modify the code to be able to specify just the path to which all the files can be saved. I'm not looking to get a save as for each worksheet.
Sub SaveOnlyCSVsThatAreNeeded()
Dim ws As Worksheet, newWb As Workbook
Application.ScreenUpdating = False
For Each ws In Sheets(Array("01 - Currencies", ..."14 - User Defined Fields"))
ws.Copy
Set newWb = ActiveWorkbook
With newWb
.SaveAs ws.Name, xlCSV
.Close (False)
End With
Next ws
Application.ScreenUpdating = True
End Sub
I have replaced the whole thing with a folder picker in an effort to simplify it. Posted updated code. Now I get Error code 9 - Subscript out of range.
Sub SaveOnlyCSVsThatAreNeeded()
Dim ws As Worksheet, newWb As Workbook
Dim pathh As Variant
Dim FolderName As String
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
If .Show = -1 Then
FolderName = .SelectedItems(1)
End If
End With
pathh = FolderName
Application.ScreenUpdating = False
For Each ws In Sheets(Array("01 - Currencies", "02 - .....14 - User Defined Fields"))
ws.Copy
Set newWb = ActiveWorkbook
With newWb
.SaveAs pathh.path & "\" & ws.Name, xlCSV
.Close (False)
End With
Next ws
Application.ScreenUpdating = True
End Sub
Use the following code to show the Save As dialog screen:
pathh = Application.GetSaveAsFilename( _
FileFilter:="CSV Files (*.csv), *.csv", _
Title:="Save all spreadsheets", _
InitialFileName:=filenamestring)
Cheers
The command to propt the save as dialog in VB is:
Application.GetSaveAsFilename