duplicating spreadsheet without losing formulas in cells

Ian1976

Board Regular
Joined
Feb 4, 2016
Messages
139
Office Version
  1. 365
Platform
  1. Windows
Hi I'm using this to create/duplicate a new worksheet, is there a way I can protect the formulas which get deleted currently?
There are formulas in F4, J4, N4, R4, V4, Z4, AD4

Thanks

Sub Button1_Click()
ActiveSheet.Copy after:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = GetName

Range("D4:AE29").Select
Selection.ClearContents
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Range("D4:E4").Select

End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
You are explicitly deleting them in your code. Expand this range reference to exclude the cells you want to keep.
VBA Code:
Range("$D$5:$AE$29,$AE$4,$AA$4:$AC$4,$W$4:$Y$4,$O$4:$U$4,$K$4:$M$4,$G$4:$I$4,$D$4:$E$4").Select
 
Upvote 0
So that line selects the whole area then for each selection after it "deselects" that cell?
 
Upvote 0
Slight adjustment to Jeff's code line as it was missing R4
VBA Code:
Range("$D$5:$AE$29,$AE$4,$AA$4:$AC$4,$W$4:$Y$4,$O$4:$Q$4,$S$4:$U$4,$K$4:$M$4,$G$4:$I$4,$D$4:$E$4").Select
 
Upvote 0
Try this:
VBA Code:
Sub Button1_Click()
Dim cell As Range
ActiveSheet.Copy after:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = GetName

Range("D4:AE29").Select
For Each cell In Selection
    If cell.HasFormula = False Then
        cell.ClearContents
        With cell.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
        .PatternTintAndShade = 0
        End With
    End If
Next cell
Range("D4:E4").Select

End Sub
 
Upvote 0
@Skyybot I would turn screenupdating off if you are going to clear the contents of the cells and format them individually
 
Upvote 1
Although I am unsure by post 1 which cells are needed to be formatted another possible option (please note that we will have to add error handling if there is ever a chance that there are no constants within the range)

VBA Code:
Sub Button1_Click()
    ActiveSheet.Copy after:=Sheets(Sheets.Count)
    Sheets(Sheets.Count).Name = GetName

    With Range("D4:AE29")
        .SpecialCells(xlCellTypeConstants, 23).ClearContents
       
        With .Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
       
    End With
    Range("D4:E4").Select
 
End Sub
 
Last edited:
Upvote 0
Solution
Thanks all, the protected cells were on every other row- F4, F6, F8 through to F28, and the same for columns J & N .......

Can I add any kind of protection to stop users from deleting the formulas manually, taking into consideration that I presume the cells need to be unprotected when the macro makes an new sheet?

Thanks for your help! (y)
 
Last edited:
Upvote 0
Protect the source sheet as normal, then turn off the protection near the start of the code and back on again at the end

Rich (BB code):
Sub Button1_Click()
    ActiveSheet.Copy after:=Sheets(Sheets.Count)
    Sheets(Sheets.Count).Name = "New Sheet"

    With Range("D4:AE29")
        ActiveSheet.Unprotect "Your Password"
    
        On Error Resume Next
        .SpecialCells(xlCellTypeConstants, 23).ClearContents
        On Error GoTo 0
        
        With .Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
        
    End With
    
    Range("D4:E4").Select
  
    ActiveSheet.Protect "Your password"
  
End Sub
 
Upvote 1

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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