Consulting

Results 1 to 12 of 12

Thread: Solved: insert date with vba

  1. #1
    VBAX Tutor CCkfm2000's Avatar
    Joined
    May 2005
    Posts
    209
    Location

    Unhappy Solved: insert date with vba

    Hi all,

    What i'm looking for is same help with the following.

    Ask user what month & year which I can do.

    Create a new worksheet and insert the date for that month.

    Then create a new worksheet and insert the next month dates and so on for the next 12 months.

    Thanks

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Create a new worksheet and insert the date for that month.
    insert where?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Tutor CCkfm2000's Avatar
    Joined
    May 2005
    Posts
    209
    Location
    starting in cell a5 going down.

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    I'm sorry, I don't follow.

    could you post a workbook with an example of what you want?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

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

    Public Sub DateDrivenData()
    Dim sh As Worksheet
    Dim mYear As Long
    Dim mMonth As Long
    Dim mDate As Date
    Dim i As Long, j As Long

    'get year and month, I will pre-load to test
    mYear = 2008
    mMonth = 3

    For i = mMonth To mMonth + 11

    mDate = DateSerial(mYear, i, 1)
    Worksheets.Add
    ActiveSheet.Name = Format(mDate, "yyyy-mmm")
    For j = 0 To 30

    Range("A5").Offset(j, 0).Value = mDate + j
    If Month(mDate + j) <> Month(mDate + j + 1) Then Exit For
    Next j
    Next i
    End Sub
    [/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

  6. #6
    VBAX Tutor CCkfm2000's Avatar
    Joined
    May 2005
    Posts
    209
    Location
    thanks xld,

    i've attached a copy for reference.

  7. #7
    VBAX Tutor CCkfm2000's Avatar
    Joined
    May 2005
    Posts
    209
    Location
    can this code put the months on order?

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Are you saying my code did/did not work?

    What exactly are you saying in the last post?
    ____________________________________________
    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

  9. #9
    VBAX Tutor CCkfm2000's Avatar
    Joined
    May 2005
    Posts
    209
    Location
    yes it worked, thanks. just asking if the worksheets can be sorted in order of month?

  10. #10
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    It will take much longer to compute how to do that with VBA than to do it with formulas and do it by hand.

    Put this Formula in Cell "A8" and Fill, Series down to the end of month cell. Change the date value in the Formula for each month.
    =DATEVALUE("1-Mar-2010")
    Then put this formula in Cell "B8" and copy down to the end of month Cell.
    =IF(WEEKDAY(A8,2)=1,"Mon",IF(WEEKDAY(A8,2)=2,"Tue",IF(WEEKDAY(A8,2)=3,"Wed",IF(WEEKDAY(A8,2)=4,"Thu",IF(WEEKDAY(A8,2)=5,"Fri","")))))
    See the "Mar 10" sheet in the attachment.

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Just change it to

    [vba]

    Public Sub DateDrivenData()
    Dim sh As Worksheet
    Dim mYear As Long
    Dim mMonth As Long
    Dim mDate As Date
    Dim i As Long, j As Long

    'get year and month, I will pre-load to test
    mYear = 2008
    mMonth = 3

    For i = mMonth To mMonth + 11

    mDate = DateSerial(mYear, i, 1)
    Worksheets.Add After:=Worksheets(Worksheets.Count)
    ActiveSheet.Name = Format(mDate, "yyyy-mmm")
    For j = 0 To 30

    Range("A5").Offset(j, 0).Value = mDate + j
    If Month(mDate + j) <> Month(mDate + j + 1) Then Exit For
    Next j
    Next i
    End Sub
    [/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

  12. #12
    VBAX Tutor CCkfm2000's Avatar
    Joined
    May 2005
    Posts
    209
    Location
    thats solved it.

    Thanks to all....

Posting Permissions

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