+ Reply to Thread
Results 1 to 6 of 6

Macro to Add sheet after last sheet

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2024
    Posts
    2,873

    Macro to Add sheet after last sheet

    I have tried to write code to create a sheet to have the same name as in cell A1 on sheet1 ("Data") and to copy all the values and formats


    For eg if A1 on sheet1 (named "Data") A1 is July-2019 , then sheet "July-2019" is to be created after the last sheet and the data copied and pasted as values and the format also to be the same


    I get a run time error when running the macro

     Sub AddSheet_Names()
      Dim MyCell As Range, MyRange As Range
    
     Set MyRange = Sheets("Data").Range("A1")
     Set MyRange = Range(MyRange, MyRange.End(xlDown))
    
     For Each MyCell In MyRange
     Sheets.Add After:=Sheets(Sheets.Count) 'creates a new worksheet
     Sheets(Sheets.Count).Name = MyCell.Value ' renames the new worksheet
     Next MyCell
    End Sub

    I have attached sample Data

    It would be appreciated if someone could assist me

  2. #2
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,944

    Re: Macro to Add sheet after last sheet

    If you're copy all data and formats from Sheet1 to the newly created sheet, why not just copy it then stamp values over the formulas?

    Try this:
    Sub CopySheetToEnd()
        Sheets("Data").Copy After:=Sheets(Sheets.Count)
        With Sheets(Sheets.Count)
            .Name = Format(Sheets("Data").Range("A1"), "mmm yyyy")
            .UsedRange.Value = .UsedRange.Value
        End With
    End Sub
    Does that work for you?

    BSB

  3. #3
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2024
    Posts
    2,873

    Re: Macro to Add sheet after last sheet

    Thanks for appreciated the help, much appreciated

    I would like to know how to save the sheet once copied to desktop using VBA

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Macro to Add sheet after last sheet

    Sub test()
        Sheets("data").Copy , Sheets(Sheets.Count)
        With Sheets(Sheets.Count)
            .Name = .[a1].Text
            .Copy
            With ActiveWorkbook
                .SaveAs CreateObject("WScript.Shell").SpecialFolders("desktop") & _
                        "\" & .Sheets(1).[a1].Text & ".xlsx"
                .Close False
            End With
        End With
    End Sub

  5. #5
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2024
    Posts
    2,873

    Re: Macro to Add sheet after last sheet

    Thanks Jindon Works perfectly

  6. #6
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2024
    Posts
    2,873

    Re: Macro to Add sheet after last sheet


+ 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. [SOLVED] Macro updating after selecting another sheet and back to Macro enabled sheet?
    By Imran Magsi in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-29-2016, 04:38 AM
  2. macro to get sum of sheet 1 subtract with sum of sheet 2 and input value of sheet 3
    By danwoltrs in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-15-2015, 04:41 PM
  3. [SOLVED] Macro to copy data from a master sheet to separate sheet as per date using a macro
    By tmaster81 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-23-2014, 08:05 AM
  4. [SOLVED] Macro to Copy Data from one Sheet A to Sheet B based on value in cell on sheet A
    By scass in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-11-2012, 07:21 PM
  5. Replies: 1
    Last Post: 07-30-2012, 02:35 PM
  6. [SOLVED] When sheet is activated, execute macro in other sheet and return to sheet
    By timtim89 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-16-2012, 10:50 AM
  7. Macro: Count rows in sheet A, copy count in sheet B, paste offset in sheet A
    By Agent:Orange in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-19-2011, 07:56 AM

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