edited to be more specific:
VBScript below runs through entire folder, opens each *.XLSM file there, refreshes all data connections and saves updated files one by one. Files are also password protected, but it's not that important.
How to enhance the code to AVOID files in folder, based on part or entire filename? For an example avoid filenames ending with z however with still .xlsm extension.
Expected outcome for files located in the same folder:
abc123.xlsm (refresh); abc123z.xlsm (avoid); file.xlsm (refresh);
testz.xlsm (avoid)
Set fso = CreateObject("Scripting.FileSystemObject")
Set xl = CreateObject("Excel.Application")
xl.Visible = False
For Each f In fso.GetFolder("C:\test\").Files
If LCase(fso.GetExtensionName(f.Name)) = "xlsm" Then
Set wb = xl.Workbooks.Open(f.Path ,,,,,"x")
wb.RefreshAll
wb.Save
wb.Close
End If
Next
xl.Quit
you can try use split command.
file_path =abc123.xlsm
file_name =split(file_path,".")
file_name is an array containing 2 values
file_name (0)=abc123
file_name (1)=xlsm
now you can check if the last char in file_name(0)="Z"
if NOT(right(file_name(0),1)="Z") THEN
do...
ELSE
do...
END IF
Related
I need a macro to automatically loop through files in a folder and copy certain values from these files. I have the copy part setup on manually selected files, but need to now automatically select them.
I already have a macro setup to loop through multiple files that the user selects. Except now, I'm trying to run it automatically and would like the macro to autoselect any files in a specified directory. How do I specify this directory? I've included my current code for manual selection.
Edit: I tried implementing the below answer, but I recieve an error saying that the file list is not in the format of an array. How do I overcome this?
FileNames = Application.GetOpenFilename(FileFilter:="Excel Files,*.xl*;*.xm*;*.csv*", MultiSelect:=True)
ii = 1
Do While ii <= UBound(FileName)
Set wbk = Workbooks.Open(FileName(ii))
'Multiple Loops
Wend
Maybe try :
Sub GetAllFileNames()
Dim FolderName As String
Dim FileName As String
FolderName = "C:\Users\sumit\Desktop\Test\" 'change your path
FileName = Dir(FolderName & "*.xls*") 'this one will browse only .xls if you want to browse all file see the note
Do While FileName <> ""
Debug.Print FileName
FileName = Dir()
Loop
End Sub
Note : FileName = Dir(FolderName) will browse you all files in folder.
If the the workbook is in the same folder than the file you want to browse you can select path like this :
FolderName = Application.ActiveWorkbook.Path & "\"
I have a number of corrupted .xlsx files in a directory.
I want to open every single file for repair and save it with the same name via VBA script.
I`ve tried following piece of code to solve this problem:
Sub ProcessFiles()
Dim Filename, Pathname As String
Dim wb As Workbook
Pathname = ActiveWorkbook.Path & "\output\"
Filename = Dir(Pathname & "*.xlsx")
Do While Filename <> ""
Set wb = Workbooks.Open(Pathname & Filename, CorruptLoad:=xlRepairFile)
wb.Close SaveChanges:=True
Filename = Dir()
Loop
End Sub
But this code only repairs first file and opens windows explorer to save file manualy.
Is there a way to perform repair and save all files with the same name in the same folder automatically?
I haven't touched VBA in years, but there is an explicit wb.SaveAs method you can call.
Have you set Application.DisplayAlert = False? Your codes seems fine. You just have to turn it on later.
Scenario
I have a file that I am opening as ReadOnly using the following code.
Set wbRead = Workbooks.Open(FilePath, ReadOnly:=True)
Here the FilePath is a variable that tells the file location of that file
Problem
The issue I am facing is, if the user run macro second time without closing this already opened readonly file, it is giving runtime error due to having similar file name opened
What I need
Is there any way whereby excel can open a file as readonly, but the opened file shows some random name?
Eg: Actual file name is A. But when excel open it as readonly, it open as A123? 123 is like a random number.
Abother Solution would be to always (open or not) use Workbooks.Add to create a new copy of your file. Excel will automatically prompt you to save under a new name when you close:
Set wbRead = Workbooks.Add(FilePath)
If the file is already open, make a copy in the temp folder under a different name, and open it from there.
Sub OpenFile()
Const fPath As String = "C:\users\tim\desktop\tmp.xlsm"
Dim fso, wb As Workbook, fName, p
Set fso = CreateObject("scripting.filesystemobject")
p = fPath
fName = fso.getfilename(p)
On Error Resume Next
Set wb = Workbooks(fName)
On Error GoTo 0
If Not wb Is Nothing Then
p = fso.GetSpecialFolder(2) & "\" & Round(Rnd() * 1000, 0) & "_" & fName
fso.copyfile fPath, p
End If
Workbooks.Open p
End Sub
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! ;)
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