don't copy specific object

ashani

Active Member
Joined
Mar 14, 2020
Messages
345
Office Version
  1. 365
Platform
  1. Windows
hi everyone,

I'd really appreciate if someone could put me in teh right direction. I'm using the following Macro to copy the sheet, however I don't want to copy specific Object name "Setday" into new sheet. However, at the moment with the following code - it's not copying any object but I only want that "Setday" not to be copied.

Please can someone help me.

Thank you

VBA Code:
Dim wks As Worksheet
    Set wks = ActiveSheet
    Application.CopyObjectsWithCells = False
    ActiveSheet.Copy After:=Worksheets(Sheets.Count)
    If wks.Range("a3").Value <> "" Then
    On Error Resume Next
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
The below code copies the entire sheet including any object and removes the desired object afterwards.

VBA Code:
    Dim oWs As Worksheet, oWsNew As Worksheet, oShp As Shape
    
    Set oWs = ActiveSheet
    oWs.Copy After:=Sheets(Sheets.Count)
    Set oWsNew = ActiveSheet
    For Each oShp In oWsNew.Shapes
        If StrComp(oShp.Name, "Setday", vbTextCompare) = 0 Then
            oShp.Delete
            Exit For
        End If
    Next
 
Upvote 0
Hi
is it possible to have a message box appears if the sheet name has already taken rather than a VBA error message ?
many thanks
 
Upvote 0
Here is the code I'm currently using.

VBA Code:
Sub Newday()
     
    Dim oWs As Worksheet, oWsNew As Worksheet, oShp As Shape
   
    Set oWs = ActiveSheet
    oWs.Copy After:=Sheets(Sheets.Count)
    Set oWsNew = ActiveSheet
    ActiveSheet.Unprotect Password:=“abc”
    For Each oShp In oWsNew.Shapes
        If StrComp(oShp.Name, "Setday", vbTextCompare) = 0 Then
            oShp.Delete
    oWsNew.Name = Format(Range("R1").Value, ("ddmm"))
    oWsNew.Range("A2:N43").Locked = True
    oWsNew.Protect Password:=“abc”
            Exit For
        End If
    Next
    
    Sheets("Main").Select
 
Upvote 0
Have been offline awhile until now, so a delayed response...
Try the below code. Please note the separate SheetExists function.

VBA Code:
Sub Newday()
     
    Dim oWs As Worksheet, oWsNew As Worksheet, oShp As Shape, sShtName As String
   
    Set oWs = ActiveSheet
    oWs.Copy After:=Sheets(Sheets.Count)
    Set oWsNew = ActiveSheet
    
    With oWsNew
        
        sShtName = Format(.Range("R1").Value, ("ddmm"))
        
        If Not SheetExists(.Parent, sShtName) Then
        
            .Unprotect Password:="abc"
            .Name = sShtName
            .Range("A2:N43").Locked = True
            
            For Each oShp In .Shapes
                If StrComp(oShp.Name, "Setday", vbTextCompare) = 0 Then
                    oShp.Delete
                    Exit For
                End If
            Next
            .Protect Password:="abc"
            
        Else
            MsgBox "There is already a sheet with the name " & sShtName, vbExclamation
        
        End If
    End With
    Sheets("Main").Select
End Sub


Function SheetExists(ByVal argWb As Workbook, ByVal argShtName As String) As Boolean
    Dim oWs As Worksheet
    If Not argWb Is Nothing And Len(argShtName) > 0 Then
        For Each oWs In argWb.Worksheets
            If StrComp(oWs.Name, argShtName, vbTextCompare) = 0 Then
                SheetExists = True
                Exit For
            End If
        Next oWs
    End If
End Function
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,729
Members
449,049
Latest member
MiguekHeka

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