Results 1 to 5 of 5

Thread: VBA coding help [Adding new worksheets before the last sheet in Excel | Renaming WS]

  1. #1
    New Member
    Join Date
    Aug 12
    Posts
    3

    VBA coding help [Adding new worksheets before the last sheet in Excel | Renaming WS]

    Hello

    I wish to design a VBA that allows me to alter the number of sheets in a template excel file
    The template excel file contains a set of blank sheets, and a final summary sheet at the end, hence I wish the VBA to adjust the number before the summary sheet

    If possible, I would also like the VBA to rename all the blank to the specific dates ie ddmmyyyy

    Ideally, the VBA is designed within excel, and I could enter the number of blank sheet I want, and the range of the dates, and the VBA detects it and adjust my template sheet automatically

    Many thanks

    Thsi is what I have so far, I'm a programming noob

    Sub newsheet()
    Dim newsheet As Worksheet

    Set wks = Worksheets.Add(after:=Worksheets(Worksheets.Count - 1))
    wks.Name = "Sheet" & Worksheets.Count + 1
    With wks
    .Cells(1, 1) = "TTL Corp."
    .Cells.Font.Bold = True
    .Cells(1, 6) = Date
    End With
    End Sub

  2. #2
    Frenzied Member
    Join Date
    Sep 02
    Location
    Columbus, Ohio
    Posts
    1,808

    Re: VBA coding help [Adding new worksheets before the last sheet in Excel | Renaming

    A very handy way of getting an idea of the code you need is to do what you want while recording a macro. Then you can look at the code that was generated. About half way down this link in the "How do I write code to … ? [a.k.a. recording macro's]" section is an explanation.

    http://www.vbforums.com/showthread.p...6-(or-VB5-VBA)

  3. #3
    New Member
    Join Date
    Aug 12
    Posts
    3

    Re: VBA coding help [Adding new worksheets before the last sheet in Excel | Renaming

    Quote Originally Posted by TysonLPrice View Post
    A very handy way of getting an idea of the code you need is to do what you want while recording a macro. Then you can look at the code that was generated. About half way down this link in the "How do I write code to … ? [a.k.a. recording macro's]" section is an explanation.

    http://www.vbforums.com/showthread.p...6-(or-VB5-VBA)
    Thank you, will give that a go

  4. #4
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,520

    Re: VBA coding help [Adding new worksheets before the last sheet in Excel | Renaming

    try like
    Code:
    cnt = 'number of sheets to add
    strtdate = ' date for first sheet name
    for i = 1 to cnt
      Set wks = Worksheets.Add(after:=Worksheets(Worksheets.Count - 1))
      wks.name = format(strtdate, "ddmmyyyy")
      strtdat = strtdate + 1
    next
    where strtdate is a date variable, cnt can be from a textbox, inputbox or any other user input
    sheets are, in this case, incremented daily, if you prefer weekly use + 7 instead, etc
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5
    Super Moderator Hack's Avatar
    Join Date
    Aug 01
    Location
    Searching for mendhak
    Posts
    58,283

    Re: VBA coding help [Adding new worksheets before the last sheet in Excel | Renaming

    VBA question moved to Office Development
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

Posting Permissions

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