+ Reply to Thread
Results 1 to 4 of 4

Automatically create a copy of Master sheet when data entered into specific rows

  1. #1
    Registered User
    Join Date
    07-28-2014
    Location
    Perth, Australia
    MS-Off Ver
    2019
    Posts
    31

    Automatically create a copy of Master sheet when data entered into specific rows

    Hello all,

    I am wondering if it is possible to automatically create a copy of the "Master Do Not Delete" sheet when data is entered in rows A4-80 on "Staff" sheet? The newly created sheet should be automatically named the same as the data entered into the previous mentioned rows. And I think it would be good for "Master Do Not Delete" to be a hidden sheet so no one can delete it accidentally.

    So to give an example of what I'm thinking:

    "Mike" is entered into Staff!A4. When this is entered, Excel automatically copies Master Do Not Delete! and renames it "Mike" and places this newly created sheet at the end of all sheets.

    I hope this makes sense.
    Attached Files Attached Files
    Last edited by itsawayoflife; 09-21-2018 at 12:10 AM.

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: Automatically create a copy of Master sheet when data entered into specific rows

    I could not reverse engineer what you wanted to do with the "cosmetics." I did modify the conditions under which the code fires. Select more than one cell and nothing happens. Then it looks to see if the cell being changed is below the original last row and it is not blank. I then do a check to make sure that a sheet with that name does not already exist and go about creating it. Then I made an attempt to do what I think you wanted for the cosmetics.

    Please Login or Register  to view this content.
    Attached Files Attached Files
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  3. #3
    Registered User
    Join Date
    07-28-2014
    Location
    Perth, Australia
    MS-Off Ver
    2019
    Posts
    31

    Re: Automatically create a copy of Master sheet when data entered into specific rows

    Thanks for that, dflak! I love that it can make a new sheet with the name I put in! That works perfect for me and is exactly what I was thinking. I'm super new to Excel and this thing amazes me more and more. Haha!

    I started playing around with it a bit and noticed a few things:

    1) when I changed the "Holiday Week Variables" (Staff!AT9) from 7 to 5, it created a new sheet named "5".
    2) when I input dates in the "Staff Holidays Approved" (Staff!B4:M80) the code doesn't like that I use a "/" and gives an error.

    Is there any way to amend the code so that it only looks for changes to Staff!A4:A80? And can it only create a new sheet when the cell goes from blank to having a name in it?

    3) When I erased all the names in the A column then tried it out to add a name to it (in cell A5), all of a sudden it has hidden rows A7:A80. This is a problem because it then hides the calendar which I would not like hidden. Is it possible to have it hide only rows A22:A80?

    The idea of the sheet is to be able to see, at a glance, when too many people are away on holidays based upon the variables on the right side of Staff!. The calendar changes when the staff holidays approved start/end = the variable set on the right side.

    The people using this document are very, very uneducated in excel so I am trying to make it as simple for them as possible. Hence the auto add and name new sheet, the hiding rows until they are ready to put a name in it.

    So far, with the help of this amazing community, everything has worked perfectly and now the last bit is to have the auto add and name new sheet going.

    Thanks so much for your help!

  4. #4
    Registered User
    Join Date
    07-28-2014
    Location
    Perth, Australia
    MS-Off Ver
    2019
    Posts
    31

    Re: Automatically create a copy of Master sheet when data entered into specific rows

    Found the problem and fixed it. Now it works as desired... Thank you!

    Option Explicit

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim lastRow As Long ' Last row this sheet
    Dim sh As Worksheet ' This worksheet

    If Target.Cells.Count > 1 Then Exit Sub

    ' Find the last row of data
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row

    If Target.Column = 1 And Target.Address <> "$A$" & lastRow + 1 And Target.Value <> "" Then

    Application.ScreenUpdating = False

    If SheetExists(Target.Value) = True Then
    MsgBox "Sheet " & Target.Value & " already exists." & Chr(10) & "Exiting program.", _
    vbOKOnly + vbCritical, "Sheet Exists"
    Application.EnableEvents = False
    Target.ClearContents
    Application.EnableEvents = True
    Exit Sub
    End If

    Set sh = ActiveSheet

    ' copy the Master Do Not Delete sheet
    Sheets("Master Do Not Delete").Copy after:=Sheets(Sheets.Count)
    Sheets(Sheets.Count).Name = Target.Value
    Sheets(Sheets.Count).Visible = True

    ' Cosmetics
    sh.Select
    Cells(lastRow + 1, "A").Select

    ' Hide rows
    Range(Cells(1, "A"), Cells(80, "A")).EntireRow.Hidden = False
    If lastRow < 80 Then
    Range(Cells(Application.WorksheetFunction.Max(23, lastRow + 2), "A"), Cells(80, "A")).EntireRow.Hidden = True
    End If

    Application.ScreenUpdating = True

    End If
    End Sub

    Public Function SheetExists(SheetName As String) As Boolean
    Dim sh As Worksheet

    For Each sh In ActiveWorkbook.Sheets
    If sh.Name = SheetName Then
    SheetExists = True
    Exit Function
    End If
    Next sh
    SheetExists = False
    End Function

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 19
    Last Post: 10-16-2017, 12:34 PM
  2. Automatically add multiple new rows when data is entered into the sheet.
    By Hyflex in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-20-2014, 12:22 AM
  3. Replies: 1
    Last Post: 02-26-2014, 08:54 PM
  4. [SOLVED] Copy Data from one sheet to another when a specific value is entered
    By kittu55 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-15-2013, 12:45 PM
  5. [SOLVED] Automatically copy data from one worksheet to another based on a specific word entered
    By arkadd61 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-26-2013, 08:23 PM
  6. Automatically add rows and copy formulas when data entered
    By alina_t in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-22-2011, 02:30 PM
  7. Excel 2007 : Automatically Copy Data from Master Sheet
    By andy9988 in forum Excel General
    Replies: 7
    Last Post: 10-13-2010, 01:04 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1