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
Related
I would like to be able to read and write values using VBA from one OPEN Excel workbook to another application without using the "as worksheet" function due to this being a non-windows application. Is this possible?
The code I've posted is from a PLC application using scripting.
I just need to know what function to use besides .open since the file is already opened.
Dim objXLApp, objXLWb, objXLWs1, objXLWs2, objXLWs5
Set objXLApp = CreateObject("Excel.Application")
objXLApp.Visible = True
Set objXLWb = objXLApp.Workbooks.Open("File.xlsm")
'~~> Working with Sheet1
Set objXLWs1 = objXLWb.Sheets(1)
Set objXLWs2 = objXLWb.Sheets(2)
Set objXLWs5 = objXLWb.Sheets(5)
'~~>Write SetPoints to Sheet1
With objXLWs1
'~~>Write
.Cells(6,23).value = SmartTags("Job Time Sec to OEE")
.Cells(6,23).value = SmartTags("Run Time Sec to OEE")
End With
''~~>Write Setup Values to Sheet2
'~~> Save as Excel File (xls) to retain format
objXLWb.Save
objXLWb.Close (False)
Set objXLWs1 = Nothing
Set objXLWs2 = Nothing
Set objXLWs5 = Nothing
Set objXLWb = Nothing
objXLApp.Quit
Set objXLApp = Nothing
If the workbook is already open you can just refer to it by name as part of the Workbooks collection.
Set objXLWb = objXLApp.Workbooks("File.xlsm")
Edit: Since you have changed your question I'm no longer certain what you're asking. My guess is that your 'already open' workbook isn't open in the same Excel instance you're creating with Set objXLApp = CreateObject("Excel.Application"), so you'll need a reference to the existing Excel application, then the open workbook within that application.
I have an Excel file named "ABCD.xlsm" in three different folders.
When I open these files using VBScript one after the other I cannot run the macros and the addin.
Set fso = CreateObject("Scripting.FileSystemObject")
Set theFile = fso.OpenTextFile("D:\Temp\Excellocation.txt", 1, False)
Do While theFile.AtEndOfStream <> True
retstring = retstring & theFile.ReadLine
Loop
theFile.Close
lx_loc = retstring
fso.DeleteFile "D:\Temp\Excellocation.txt"
set objExcel = Createobject("Excel.Application")
objExcel.visible = True
objExcel.workbooks.open(lx_loc)
msgBox "RUN macro CallVSTOMethod and press ok",1, true
In the code the text file has the location of the Excel file.
If there is only one use of this code, then I have no issues. When I call this function more than once, I can't run the addin or the macro.
It is because when you open your file with a connection, it is locking this file, so you cannot open it for that reason. What you can do, is copy past a duplicate and open the copy. It will open right away! ;)
I have been working with this issue for days. I tried multiple different ways. I am attempting to append multiple files into an Access linked table or even a temp table or even into a single excel file. At first EVERY SINGLE TIME on the first attempt the program works perfectly, then after that it stops functioning for a period of time and then starts operating again. When it stop functioning I get an Subscript out of range run-time error 9.I open the proper excel file but for some reason it won't let me set it... How can it OPEN THE FILE but in the next line CAN'T FIND IT??? It is driving me insane, it works, it stops working, then it works again... Any advice or hints would be very much appreciated.
This is just one way I tried to do this but they all end the same.
i = 2 'i is created through another loop previously.
j = 0
With MyXL
.Visible = True
.DisplayAlerts = True
End With
Do
Set MyXL = CreateObject("Excel.Application")
MyXL.Workbooks.Open Directory & fileArray(j), Notify:=False, ReadOnly:=False 'Tried True previously but changed since i was making changes to the file.
Set wb = Workbooks(fileArray(j)) 'DING DING DING!!! WHY??? You WORKED before!!!
If wb.Sheets("Sheet1").Range("A1") = "System Status" Then
wb.Sheets("Sheet1").Range("A1") = "PO System Status"
wb.Save
End If
wb.Close True
Set wb = Nothing
MyXL.Quit
Set MyXL = Nothing
Set wb = Nothing
j = j + 1
Loop Until j = i
Previously I thought I wasn't closing the workbook correctly, but I have closed the MyXL and previous wb but i still run into the error. I was wondering if this is something that Access/vba just can't do in succession as well. I changed the ReadOnly to true and it still ends up the same way.
Set your workbook to the return value from the Open method:
Set wb = MyXL.Workbooks.Open(Directory & fileArray(j), Notify:=False, ReadOnly:=False)
If wb.Sheets("Sheet1").Range("A1") = "System Status" Then
wb.Sheets("Sheet1").Range("A1") = "PO System Status"
wb.Save
End If
You don't need to/shouldn't create a new Excel application instance for every file - set that up before you enter the loop, and close it once you're done updating files. Check your Task Manager and make sure you don't have a bunch of Excel instances hanging around.
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.
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