Save close files

MILTINO

New Member
Joined
Jan 30, 2022
Messages
16
Office Version
  1. 2016
Platform
  1. Windows
class=y2iqfc>Greetings dear friends, please can you help me with this query.
style='font-size:21.0pt;font-family:inherit;color:#202124;mso-ansi-language:
EN'>I found a very interesting macro by which files in a folder are opened using a macro from a file in which Sheet 1 contains the name of the files and the password
style='font-size:21.0pt;font-family:inherit;color:#202124;mso-ansi-language:
EN'>What I need is that with an identical sheet in which the lists of the open files are found and with the keys for each one, they are saved and closed in the same folder that they were originally opened.
style='font-size:21.0pt;font-family:inherit;color:#202124;mso-ansi-language:
EN'>I attached the macro
Thank you
Sub AbrirArchivosConContraseña()
Dim ws As Worksheet
Dim wb As Workbook
Dim filePath As String
Dim password As String
Dim lastRow As Long
Dim i As Long
Dim currentPath As String
' Establece la hoja donde está la tabla
Set ws = ThisWorkbook.Sheets(1) ' Cambia el índice o nombre de la hoja según sea necesario
' Encuentra la última fila con datos en la columna A
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
' Obtén la ruta del directorio actual
currentPath = ThisWorkbook.Path
' Recorre cada fila de la tabla para abrir los archivos
For i = 2 To lastRow
' Obtén el nombre del archivo y la contraseña de la tabla
filePath = currentPath & "\" & ws.Cells(i, 1).Value
password = ws.Cells(i, 2).Value
' Intenta abrir el archivo con la contraseña
On Error Resume Next
Set wb = Workbooks.Open(Filename:=filePath, password:=password)
If Err.Number <> 0 Then
MsgBox "No se pudo abrir el archivo: " & filePath & " con la contraseña proporcionada.", vbExclamation
Err.Clear
End If
On Error GoTo 0
Next i
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top