Macro to Create a New Book and Rename

squeakums

Well-known Member
Joined
May 15, 2007
Messages
851
Office Version
  1. 365
I have a written macro that works but one little issue. It is setup to create a new excel worksheet (BOOK1) and then rename (BOOK1) to current date. Although, the user of the macro has decided that he/she wants to run it several times while its still open. In doing that the next time it tries to run it creates (BOOK2), not (BOOK1). I explained he/she would have to close the macro and reopen it and run it again so that it doesn't error our (which it does, if he/she keeps it open and runs it again, because it cannot rename (BOOK1) since (BOOK2) is now the next spreadsheet. How can I either tell it to always create (BOOK1) or change the other parts of the macro to tell it to take the current active spreadsheet and rename tab what I want it to be.

**apparently I have to make the macro super easy friendly...

Thanks.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
You can build a check within the code to see if the workbook with a specific name (in your case the date) is already open, if so activate that workbook, if not create it.
 
Upvote 0
I wouldn't want it to re-activate the same worksheet it would need to create in another spreadsheet (BOOK1) which doesn't exist anymore since it was renamed.
 
Upvote 0
Could you please supply your code?
 
Last edited:
Upvote 0
Sure, here it is:

Code:
Sub Macro2()
'
' Macro2 Macro
'
    Workbooks.Add
    Windows("Macro Ariba Tree Master and Sub.xlsm").Activate
    Windows("Book1").Activate
    Windows("Macro Ariba Tree Master and Sub.xlsm").Activate
    Sheets("Ariba Tree").Select
    Range("A1:M1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Windows("Book1").Activate
    Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
        , SkipBlanks:=False, Transpose:=False
    ActiveWindow.DisplayGridlines = False
    Range("A1").Select
    Windows("Macro Ariba Tree Master and Sub.xlsm").Activate
    Windows("Book1").Activate
    Windows("Macro Ariba Tree Master and Sub.xlsm").Activate
    Columns("A:M").Select
    Sheets("Ariba Tree").Select
    Application.CutCopyMode = False
    Selection.Copy
    Windows("Book1").Activate
    Columns("A:M").Select
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False
    Range("A1").Select
    Windows("Macro Ariba Tree Master and Sub.xlsm").Activate
    Application.WindowState = xlMinimized
    Windows("Book1").Activate
    Columns("A:M").Select
    Selection.AutoFilter
    Range("A2").Select
    Application.WindowState = xlNormal
    Windows("Macro Ariba Tree Master and Sub.xlsm").Activate
    Range("A2").Select
    Windows("Book1").Activate
    Sheets("Sheet1").Select
    Sheets("Sheet1").Name = "Ariba Tree"
    Range("A1").Select
    ActiveSheet.Name = ActiveSheet.Name & " " & Format(Date, "(mm-dd-yy)")

Windows("Macro Ariba Tree Master and Sub.xlsm").Activate
Sheets("Macro").Select
MsgBox "Macro Done!"
'
End Sub
 
Last edited by a moderator:
Upvote 0
When posting code please use code tags (the # icon in the reply window).

Try
Code:
Sub Macro2()

   Dim Wbk As Workbook
   
   Set Wbk = Workbooks.Add
   
   Windows("Macro Ariba Tree Master and Sub.xlsm").Activate
   Wbk.Activate
   Windows("Macro Ariba Tree Master and Sub.xlsm").Activate
   Sheets("Ariba Tree").Select
   Range("A1:M1").Select
   Range(Selection, Selection.End(xlDown)).Select
   Selection.Copy
   Wbk.Activate
   Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
      , SkipBlanks:=False, Transpose:=False
   ActiveWindow.DisplayGridlines = False
   Range("A1").Select
   Windows("Macro Ariba Tree Master and Sub.xlsm").Activate
   Wbk.Activate
   Windows("Macro Ariba Tree Master and Sub.xlsm").Activate
   Columns("A:M").Select
   Sheets("Ariba Tree").Select
   Application.CutCopyMode = False
   Selection.Copy
   Wbk.Activate
   Columns("A:M").Select
   Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
      SkipBlanks:=False, Transpose:=False
   Application.CutCopyMode = False
   Range("A1").Select
   Windows("Macro Ariba Tree Master and Sub.xlsm").Activate
   Application.WindowState = xlMinimized
   Wbk.Activate
   Columns("A:M").Select
   Selection.AutoFilter
   Range("A2").Select
   Application.WindowState = xlNormal
   Windows("Macro Ariba Tree Master and Sub.xlsm").Activate
   Range("A2").Select
   Wbk.Activate
   Sheets("Sheet1").Select
   Sheets("Sheet1").Name = "Ariba Tree"
   Range("A1").Select
   ActiveSheet.Name = ActiveSheet.Name & " " & Format(Date, "(mm-dd-yy)")
   
   Windows("Macro Ariba Tree Master and Sub.xlsm").Activate
   Sheets("Macro").Select
   MsgBox "Macro Done!"
'
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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