Consulting

Results 1 to 4 of 4

Thread: Add week ending date to newly created and renamed sheet?

  1. #1

    Add week ending date to newly created and renamed sheet?

    Hiya

    I haven't used VBA properly for 2 years or so now( used to use it daily)..

    I have a very simple problem that can be solved 2 ways, neither of which I know how to do lol

    I have a macro that copies a sheet and forces user to rename it, which works fine (its a weekly sheet)

    On the sheet is cell C3 which has the "Week Ending Date".

    I have tried to

    a) insert a formula that looks up the previous week and adds 7 ..BUT..the problem I have is that the formula in VBA specifies the sheet name..this will work once, but next time macro used it will still look at this "old" sheet rather than the previous weeks if that makes sense (ie always goes back to sheet "we310508" and adds 7 days to that date)

    b) I have managed to get an input box up asking user to enter date in correct format in C3.. they can type in the date in this input box but it doesnt populate anything? I want what they type in the input box to populate cell c3?

    sorry, easy stuff I know, but been a while lol

    this is my amateurish code so far

    [VBA]
    Sheets.Add
    ActiveSheet.Name = InputBox("Give name.")
    ActiveSheet.Next.Select
    Range("A1:U49").Select
    Selection.Copy
    ActiveSheet.Previous.Select
    Range("A1").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Columns("C:C").Select
    Columns("C:C").EntireColumn.AutoFit
    Range("C3").Select
    Selection.Delete
    Value = UserEntry = InputBox("Please enter week end date in dd/mm/yy format.")[/VBA]

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Dim sh As Worksheet
    Dim this As Worksheet
    Dim inVal As Date
    Set this = ActiveSheet
    Set sh = Worksheets.Add(after:=Worksheets(Worksheets.Count))
    sh.Name = InputBox("Give name.")
    this.Range("A1:U49").Copy sh.Range("A1")
    sh.Columns("C:C").AutoFit
    inVal = CDate(InputBox("Please enter week end date in dd/mm/yy format."))
    sh.Range("C3").Value = inVal
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Perhaps this would work.
    Dim oldSheet As Worksheet, newSheet As Worksheet
    Dim userInput As String
    
    Set oldSheet = ActiveSheet
    
    With ThisWorkbook
        oldSheet.Copy after:=.Sheets(.Sheets.Count)
        Set newSheet = ActiveSheet
    End With
    
    userInput = Application.InputBox("New Sheet's Name?", Default:=newSheet.Name, Type:=2)
    If userInput = "False" Then Exit Sub: Rem cancel pressed
    
    With newSheet
        .Name = userInput
        .Range("c3").Value = Format(7 + oldSheet.Range("c3").Value, "mm/dd/yy")
        .Columns("C:C").EntireColumn.AutoFit
    End With

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    If you are normally dealing with the current week, you could add a default value
    [vba]WkEnd = Date + (7 - Day(Date))
    inval = CDate(InputBox("Please enter week end date in dd/mm/yy format.", , WkEnd))

    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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