Copy all named ranges but first unprotecting all sheets

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,566
Office Version
  1. 2021
Platform
  1. Windows
I have a macro that first unprotects all sheets from sheet3 and the copies the named range that match the sheet with the same name as the ranged sheet and then protects the sheet

I am battling to get this to work,

It would be appreciated if someone could assist me


Code:
 Sub Copy_Comm()
Application.DisplayAlerts = False

Dim i As Long
For i = 3 To Worksheets.Count


     
       Worksheets(i).Unprotect
      
    Next i
    

With Sheets(1)
    .Range("Peter).Copy
    
        Sheets("Peter).Range("A1").PasteSpecial Paste:=xlPasteValues
        End With
        
     Worksheets(i).Protect
   Next i
    

 End Sub
 
It depends on how you want to Paste:
If you want to paste keeping formats AND pasting values only:

Code:
.PasteSpecial Paste:=xlPasteFormats, Paste:=xlPasteValues

Or if you want to paste keeping formats and formulas intact:

Code:
.PasteSpecial Paste:=xlPasteFormats
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
I need a bit more help.

I have added some code at the end to hide specific columns but get an error message "Subscript out of range" and the code With Worksheets(i) is highlighted

It would be appreciated if you could assist me



Code:
 Sub Copy_All_Comms()

Clear_Sheets
 Dim i As Integer, LastRow As Long
        
        'Loop Through Worksheets
        For i = 3 To Worksheets.Count
        
        'Unprotect Worksheet
        Worksheets(i).Unprotect
    
            'Find the last row in Column A in Sheet Neil_PE
            LastRow = Sheets("Peter").Cells(Sheets("Peter").Rows.Count, "A").End(xlUp).Row

            'Work with The Current Unprotected Sheet:
            With Worksheets(i)
                'Use The Worksheet Name to Return The Named Range
                Range(Worksheets(i).Name).Copy
                'Use The Worksheet Name to Copy The Above Named Range Into The Worksheet by the SAME NAME
                Worksheets(i).Range("A1:A" & LastRow).Offset(1).PasteSpecial Paste:=xlPasteValues
            End With
             Columns("C:C").ColumnWidth = 11
        'ReProtect WorkSheet
        Worksheets(i).protect
        Next i
        Sheets(2).Unprotect
         With Worksheets(i)
        .Range("D:D", "F:F", "H:H", "L:L").EntireColumn.Hidden = True
        End With
        
End Sub
 
Upvote 0
According to your code, you want to hide columns D,F,H, & L, but only in sheet 2
IF that is the case, replace your added code with this:
Code:
       'Work With Sheet 2 Only:
    With Sheets(2)
        .Unprotect
       'Hide Columns D,F,H,&L
        .Range("D:D").EntireColumn.Hidden = True
        .Range("F:F").EntireColumn.Hidden = True
        .Range("H:H").EntireColumn.Hidden = True
        .Range("L:L").EntireColumn.Hidden = True
       'ReProtect The Sheet
        .Protect
    End With

Regards
 
Last edited:
Upvote 0
Thanks for your code, much appreciated

Sorry I meant on all sheets except sheet 1 & 2

See my code below , which I amended and which works



Code:
 Sub Copy_All_Comms()

Clear_Sheets
 Dim i As Integer, LastRow As Long
        
        'Loop Through Worksheets
        For i = 3 To Worksheets.Count
        
        'Unprotect Worksheet
        Worksheets(i).Unprotect
    
            'Find the last row in Column A in Sheet Neil_PE
            LastRow = Sheets("Comm_PE").Cells(Sheets("Comm_PE").Rows.Count, "A").End(xlUp).Row

            'Work with The Current Unprotected Sheet:
            With Worksheets(i)
                'Use The Worksheet Name to Return The Named Range
                Range(Worksheets(i).Name).Copy
                'Use The Worksheet Name to Copy The Above Named Range Into The Worksheet by the SAME NAME
                Worksheets(i).Range("A1:A" & LastRow).Offset(1).PasteSpecial Paste:=xlPasteValues
            End With
             Columns("C:C").ColumnWidth = 11
        'ReProtect WorkSheet
        Worksheets(i).protect
       
        Worksheets(i).Unprotect
       'Hide Columns D,F,H,&L
        Range("D:D").EntireColumn.Hidden = True
        Range("F:F").EntireColumn.Hidden = True
        Range("H:H").EntireColumn.Hidden = True
        Range("L:L").EntireColumn.Hidden = True
       'ReProtect The Sheet
       Worksheets(i).protect
     Next i
        
End Sub
 
Upvote 0
No problem Howard, glad I could help.

Hmmm, but do I detect a potential problem in the final code?
This copies the Named Range of the Matching Worksheet.
Then it pastes that named range, Into the Worksheet that has the same name.

The potential problem is that "LastRow" is being calculated on sheet Comm_PE, and using that count in each worksheet in the loop.
If this is what you want, then that is perfectly OK, just make sure it is what you want.

Regards
 
Upvote 0
Thanks for the reply. Thank goodness all the named ranges contain the same number of rows

If they were different, would I change the code as follows where Comm_Br15 is the last range to be copied?

LastRow = Sheets("Comm_PE").Cells(Sheets("Comm_Br15").Rows.Count, "A").End(xlUp).Row
 
Upvote 0
You would change it to this instead:

Code:
LastRow = Worksheets(i).Cells(Worksheets(i).Rows.Count, "A").End(xlUp).Row

There were a few issues with the code that I cleaned up...

1st, if the row size ever did change, you would have received an error due to size mismatch.
2nd the copied data was overwriting any existing data, but only for the current row size, which would also cause headaches if the row size ever changed.
3rd removed redundant protecting/unprotecting of current worksheet

I put in a Msgbox to reflect the above changes; comment it out once you are sure your code is working the way it is supposed to.

Tested:
Code:
 Sub Copy_All_Comms()

    'Clear_Sheets
    Dim i As Integer, LastRow As Long
        
    'Loop Through Worksheets
    For i = 3 To Worksheets.Count
    
        'Unprotect Worksheet
        Worksheets(i).Unprotect
        
            'Find the last row in Column A in Current WorkSheet
            LastRow = Worksheets(i).Cells(Worksheets(i).Rows.Count, "A").End(xlUp).Row
            
            'Comment this next line out, It is only for Debugging
            If LastRow > 1 Then MsgBox """" & Worksheets(i).Name & """ has " & LastRow & " rows of data. Copied data will be appended starting at row " & LastRow + 1
 
            'Use The Worksheet Name to Return The Named Range
            Range(Worksheets(i).Name).Copy
            
            'In Worksheet(i), Copy The Above Named Range Into First Unused Cell
            Worksheets(i).Range("A" & LastRow).Offset(1).PasteSpecial Paste:=xlPasteValues
            
                       
            'Work with The Current Unprotected Sheet:
            With Worksheets(i)
                'Hide Columns D,F,H,&L
                .Range("D:D").EntireColumn.Hidden = True
                .Range("F:F").EntireColumn.Hidden = True
                .Range("H:H").EntireColumn.Hidden = True
                .Range("L:L").EntireColumn.Hidden = True
                'Resize Column C
                .Columns("C:C").ColumnWidth = 11
            End With
            
        'ReProtect The Sheet
        Worksheets(i).Protect
    Next i
        
End Sub

Regards
 
Upvote 0
Thanks very much for the update. This is most appreciated
 
Upvote 0

Forum statistics

Threads
1,215,475
Messages
6,125,028
Members
449,205
Latest member
Eggy66

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