I need to parse Excel work sheets. Now I save each individual work sheet as .csv and it works great. I use OpenCSV to parse the files etc. but to create those .csv files are a pain.
What would be the easiest and quickest way to save individual work sheets as .csv in Excel? I am assuming some kind of VBA macro would do the job, but since I am not a VBA programmer I have no idea how to do it. Maybe I can just record a macro somehow?
Very roughly,
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.SaveAs "C:\docs\" & ws.Name & ".csv", xlCSV
Next
This does not include any error coding nor does it make allowances for sheet names that will lead to illegal file names. It all depends on how robust you need the whole thing to be.
As a first answer, here is the code used to save a file to CSV:
ActiveWorkbook.SaveAs "C:\Marthinus.csv", fileformat:=6
More info about SaveAs
Related
I am having some issues with printing an excel file i'm using.
The file is made of a mastersheet, and several sheets that contain individual reports (using data in the mastersheet).
The sheets are generated from a template by a macro, and are automatically named. The sheet name is referenced in the sheet, and I am using Index/match to pull the data I need from the mastersheet. So this means that the tabs need to be recalculated constantly when I switch between them (and there's a little piece of VBA code that does it perfectly).
My issue is that when I want to print the individual reporting sheets, they are all printed with the same data (because their contents are not refreshed individually before printing).
Is there any way of printing them without selecting them one by one and individually printing them each time ?
I thought about some macro/vba code that would copy paste the sheets as values in another workbook, and then printing this workbook, but I'm not sure if its the optimal or quickest way of doing things. I would appreciate it if someone could help me.
Thanks !
Here's a start for you
Sub S
Dim ws as worksheet
For each ws in worksheets
ws.calculate
'or call your recalc routine
ws.printout
next ws
end sub
I've finally figured out why my code was crashing. I have this set up as part of my Personal Macro Workbook so when I open a default Book1 I can run it. However, the issue is that since it's running the macro from the PMW the "Sheet.Copy After:=ThisWorkbook.Sheets(1)" is crashing.
How can I make it that the code below running from the PMW would copy the sheets into the default Book1?
Original code below;
Sub GetSheets()
Application.AutoRecover.Enabled = False
LInput:
PL = Application.InputBox("Threshold Report Path", "", "C:\Users\")
Path = PL
Filename = Dir(Path & "*.csv")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Next Sheet
Workbooks(Filename).Close
Filename = Dir()
Loop
End Sub
ThisWorkbook refers to the workbook with the macro.
You can refer to it by name:
Sheet.Copy After:=Workbooks("Foo").Sheets(1)
I think you misunderstand the purpose of the Personal Macro Workbook; it shouldn't be auto-running anything. It's not a template. It's a place to store macros that you use often, so that instead of copying the macros to different workbooks, you can leave it in one place an run it from there.
I think what you want is a Personal Template that includes the template worksheet already, so nothing needs to be copied every time you create a new document.
Create a workbook, copy the worksheet in manually, and save it as a template. Avoid auto-run code in the template as well.
See links below for more information.
More information:
What you are trying to use:
Office.com : Create and save all your macros in a single workbook
Office.com : Create and save all your macros in a single workbook
What you should be using:
Office.com : Save a workbook as a template
Makeuseof : How to Quickly Create a Custom Excel Template to Save Time
I am sending a request two times using LOOP, and in output I am getting a unique value. I want to store these unique values in an excel sheet column so that further I can parameterize all the values.
I have successfully got the values to a TEXT file. But what I need is to store them in separate rows (in a column) in an EXCEL file, which I am not able to do.
Directly, I don't know. But you can always cheat. For example, EXCEL has a secondary format called .CSV, which is plain test...with ";" as separators.
So write your "output.csv" file with the following kind of structure :
Name;Income;Status
Martin;25000;OK
Johnson;32500;KO
And it should be plain openable in EXCEL. As a CSV. Once in EXCEL, save it back as a XLS or XLSX, and you're done, you've got a EXCEL file perfectly usable.
If you need some kind of automation for making this .csv into a .xlsx, here is a sample macro I made quickly & dirty, but works with EXCEL 2013 :
Sub ConvertFile()
On Error Resume Next
Kill "C:\MyPath\output.xlsx"
On Error GoTo 0
Workbooks.Open Filename:= _
"C:\MyPath\output.csv"
ActiveWorkbook.SaveAs Filename:= _
"C:\MyPath\output.xlsx", FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
End Sub
Beware : this is a EXCEL macro, not vbscript.
I currently am using a VBA macro that I found here on stack over flow. The problem is when I run it it saves all the data into a separate excel sheet but when i open it it appears as "jargon" in other words unreadable type. This is the "Save code"
'Save the new workbook, and close it
wb.SaveAs ThisWorkbook.Path & "\test" & WorkbookCounter
wb.Close
The way I am currently running the code is that it separates my excel sheets into different spread sheets by rows of 250. Everything works but when I open the saved documents it says that this file format is unacceptable to Excel. Then I try importing it and I get an error. Here is a snap shot of the way it appears in my screen. Also here is the file name: built.list\test.xls39
Your workbook counter always ends in a number, Windows and Excel use the file extension to determine file-type, so an '.xls39' file is unrecognisable. Try:
wb.SaveAs _
Filename:=ThisWorkbook.Path & "\test" & WorkbookCounter & ".xls" _
FileFormat:=XlFileFormat.xlExcel8
'Use xlOpenXMLWorkbook for the .xlsx format
(Use space followed by underscore to separate lines in VBA)
Or make sure WorkbookCounter ends in .xls and not a number.
(Edit: For other formats, please look in the References dialog in Excel VBA Editor)
When I use excel to open a .txt file (a notepad file), the worksheet name is the file name of the notepad file that was opened by default. Therefore, the sheet name will be different when open a different notepad file. Downstream code need this worksheet name be a fixed one. Is there anyway to make change the sheet name to a fixed name such as "sheet1". By the way, codename can not be used, since the macro to use the data in the open file is not another workbook.
Thanks!
You don't need the codename not the worksheet name when you are opening .txt files from Excel. There will always be 1 sheet. So in your code you can always address that sheet as
wb.Sheets(1)
Where wb is the workbook object.
For your reference every .txt file that you open with VBA cannot have a common name unless you set it via code. And if you do that, you will have to still use wb.Sheets(1)
For example
wb.Sheets(1).Name = "Blah Blah"
Could you call your text file sheet1.txt? Would that solve your problem?
I am picturing that your macro opens the text file dynamically because you want to use excel's built in csv parsing. Perhaps sorting and filtering the data afterwards.
Siddarth gave you a good lead, but you shouldn't worry about the name or the sheet because as he said you have the worksheet object to use for your downstream code.
wb.Sheets(1)
Now, if you want to reference this sheet outside of the subroutine that you opened the file. Use a global variable for your
wb