Unprotect and Paste Values - errors if sheet deleted.

mardeebo

New Member
Joined
Aug 13, 2004
Messages
39
Office Version
  1. 2016
Platform
  1. Windows
  2. Mobile
  3. Web
Acquired a workbook with this VBA code. The workbook has 25+ tabs, 5 of which are always named exactly the same but the remaining will contain something like "Day 1 XX YY" or "Day 2 YY ZZ". And, just for fun, sometimes its "XX YY Day 1" and "YY ZZ Day 2". In other words, the bulk of the tabs can have varying names in them but they do always have the word "Day" in it.

Sometimes the person doing the data entry deletes or renames a sheet and then when I run the macro (or, rather, a set of macros), it bombs out on the line WS.Select. I'd really like to rewrite this so it's not so fussy. How could I write this so it will do the steps for any worksheet containing the word "day" in the sheet name?

VBA Code:
Sub UnprotectANDPasteValues()
Dim iCurWS As Integer
Dim WS As Worksheet


For iCurWS = 6 To Worksheets.Count - 1
    Set WS = Sheets(iCurWS)
    WS.Select
    ActiveSheet.Unprotect
    Cells.Select
    Selection.EntireColumn.Hidden = False
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Columns("T:T").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft
    Columns("W:W").Select
    Selection.Delete Shift:=xlToLeft
    Columns("Z:Z").Select
    Selection.Delete Shift:=xlToLeft

Next iCurWS

iCurWS = 6
Set WS = Sheets(iCurWS)

WS.Select

End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Updated, best not to select ether so removed that.

VBA Code:
Sub UnprotectANDPasteValues()

Dim WS As Worksheet

For Each WS In ThisWorkbook.Worksheets
    If WS.Name Like "*Day*" Then
    WS.Unprotect
    WS.Cells.EntireColumn.Hidden = False
    WS.Cells.Copy
    WS.Cells.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    WS.Columns("T:T").Delete Shift:=xlToLeft
    WS.Columns("W:W").Delete Shift:=xlToLeft
    WS.Columns("Z:Z").Delete Shift:=xlToLeft
    End If
Next WS

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,912
Messages
6,122,200
Members
449,072
Latest member
DW Draft

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