Unprotected and reprotect a worksheet

Joe9238

Board Regular
Joined
Jul 14, 2017
Messages
67
Hey,
I have a macro that does things to xls files that are found in a long list of hyperlinks. Some of these hyperlinks cannot be opened because they are protected. Is there a way to unprotect and reprotect any protected files?

This is the relevant part of the code:

Code:
CodeNames = Range("Z2:Z" & Cells(Rows.Count, "Z").End(xlUp).Row)   'the list of hyperlinksApplication.ScreenUpdating = False
For i = 1 To UBound(CodeNames, 1)                              'for each link in the list
    If InStr(1, CodeNames(i, 1), ".xls") > 0 Then             'if it is an xls file
    On Error GoTo linemarker3                                       'on error will not work on skipping unprotected sheets
        If Not WorkbookOpen(CStr(Split(CodeNames(i, 1), "\")(UBound(Split(CodeNames(i, 1), "\"))))) Then
            Set wbTarget = Workbooks.Open(CodeNames(i, 1)) 'this part gets highlighted when debugging

Code:
Function WorkbookOpen(WorkBookName As String) As Boolean
    WorkbookOpen = False
    On Error GoTo WorkBookNotOpen
    If Len(Application.Workbooks(WorkBookName).Name) > 0 Then
        WorkbookOpen = True
        Exit Function
    End If
WorkBookNotOpen:
End Function
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
first thing why your are using hyperlink? just take out path and filename details and loop through.


I'm assuming you declared "CodeNames" as range (CodeNames = Range("Z2:Z" & Cells(Rows.Count, "Z").End(xlUp).Row))

and here looks like you treat as CELL Workbooks.Open(CodeNames(i, 1))
to open workbook is simple

workbooks.open("pathdeails")
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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