Copy data from protected worksheet with VBA

Bandito1

Board Regular
Joined
Oct 18, 2018
Messages
233
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

I have this code that copies data from A1 down to J1 and down where the cells in column C (3) is not empty.

Code:
Sub FilterData()

ActiveSheet.AutoFilterMode = False


Range("J1:Q" & Range("J" & Rows.Count).End(xlUp).Row).ClearContents


Range("A1").AutoFilter Field:=2, Criteria1:="<>"


ActiveSheet.AutoFilter.Range.Copy


Range("J1").PasteSpecial xlPasteAll


Application.CutCopyMode = False


ActiveSheet.AutoFilterMode = False


End Sub

Now i wanna use this in my project but the data that needs to be copied is in a protected worksheet named Shifts2019 which is placed somewhere on the disk P:

How do i change my code that the source is a protected workfile somewhere else?
The protected file is opened with read only.

I started with this but got stuck;

Code:
Sub FilterData()

ActiveSheet.AutoFilterMode = False


Sheets("Shifts").Range("A1:H" & Range("A" & Rows.Count).End(xlUp).Row).ClearContents


'in this line it should point to the protected file?
Range("A1").AutoFilter Field:=2, Criteria1:="<>"


ActiveSheet.AutoFilter.Range.Copy


Range("A1").PasteSpecial xlPasteAll


Application.CutCopyMode = False


ActiveSheet.AutoFilterMode = False


End Sub
 
Last edited:

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try this. change "abc" By the password of the sheet

Code:
Sub FilterData()
[COLOR=#0000ff]    Sheets("Shifts2019").Unprotect "[/COLOR][COLOR=#ff0000]abc[/COLOR][COLOR=#0000ff]"[/COLOR]
    ActiveSheet.AutoFilterMode = False
    Sheets("Shifts").Range("A1:H" & Range("A" & Rows.Count).End(xlUp).Row).ClearContents
    'in this line it should point to the protected file?
    Range("A1").AutoFilter Field:=2, Criteria1:="<>"
    ActiveSheet.AutoFilter.Range.Copy
    Range("A1").PasteSpecial xlPasteAll
    Application.CutCopyMode = False
    ActiveSheet.AutoFilterMode = False
[COLOR=#0000ff]    Sheets("Shifts2019").Protect "[/COLOR][COLOR=#ff0000]abc[/COLOR][COLOR=#0000ff]"[/COLOR]
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,531
Messages
6,114,172
Members
448,554
Latest member
Gleisner2

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