VBA to avoid duplicate sheet

Shadkng

Active Member
Joined
Oct 11, 2018
Messages
365
Hi, the below code creates a new sheet "PARTS_LIST_CLUTCH." Sometimes I have to run the code again and I get a debug because the sheet name already exists. Is it tpossible to delete the sheet and re-create it? Or if we could just overwrite it that would be fine too. Thanks for the help.

VBA Code:
Sub PARTS_LIST_CLUTCH()  'steve added 10-26-21
Application.ScreenUpdating = False

If Sheets("FINALORDER").Range("B23").Value = "SR" Then '10/17/2019 - Added "SR" Condition
    Exit Sub
    Else
End If

Sheets("PARTS LIST").Copy after:=Sheets("PARTS LIST")
ActiveSheet.Name = "PARTS LIST CLUTCH"
Range("A7:F300").Clear
Range("E1:F5").Clear
ActiveSheet.Buttons.Delete

Worksheets("PARTS LIST").Range("E7:F50").Copy
Worksheets("PARTS LIST CLUTCH").Range("A11").PasteSpecial


Range("A1") = "** PARTS LIST CLUTCH ASSEMBLY **"
Range("A8") = "PACKED BY:________________"
Range("A6").HorizontalAlignment = xlLeft
Range("A6") = "DATE:________________"
Range("C8") = "CHECKED BY:________________"
Range("a1").Font.Size = 24
Columns("A:A").AutoFit
Columns("C:C").AutoFit
Columns("A:A").ColumnWidth = Columns("A:A").ColumnWidth + 1
Columns("B:B").HorizontalAlignment = xlCenter
Range("A10").Select

Application.ScreenUpdating = True

End Sub
 
If you don't want that cleared everytime , you can move it into the If statement, like
VBA Code:
Sub PARTS_LIST_CLUTCH()  'steve added 10-26-21
Application.ScreenUpdating = False
 Dim Sht As Worksheet
If Sheets("FINALORDER").Range("B23").Value = "SR" Then '10/17/2019 - Added "SR" Condition
    Exit Sub
    Else
End If
If Not Evaluate("isref('PARTS LIST CLUTCH'!a1)") Then
   Sheets("PARTS LIST").Copy after:=Sheets("PARTS LIST")
   ActiveSheet.Name = "PARTS LIST CLUTCH"
   With Sheets("PARTS LIST CLUTCH")
      .Range("A7:F300").Clear
      .Range("E1:F5").Clear
      .Buttons.Delete
   End With
Else
   Sheets("PARTS LIST CLUTCH").UsedRange.Clear
End If
With Sheets("PARTS LIST CLUTCH")
   Worksheets("PARTS LIST").Range("E7:F50").Copy
   .Range("A11").PasteSpecial


   .Range("A1") = "** PARTS LIST CLUTCH ASSEMBLY **"
   .Range("A8") = "PACKED BY:________________"
   .Range("A6").HorizontalAlignment = xlLeft
   .Range("A6") = "DATE:________________"
   .Range("C8") = "CHECKED BY:________________"
   .Range("a1").Font.Size = 24
   .Columns("A:A").AutoFit
   .Columns("C:C").AutoFit
   .Columns("A:A").ColumnWidth = Columns("A:A").ColumnWidth + 1
   .Columns("B:B").HorizontalAlignment = xlCenter
End With

Application.ScreenUpdating = True

End Sub
 
Upvote 0

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
If you don't want that cleared everytime , you can move it into the If statement, like
VBA Code:
Sub PARTS_LIST_CLUTCH()  'steve added 10-26-21
Application.ScreenUpdating = False
 Dim Sht As Worksheet
If Sheets("FINALORDER").Range("B23").Value = "SR" Then '10/17/2019 - Added "SR" Condition
    Exit Sub
    Else
End If
If Not Evaluate("isref('PARTS LIST CLUTCH'!a1)") Then
   Sheets("PARTS LIST").Copy after:=Sheets("PARTS LIST")
   ActiveSheet.Name = "PARTS LIST CLUTCH"
   With Sheets("PARTS LIST CLUTCH")
      .Range("A7:F300").Clear
      .Range("E1:F5").Clear
      .Buttons.Delete
   End With
Else
   Sheets("PARTS LIST CLUTCH").UsedRange.Clear
End If
With Sheets("PARTS LIST CLUTCH")
   Worksheets("PARTS LIST").Range("E7:F50").Copy
   .Range("A11").PasteSpecial


   .Range("A1") = "** PARTS LIST CLUTCH ASSEMBLY **"
   .Range("A8") = "PACKED BY:________________"
   .Range("A6").HorizontalAlignment = xlLeft
   .Range("A6") = "DATE:________________"
   .Range("C8") = "CHECKED BY:________________"
   .Range("a1").Font.Size = 24
   .Columns("A:A").AutoFit
   .Columns("C:C").AutoFit
   .Columns("A:A").ColumnWidth = Columns("A:A").ColumnWidth + 1
   .Columns("B:B").HorizontalAlignment = xlCenter
End With

Application.ScreenUpdating = True

End Sub
Hi Fluff, I went back to my original code below and added 3 lines (see bold) that I got on the web. I guess it's deleting the "PARTS LIST CLUTCH" every time which is fine and my code is just recreating it. This seems to be working, except I get a pop up box confirming if I want to delete. Maybe we can go this route and you can fix the code or sub something in. Thanks


VBA Code:
sub PARTS_LIST_CLUTCH()  'steve added 10-26-21
Application.ScreenUpdating = False

If Sheets("FINALORDER").Range("B23").Value = "SR" Then '10/17/2019 - Added "SR" Condition
    Exit Sub
    Else
End If

[B]On Error Resume Next
    ThisWorkbook.Worksheets("PARTS LIST CLUTCH").Delete
    On Error GoTo 0[/B]


Sheets("PARTS LIST").Copy after:=Sheets("PARTS LIST")
ActiveSheet.Name = "PARTS LIST CLUTCH"
Range("A7:F300").Clear
Range("E1:F5").Clear
ActiveSheet.Buttons.Delete

Worksheets("PARTS LIST").Range("E7:F50").Copy
Worksheets("PARTS LIST CLUTCH").Range("A11").PasteSpecial


Range("A1") = "** PARTS LIST CLUTCH ASSEMBLY **"
Range("A8") = "PACKED BY:________________"
Range("A6").HorizontalAlignment = xlLeft
Range("A6") = "DATE:________________"
Range("C8") = "CHECKED BY:________________"
Range("a1").Font.Size = 24
Columns("A:A").AutoFit
Columns("C:C").AutoFit
Columns("A:A").ColumnWidth = Columns("A:A").ColumnWidth + 1
Columns("B:B").HorizontalAlignment = xlCenter
Range("A10").Select

Application.ScreenUpdating = True

End Sub
 
Upvote 0
What's wrong with the code in post#11?
 
Upvote 0
What's wrong with the code in post#11?
It ran OK the first time, but the 2nd time I lost data at the top of the form and the formatting was off. I found the below code which works perfectly so far.

VBA Code:
Application.DisplayAlerts = False
On Error Resume Next
    ThisWorkbook.Worksheets("PARTS LIST CLUTCH").Delete
    On Error GoTo 0
Application.DisplayAlerts = True
 
Upvote 0
Solution
Glad you sorted it & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,823
Messages
6,121,779
Members
449,049
Latest member
greyangel23

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