I am a beginner in using macro excel for automation.
I have nearly 100 of excel files with password. I would like to do as follows.
- Step 1: I will choose Input folder contains all excel files.
- Step 2: I will choose Output folder contains all output excel file.
- Step 3: File structure and password are as follows.
Re: Open all files in a folder using list of password
Originally Posted by Marc L
Hi,
according to your attachment, only 2 files from the Input folder are matching your 'File structure' list
but if for you it's not correct so I first need to know which file(s) must not be treated …
Dear Marc,
Sorry for the confusing.
So, what I want is:
- PV SV_2020(10) 薪水.xlsx and 4.PV V_2020(2) 薪水.xlsx: these files must be open with password "PV" then save as new file without password.
- 5.JP Salary -Mar 2020(pw).xls; 4.JP Salary -Feb 2020(pw).xls; JP Salary Nov 2020(pw).xls : these files must be open with password "JP" then save as xlsx without password.
- PIT calculation 11-20.xlsx: must be open with password "1120" then save as new file without password.
- PIT calculation 12-20 .xlsx: must be open with password "1220" then save as new file without password.
As yet stated according to your 'File structure' list only 2 files can be treated so you must first correct this list …
On my side the Dir VBA function can not work 'cause of the asian characters in some files names so according to your attachment :
PHP Code:
Sub Demo1()
Dim P, F$, oF As Object, V
With Sheet1
P = .[TRANSPOSE(B2:B3&IF(RIGHT(B2:B3,1)<>"\","\",""))]
With .Range("A8", .[B7].End(xlDown)).Columns
F = "TRANSPOSE(IF(ISNUMBER(MATCH(" & .Item(1).Address(, , , True) & ",{""#""},0))," & .Item(2).Address(, , , True) & "))"
End With
End With
With CreateObject("Scripting.FileSystemObject")
If .FolderExists(P(1)) And .FolderExists(P(2)) Then
Application.DisplayAlerts = False: Application.ScreenUpdating = False
For Each oF In .GetFolder(P(1)).Files
V = Filter(Evaluate(Replace(F, "#", oF.Name)), False, False)
If UBound(V) = 0 Then
With Workbooks.Open(oF.Path, , , , V(0))
.SaveAs P(2) & Left(oF.Name, InStrRev(oF.Name, ".")) & "xlsx", 51, Empty
.Close
End With
End If
Next
Application.DisplayAlerts = True: Application.ScreenUpdating = True
Else
Beep
End If
End With
End Sub
► Do you like it ? ► ► So thanks to click on bottom left star icon « ★ Add Reputation » ! ◄ ◄
Last edited by Marc L; 06-13-2021 at 06:57 AM.
Reason: tiny optimization …
Re: Open all files in a folder using list of password
Dear Marc L,
I have updated the file name structure with your code as attached in the macro file but the code only works for the file name PV*.xlsx (password PV) and file name JP*.xls (password JP). The code does not work with the remaining files as follow. Please help me. Thank you.
Re: Open all files in a folder using list of password
Thanks for the rep' !
So the file name structure is still bad as a ".xlsx" file can not be a ".xlsx " file !
Again, just correct the list and no need to review the code as it well works on my side once the list is correct,
the reason why of my warning about this list …
Bookmarks