I have an Excel with several sheets I want to export to csv delimited by columns.
When I run the code, it exports the files to csv but comma delimited, not column delimited as I export in csv.
Any help would be appreciated.
Sub SaveShtsAsBook()
Dim Sheet As Worksheet, SheetName$, MyFilePath$, N&
MyFilePath$ = ActiveWorkbook.path & "\" & _
Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 5)
With Application
.ScreenUpdating = False
.DisplayAlerts = False
' End With
On Error Resume Next '<< a folder exists
MkDir (MyFilePath & "_csv") '<< create a folder
For N = 1 To Sheets.Count
Sheets(N).Activate
SheetName = ActiveSheet.Name
Cells.Copy
Workbooks.Add (xlWBATWorksheet)
With ActiveWorkbook
With .ActiveSheet
.Paste
.Name = SheetName
[A1].Select
End With
'save book in this folder
.SaveAs ThisWorkbook.path & "\_csv\" & SheetName & ".csv", FileFormat:=xlCSV
.Close SaveChanges:=True
End With
.CutCopyMode = False
Next
End With
Sheet1.Activate
End Sub
Thanks!
Edit: Screenshot that clarifies my problem.
https://imgur.com/a/mPn997B
Define FileFormat as xlText and the file will be TAB delimited, which you obviously are looking for.
f.ex.:
Private Sub CommandButton1_Click()
Dim wb As Workbook
Set wb = ActiveWorkbook
wb.SaveAs "c:\tmp\tabtest.csv", xlText
End Sub
Related
I want to create a new (separate) workbooks for each worksheets from Test_Main but I am getting a
1004 error
in this line of code stating that I need a new name and cannot use this name as it is in use.
wb.SaveAs "H:\IT\Melissa\Essengeld\TEST" & "\" & ActiveSheet.Name & ".csv"
Is my code right? Or is there any other alternative for this ?
Thanks in advance !
Private Sub CommandButton3_Click()
Dim a As Integer
Dim ws As Worksheet
Dim wb As Workbook
a = ThisWorkbook.Worksheets.Count 'counts all the sheets
For i = 1 To a 'loops for all sheets
If ThisWorkbook.Worksheets(i).Name <> "Test_Main" Then 'rule out the main sheet
Set wb = Workbooks.Add
ThisWorkbook.Worksheets(i).Copy before:=wb.Worksheets(1) 'new workbook has 1 worksheet by deafult
wb.SaveAs "H:\IT\Melissa\Essengeld\TEST" & "\" & ActiveSheet.Name & ".csv"
wb.Close savechages = True
End If
Next i
ThisWorkbook.Activate
ThisWorkbook.Sheets(1).Activate
ThisWorkbook.Sheets(1).Cells(1, 1).Select
MsgBox ("Task Completed")
End Sub
Currently I have this code below:
Sub EXCELS()
'Create excel files
Dim i As Integer
Dim name_file As String
For i = 5 To Sheets.Count
name_file = Sheets(i).Name
Worksheets(i).Copy
With ActiveWorkbook
.SaveAs Filename:="C:\Users\marya\OneDrive - Desktop\Cantina\listas" & "\" &
name_file & ".xlsx", FileFormat:=xlOpenXMLWorkbook
.Close SaveChanges:=False
End With
Next i
End Sub
I would like to change and define this code below by sheets names, as "Lista_AA", "Lista_BB". Instead of creating excel files from sheet 5, create excel files that contain the names "Lista_AA", "Lista_BB"...
For i = 5 To Sheets.Count
Error:
It's not clear how we know which sheets should be exported to their own workbooks. This answer assumes that you want to export each sheet that has a name starting with "Lista_", so that would export "Lista_AA", "Lista_BB" but not "Meal_Register"
Sub EXCELS()
'Create excel files
Dim i As Integer
Dim name_file As String
For i = 1 To Worksheets.Count
name_file = Sheets(i).Name
If Left(name_file, 6) = "Lista_" Then
Worksheets(i).Copy
With ActiveWorkbook
.SaveAs Filename:="C:\Users\marya\OneDrive - Desktop\Cantina\listas\" & _
name_file & ".xlsx", FileFormat:=xlOpenXMLWorkbook
.Close SaveChanges:=False
End With
End If
Next i
End Sub
If there is a different pattern for the sheet names, or if you want to specify a list of sheet names to export, please make a comment below and I'll amend or make a new answer.
Here's a version that will overwrite existing workbooks with the same name without prompting the user
Sub EXCELS()
'Create excel files
Dim i As Integer
Dim name_file As String
Dim file_path as String
Application.DisplayAlerts = False
For i = 1 To Worksheets.Count
name_file = Sheets(i).Name
If Left(name_file, 6) = "Lista_" Then
Worksheets(i).Copy
file_path = "C:\Users\marya\OneDrive - Desktop\Cantina\listas\" & name_file & ".xlsx"
' try to delete a file that has the same name as the one we are about to save
on error resume next
kill file_path
on error goto 0
debug.print file_path
With ActiveWorkbook
.SaveAs Filename:=file_path, FileFormat:=xlOpenXMLWorkbook
.Close SaveChanges:=False
End With
End If
Next i
End Sub
I have an Excel file with 4 auxaliary sheets + 7 sheets with tables.
I would like to copy and separate each sheet (of the 7 sheets) into multiple excel's, so that each excel file has only 1 table. These sheets starts with "Lista", as for example "Lista_AA", "Lista_BB"...
After I would like to save these sheets with same name they had in the main excel.
I don't have code because I try with with macro recorder and didn't function.I have already looked for several videos and questions on this site and they are a little different from what I want
I have this code for create these sheets in pdf:
Sub excels()
Application.ScreenUpdating = False
Dim i As Integer
Dim nome_arquivo As String
For i = 5 To Sheets.Count
nome_arquivo = Sheets(i).Name
With Sheets(i)
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Path & "\" & nome_arquivo & ".pdf"
End With
Next i
Application.ScreenUpdating = True
End Sub
Is it possible to adapt for Excel files for same sheets?
Use a loop:
Const filepath As String = "https://agits-my.sharepoint.com/personal/Documents/Desktop/Cantina/"
Sub macro()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name Like "Lista_*" Then
SaveCopy ws:=ws
End If
Next
End Sub
Private Sub SaveCopy(ByVal ws As Worksheet)
ws.Copy
Dim wb As Workbook
Set wb = ActiveWorkbook
wb.SaveAs FileName:=filepath & ws.Name & ".xlsx", _
FileFormat:=xlOpenXMLWorkbook, _
CreateBackup:=False
wb.Close SaveChanges:=False
End Sub
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
It seems like a easy question, yet I can't seem to find the correct answer on Google.
What I want to do is open a workbook, copy a section and then close the workbook while saving the section I just copied.
I'm aware of the function to disable the clipboard prompt:
Application.CutCopyMode = False
ActiveWindow.Close
But this does not save the clipboard. Thus far I have written the following code to do so:
Sub Input()
Application.ScreenUpdating = False
Dim wb As Workbook
Dim wbPad As String
On Error GoTo ErrHandler
wbPad = ThisWorkbook.Sheets("Voorblad").Range("C10").Value
Set wb = Workbooks.Open(wbPad)
Cells.Select
Selection.Copy
Windows("Masterfile.xlsm").Activate
Worksheets("INPUT").Activate
Cells.Select
ActiveSheet.Paste
Range("A1").Select
Worksheets("Voorblad").Activate
Exit Sub
ErrHandler:
MsgBox ("Bestand niet gevonden. Controleer de maand en de naam van het bestand dat je wilt openen")
End Sub
If this is not possible, I would like to .Activate the workbook I opened using the cell reference and close this.
Maybe you could just skip the whole .select and .activate commands and use the optional Destination parameter of the .copy function.
(https://learn.microsoft.com/de-de/office/vba/api/excel.range.copy)
Since you did not provide how you want to save the range, I've added multiple basic examples below.
OPT1 - Save as .xlsx or .csv
Dim cpyRng As Range, newWb As Workbook, sPath As String
Application.DisplayAlerts = False 'remove system alert prompts
Set cpyRng = ThisWorkbook.Sheets("Sheet1").Range("A1:C10") 'Change sheet and range as needed
sPath = ThisWorkbook.Path & "\"
Set newWb = Workbooks.Add
With newWb
cpyRng.Copy
.Sheets("Sheet1").Cells(1, 1).PasteSpecial Paste:=xlPasteValues
.SaveAs Filename:=sPath & "Test" & "_" & Format(Date, "yyyymmdd") & ".xlsx", FileFormat:=51 'change file name to suit
'If you want to save as .csv use
'.SaveAs Filename:=sPath & "Test" & "_" & Format(Date, "yyyymmdd") & ".csv", FileFormat:=6
.Close
End With
'save your workbook and quit Excel
ThisWorkbook.Save = False 'use "True" if you want to save changes
Application.Quit
Application.DisplayAlerts = True 'Turn system alert prompts back on(best practice)
OPT2 - Save as .pdf
Dim cpyRng As Range, sPath As String
Application.DisplayAlerts = False 'remove system alert prompts
Set cpyRng = ThisWorkbook.Sheets("Sheet1").Range("A1:C10") 'Change sheet and range as needed
sPath = ThisWorkbook.Path & "\"
'Change file name to suit
cpyRng.ExportAsFixedFormat Type:=xlTypePDF, Filename:=sPath & "Test" & "_" & Format(Date, "yyyymmdd") & _
".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
Application.DisplayAlerts = True 'Turn system alert prompts back on(best practice)
OPT3 - Save as Word Doc
Dim cpyRng As Range
Set cpyRng = ThisWorkbook.Sheets("Sheet1").Range("A1:C10") 'Change sheet and range as needed
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
cpyRng.Copy
With objWord
.Visible = True
.Documents.Add
.Selection.Paste
End With
Application.CutCopyMode = False
Set objWord = Nothing