on my windows pc I have a folder with a couple of csv files but sometimes there is a xlsx file between.
Later I want to copy all csv files into one, so that I can load it into a DB.
But for that I need to transform the xlsx files also into csv. I do not want to open all separately. Is there a way to do it automatically? I tried to create a macro in Excel but I didn't know how to apply that to all xlsx files.
Thanks for your help!
try the FileSystemObject approach
strPath = "C:\PATH_TO_YOUR_FOLDER"
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.DisplayAlerts = False
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFso.GetFolder (strPath)
For Each objFile In objFolder.Files
If objFso.GetExtensionName (objFile.Path) = "xls" Then
Set objWorkbook = objExcel.Workbooks.Open(objFile.Path)
' Include your code to work with the Excel object here
objWorkbook.Close True 'Save changes
End If
Next
objExcel.Quit
that should get u started.
Remember, after version 2003, Excel's Application.FileSearch is deprecated, so FileSystemObject approach is better
Related
How are you?
I have a vbs like below that works normally on files that are on network drives but does not work when the file is on my pc.
Set WshShell = CreateObject("WScript.Shell")
strCurDir = WshShell.CurrentDirectory
Set objExcel = CreateObject("Excel.Application")
with objExcel
.AskToUpdateLinks = False
.DisplayAlerts = False
.visible = True
end with
Set objWorkbook = objExcel.Workbooks.Open(strCurDir & "\4_?????.xlsb")
'Save the workbook
objWorkbook.Save
'close the workbook
objWorkbook.Close
with objExcel
.AskToUpdateLinks = True
.DisplayAlerts = True
.visible = True
end with
'exit the excel program
objExcel.Quit
'release objects
Set objExcel = Nothing
Set objWorkbook = Nothing
Where "?????" it's a timestamp. In this case, I know that the file starts with "4_" and has the extension ".xlsb" and my timestamp is 5 characters long. Soon the "?????" I used to "escape" this timestamp that changes continuously.
Note: I did a test, removing the "?????" and putting the timestamp that is today and it works on my machine. So for some reason my computer doesn't recognize the "?????" pattern.
Can someone help me find out why this script doesn't work for files that are local?
Thanks!!!
The (Excel)Workbooks.Open expects a file path, not a pattern. The easiest way to get what you want is to get list of files in the folder and check each name if it fits your pattern, for example:
Set fs = CreateObject("Scripting.FileSystemObject")
Set dir = fs.GetFolder(strCurDir)
Set ff = dir.Files
For Each f in ff
if left(f.Name,2)="4_" and right(f.Name,5)=".xlsb" then
Set objWorkbook = objExcel.Workbooks.Open(f.Path)
...
end if
Next
Alternatively, you can use regex.
I have VBscript code which opens XLS file and export it to CSV, see below:
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open("test.xls")
oBook.SaveAs "test.csv", 6
oBook.Close False
oExcel.Quit
It works fine but I need to do the same - convert XLS to CSV with already opened file "test.xls" on my computer which is different (edited by me) versus saved version. Is that possible?
Yes, you just need to connect the existing instance of Excel and set workbook object variable equal to the already open workbook instead:
Set oExcel = GetObject(, "Excel.Application")
Set oBook = oExcel.Workbooks("test.xls")
I want to convert .csv files to .xls. The files are created by another application without .csv suffix (ex.1 for example). I have background in C++ and C#.
There are a lot of VBScript examples but I don't know how to start, where to write the script, how to run it. I would appreciate a detailed answer.
This is very simple with Pyexcel https://pyexcel.readthedocs.org/en/latest/ for Python.
import pyexcel.cookbook import merge_all_to_a_book
import pyexcel.ext.xlsx
import glob
merge_all_to_a_book(glob.glob("test.csv"), "excelformat.xlsx")
If all of the files you wish to convert are in the same folder, you can use this code. As an option, if you don't need to save the original files, delete the oFSO.CopyFile line and un-comment out the oFSO.MoveFile line. Additional checking might be needed if other files are in the same folder.
Option Explicit 'Require all vars to be declared
Dim oXLApp, sCSVFiles, oFSO, oFiles 'Vars required
Dim oFile, sCSVName, sDestinName, oWorkBook 'Vars required
Set oXLApp = CreateObject("Excel.Application") 'Create excel object
oXLApp.Visible = True 'Make excel visible
oXLApp.DisplayAlerts = False 'Tell excel to not alert when saving files
sCSVFiles = "E:\Sample\" 'Folder where files to convert are
Set oFSO = CreateObject("Scripting.FileSystemObject") 'Create a File System Object
Set oFiles = oFSO.GetFolder(sCSVFiles).Files 'Get list of files in the folder
For Each oFile In oFiles 'Look at each file in the folder
sCSVName = oFile.Path & ".csv" 'Create the csv file name
sDestinName = oFile.Path & ".xlsx" 'Create the excel file name
If oFSO.FileExists(sCSVName) Then oFSO.DeleteFile(sCSVName) 'If csv name exists delete it
If oFSO.FileExists(sDestinName) Then oFSO.DeleteFile(sDestinName) 'If excel name exists delete it
oFSO.CopyFile oFile.Path, sCSVName 'Copy the file into CSV file ext
'oFSO.MoveFile oFile.Path, sCSVName 'Rename the existing file
Set oWorkBook = oXLApp.Workbooks.Open(sCSVName) 'Open the CSV file
oWorkbook.SaveAs sDestinName 'Save it as the default excel file type
oXLApp.Workbooks.Close 'Close the workbook
Next 'Process next file in the folder
oXLApp.DisplayAlerts = True 'Excel bug Fix: turn back on alerts
oXLApp.Quit 'Exit excel
Set oFiles = Nothing 'Destroy the oFile object
Set oFSO = Nothing 'Destroy the FSO object
Set oXLApp = Nothing 'Destroy the excel object
So I am having a problem uploading files from RStudio to Excel for MATLAB processing.
I had this problem before with formulas not being populated, so I made a script to open, save, and close the Excel file which then worked fine for populating the formulas and loading the numerical values back into RStudio. However I can not figure out how to open multiple .csv files that have changing names depending on our sample ID.
Heres my script that I tried to have open up multiple files:
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\PCRdata\*.csv")
## Also tried Set objWorkbook = objExcel.Workbooks.Open("C:\PCRdata\"& "*.csv")
objExcel.Application.Visible = True
objExcel.ActiveWorkbook.Save
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
WScript.Echo "Your Excel Spreadsheet was Updated, Open these files in Matlab"
WScript.Quit
However the script doesnt like the * to call upon all files, is there another way I can do this? or a better way to have RStudios come out with data that is usable to matlab.
MATLAB Error:
Error using netest/testingBrowseButton_Callback (line 63)
Cannot concatenate the table variables 'AKAP8L' and 'ARAF', because their types are double and cell.
Error while evaluating UIControl Callback
Well did some digging on the website and found an alternative post that used a script to change change xls to xslx so I just made it so it went from csv to csv. Hope this helps anyone else that had this problem.
Set app = CreateObject("Excel.Application")
app.Visible = False
app.DisplayAlerts = False
Set fso = CreateObject("Scripting.FileSystemObject")
For Each f In fso.GetFolder("C:\PCRdata").Files
If LCase(fso.GetExtensionName(f)) = "csv" Then
Set wb = app.Workbooks.Open(f.Path)
wb.Save
wb.Close True
End if
Next
app.Quit
Set f = Nothing
Set app = Nothing
Set fso = Nothing
wScript.Quit
I have a VBScript that opens all the excel files in a folder one by one and copies a certain range into a summary file. The summary file stays open through the entire operation, but the other files are closed after copying the range. This means that the Excel application stays open the whole time.
The problem is that Windows is holding onto each workbook even after it closes and the memory use steadily climbs. I tried to isolate the problem by disabling all add-ins and removing the personal.xlsb worksheet. I then manually opened several workbooks (no script involved) and closed them one by one. The memory use increased with each open file but did not decrease when I started closing them.
I have searched for hours now and the only answer I can find is to quit and restart the application. That's a pathetic workaround (and a pain for my script) - there has to be a better way to release closed workbook memory.
I'm running Excel 2013.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Shell.Application")
'Get summary folder location
Set StartFolder = objShell.BrowseForFolder(0, "Select the folder to summarize", 0, 5)
If StartFolder Is Nothing Then
Wscript.Quit
End If
StartFolderName = StartFolder.self.path
Set SuperFolder = objFSO.GetFolder(StartFolderName)
'Open Excel
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
Set objWorkbook = objExcel.Workbooks.Add()
NewExcelName = StartFolderName & "\Summary.xlsx"
objWorkbook.SaveAs(NewExcelName)
Call ShowSubfolders (SuperFolder)
'------------------------------------------------------------------------------
'Save and quit
objWorkbook.Save
objExcel.Quit
Wscript.echo "Done"
'------------------------------------------------------------------------------
Sub ShowSubFolders(fFolder)
Set objFolder = objFSO.GetFolder(fFolder.Path)
Set colFiles = objFolder.Files
For Each objFile in colFiles
'Open document
Set objxls = objExcel.Workbooks.Open(objFile.path)
'Do stuff here
'Close Document
objxls.Close False
Next
For Each Subfolder in fFolder.SubFolders
ShowSubFolders(Subfolder)
Next
End Sub
Try objxls.quit
The Set objxls = Nothing just destroys the object reference. The .quit method destroys the object itself.