Copy data from protected worksheet with VBA

Bandito1

Board Regular
Joined
Oct 18, 2018
Messages
85
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:

Some videos you may like

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,593
Office Version
  1. 2007
Platform
  1. Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,109,359
Messages
5,528,226
Members
409,809
Latest member
VICKRAM

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top