prevent add a new sheet based on cell value

MKLAQ

Active Member
Joined
Jan 30, 2021
Messages
397
Office Version
  1. 2016
Platform
  1. Windows
hi

I would add some lines to this code when I add a new sheet if from the first time then should add and if I repeat again add a new sheet then should prevent add a new sheet until change the value in D3 it shouldn't add a new sheet contains for the same value in d3 for more than sheet .
VBA Code:
Sub CreateNewSheet()
Dim dDate As Long
Dim oNum As String
Dim lst As Long

sheet1.Copy After:=Sheets(Sheets.Count)

With ActiveSheet
    dDate = .Range("B3").Value
    oNum = .Range("D3").Value
    lst = .Range("A" & Rows.Count).End(xlUp).Row
    .Columns(3).Insert
    .Columns("1").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    .Range("A4").Value = "date"
    .Range("D4").Value = "order"
    .Range("A5:A" & lst).Value = Format(dDate, "dd/mm/yyyy")
    .Range("D5:D" & lst).Value = oNum
    .Rows("1:3").Clear
End With
End Sub
any help will truly appreciate
 
as to your code when I add a new sheet it shows me the message I have to change in d3 it's it' should not show the message in first time when I add a new sheet because based on my orginal code . it create headers and repeat values in some columns when I add a new sheet that's why I want I add a new sheet from the first time but when I change the value in d3 it still show message I can't add a new sheet despite I changed in d3 .

do you understand me what's the problem I face it.
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hopefully I've nailed it with the code below ...
Rich (BB code):
Sub CreateNewSheet_r3()

    Dim dDate As Long
    Dim oNum As String
    Dim lst As Long

    Dim Ws         As Worksheet
    Dim Proceed    As Boolean

    With Sheet1
        Proceed = True
        oNum = .Range("D3").Value
        For Each Ws In ThisWorkbook.Worksheets
            If Ws.Name <> .Name Then
                If Ws.Range("D5").Value <> oNum Then
                    Proceed = True
                Else
                    Proceed = False
                    Exit For
                End If
            End If
        Next Ws

        If Proceed Then

            .Copy After:=Sheets(Sheets.Count)
            With ActiveSheet
                dDate = .Range("B3").Value
                oNum = .Range("D3").Value
                lst = .Range("A" & Rows.Count).End(xlUp).Row
                .Columns(3).Insert
                .Columns(1).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
                .Range("A4").Value = "date"
                .Range("D4").Value = "order"
                .Range("A5:A" & lst).Value = Format(dDate, "dd/mm/yyyy")
                .Range("D5:D" & lst).Value = oNum
                .Rows("1:3").Clear
            End With
        Else
            
            MsgBox "Please change order number.", vbExclamation
        End If
    End With
End Sub
 
Upvote 0
Solution
fantasic ! you're genuis
thanks so much for a great assistance :)
 
Upvote 0
Sometimes patience pays off ... :unsure:
You're welcome and thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,428
Members
449,083
Latest member
Ava19

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