Results 1 to 5 of 5

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

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2012
    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
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    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

    Thread Starter
    New Member
    Join Date
    Aug 2012
    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 2004
    Posts
    25,618

    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
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

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

    VBA question moved to Office Development

Posting Permissions

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



Click Here to Expand Forum to Full Width