Unprotect the Excel workbooks in a specified path and refresh the workbook conection and then protect the workbooks

Dhakshna Moorthy

To elaborate(Unprotect the Excel workbooks in a specified path and refresh the workbook conection and then protec the workbooks)

I've a four workbooks in a specific path used by different users. I've used the power query to consolidate the workbooks with my Mastersheet using Data--> Refreshall. Some of the internal team issues I'm in a situation of protecting the workbooks.

After the initiation, the mastersheet won't get refreshed it indicates '[DataFormat.Error] File contains corrupted data.'. When i'm unprotecting the workbook, it worked properly.

So, please help me with this.

Option Explicit
Sub Unlock_Refresh()

    Dim wb As Workbook, ws As Worksheet
    Dim Filepath As String, Filename As String
    Dim n As Long
    
    Const pass = "1519"
    Filepath = Worksheets("Sheet2").Range("A1").Value
    If Right(Filepath, 1) <> "\" Then Filepath = Filepath & "\"
    
    Filename = Dir(Filepath & "*.xls*")
    Application.ScreenUpdating = False
    Do While Filename <> ""
        
        Set wb = Workbooks.Open(Filepath & Filename, Password:=pass)
        With wb
            .Unprotect Password:=pass
            .RefreshAll
            Application.Wait Now + TimeValue("00:00:10")
            .Protect Password:=pass
            .Close savechanges:=True
        End With
        n = n + 1
        Filename = Dir

    Loop
    Application.ScreenUpdating = True
    MsgBox n & " workbooks refreshed in " & vbLf & Filepath, vbInformation    
End Sub
Spinner

Don't know if this is related to the issue you're having or not, but there appears to be some logical disconnections in your code.
Line:

Set wb = Workbooks.Open(Filepath & Filename, Password:=pass)

Opens a workbook using a password that would be required only if the book was password protected to open. I note here:
a) this line of code will work even of no 'password-to-open' was set
b) as the code doesn't set a password-to-open, one assumes that is done manually?

Lines:

.Unprotect Password:=pass
.RefreshAll
.Protect Password:=pass

Un-protects the book-structure (and windows), refreshes external data, then re-protects the book-structure (only). I note here:
a) book-structure protection doesn't need to be off, in order to refresh external data
b) this doesn't set the 'password-to-open'
So it's unclear why the un-protect and re-protect logic cycle is there?

In case it's relevant, the following code would be used to set the 'password-to-open':

[workbook].Protect = pass

Este artículo se recopila de Internet, indique la fuente cuando se vuelva a imprimir.

En caso de infracción, por favor [email protected] Eliminar

Editado en
0

Déjame decir algunas palabras

0Comentarios
Iniciar sesiónRevisión de participación posterior

Artículos relacionados

Unprotect the Excel workbooks in a specified path and refresh the workbook conection and then protect the workbooks

Combine multiple workbooks to one workbook

copy paste one row from multiple workbooks to master workbook

c # Excel Interop _Workbooks.Open

Using Excel VBA to move columns for different workbooks

c # Excel Interop _Workbooks.Open

Apply a macro to all opened Excel Workbooks

Как установить объект Master Workbooks?

How to convert first worksheet of several excel workbooks into pdf in PowerShell?

Excel VBA Workbooks.open avec des paramètres facultatifs

Workbooks.OpenText no analiza los archivos csv correctamente Excel 2016

Excel VBA: Workbooks.Open devuelve el objeto incorrecto

Read all worksheets (as dataframes) from multiple Excel workbooks of different structure

Combining excel workbooks with same header columns but various rows in a master wb

Excel VBA Workbooks.open con parámetros opcionales

Activating Workbooks written a cell

Когда можно будет использовать Workbooks.Close в VBA?

Workbooks.Add n'active pas le nouveau classeur lors de l'exécution à partir de Workbook_Open ()

Workbooks.add falla 1004

Workbooks.open devuelve Nothing

Por qué ExcelApplication.Workbooks.Count muestra cero incluso cuando tengo un libro de Excel abierto

VBA Excel Workbooks.Open funciona solo si lo he abierto manualmente primero

Exception HResult 0x800a03ec when trying to open Excel with Microsoft.Office.Interop.Excel.Workbooks.Open()

VBA Excel ActiveWorkbook.Unprotect, не удаляя известный пароль?

Copy multiple workbooks and paste in a single sheet

VBA: Workbooks.Save & Close not saving

workbooks.close no funciona para mí

Comparing Columns in different Workbooks, editing rows that match

workbooks.close no funciona para mí

TOP Lista

CalienteEtiquetas

Archivo