Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 26

Thread: Code review?

  1. #1
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location

    Code review?

    Hi Everyone,

    I made a macro that will create a landscape-style calendar for a given year (chosen at runtime). I'd love any suggestions/improvements/ideas about it. Written in excel 2000, will be given to different versions. After it's done I'll add it as a KB entry too.
    Any thoughts are appreciated!

    Sub CreateCalendar()
     Dim Mos As Range, CLL As Range, ddt As Date
     Dim wk As Integer, i As Integer, yr As Long, x As Integer
     yr = Application.InputBox(Prompt:="Please enter the 4 digit year", _
      Title:="Enter year", Default:=Year(Now), Type:=1)
     Application.ScreenUpdating = False
     Workbooks.Add
     Application.DisplayAlerts = False
     For i = Application.SheetsInNewWorkbook To 2 Step -1
      Sheets(i).Delete
     Next i
     Application.DisplayAlerts = True
     Range("A1:AE26").Font.Size = 12
     Range("A:G,I:O,Q:W,Y:AE").ColumnWidth = 3.71
     Range("H:H,P:P,X:X").ColumnWidth = 1
     Range("1:8,10:17,19:26").RowHeight = 24.75
     Range("9:9,18:18").RowHeight = 9
     Set Mos = Union([A1], [i1], [q1], [y1], [A10], [i10], [q10], [y10], _
      [a19], [i19], [q19], [y19])
     i = 1
     Mos.NumberFormat = "@"
     Mos.Font.Bold = True
     For Each CLL In Mos.Cells
      Range(CLL, CLL.Offset(0, 6)).HorizontalAlignment = 7
      Range(CLL, CLL.Offset(7, 6)).BorderAround (1)
      Range(CLL.Offset(1, 0), CLL.Offset(7, 6)).HorizontalAlignment = -4108
      Range(CLL.Offset(1, 0), CLL.Offset(1, 6)) = Array("S", "M", "T", "W", _
       "R", "F", "S")
      Range(CLL.Offset(1, 0), CLL.Offset(1, 6)).Borders(9).LineStyle = 1
      CLL = Format(DateValue(i & "/1/" & yr), "Mmmm yyyy")
      wk = 1
      For x = 1 To 31
       If IsDate(i & "/" & x & "/" & yr) Then
        ddt = DateValue(i & "/" & x & "/" & yr)
        If x > 1 And Weekday(ddt) = 1 Then wk = wk + 1
        CLL.Offset(wk + 1, Weekday(ddt) - 1) = Day(ddt)
       End If
      Next x
      i = i + 1
     Next CLL
     With ActiveSheet.PageSetup
      .LeftMargin = Application.InchesToPoints(0.25)
      .RightMargin = Application.InchesToPoints(0.25)
      .TopMargin = Application.InchesToPoints(0.25)
      .BottomMargin = Application.InchesToPoints(0.25)
      .CenterHorizontally = True
      .CenterVertically = True
      .Orientation = xlLandscape
      .PaperSize = xlPaperLetter
      .Zoom = False
      .FitToPagesWide = 1
      .FitToPagesTall = 1
     End With
     ActiveSheet.Name = Format(ddt, "yyyy")
     Application.ScreenUpdating = True
    End Sub
    Matt

  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Thanks Matt! I needed a new calendar!

    Works on 97 and 2003, although I only tested the printing in 2003. I wasn't expecting it to make a new book, but it's probably a good idea.

    I'll see if any other thoughts come up and post back if they do.

    Cheers,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  3. #3
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    I appreciate it! I made it a new book as I'm going to be creating an add-in out of this, and don't want to have to worry about overwriting someones work or checking for an activeworkbook, etc
    I thought about asking for a span of years and creating a tab for each one, but i couldn't think of a time that would really be necessary, and would confuse more people than help, probably. Still looking for new features though, possibly creating a tab for each month of the year, will room to write information/etc? Think that could be useful?

  4. #4
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location


    Could be... don't know really, as it depends on the end user, really.

    You know, J-Walk had something in one of his books on using an array to make a monthly calendar... I'll see if I can locate it tonight.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Matt,
    I must have a different 2003 from Ken, haven't had a chance to look at the code, but here's the result
    MD
    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'

  6. #6
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Wow thats pretty cool
    Strange that it would do that though. I think I have office xp and office 97 on cds around here, I just keep it on 2000 as thats what i have at work. Im sure I can install them all separately, but just havent done it yet.
    How odd that it would do that to the months and days

  7. #7
    Administrator
    VP-Knowledge Base VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    Hi Malcolm,

    I got the same result (using 2003 also) plus an error when setting the paper size. Could it be a difference because of language settings???
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  8. #8
    Administrator
    VP-Knowledge Base VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    For testing I have changed it from
    .PaperSize = xlPaperLetter 
    to
    .PaperSize = 70
    No error, but same result.
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  9. #9
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Quote Originally Posted by mvidas
    Wow thats pretty cool ....How odd that it would do that to the months and days
    I think I may have an idea what happened here... Could be wrong, but look at this part of your code:

    For x = 1 To 31
                   If IsDate(i & "/" & x & "/" & yr) Then
                      ddt = DateValue(i & "/" & x & "/" & yr)
                      If x > 1 And Weekday(ddt) = 1 Then wk = wk + 1
                      CLL.Offset(wk + 1, Weekday(ddt) - 1) = Day(ddt)
                   End If
               Next x

    I use the American date format on my machine MM/DD/YYYY. I'm willing to bet that neither Malcolm nor Carlos do though! And I'm also pretty sure that the ISDate function might get confused in this circumstance, too.

    Not knowing the exact date format they'd use, I just tried flipping your i's and x's around. While it didn't produce Malcolm's output exactly, it was similar.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You could be right Ken, we which is the correct way to write the date over here!
    MD
    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'

  11. #11
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Good call, Ken!
    Hmmm, I thought about using the dateserial function, but I don't trust it (put dateserial(2005,2,30) and it comes back as march 2, 2005)

    OK, try changing the For x loop to:

    For x = 1 To 31
       If TheDate(yr, i, x) <> "Error" Then
        ddt = TheDate(yr, i, x)
        If x > 1 And Weekday(ddt) = 1 Then wk = wk + 1
        CLL.Offset(wk + 1, Weekday(ddt) - 1) = Day(ddt)
       End If
      Next x

    and add at the bottom:

    Public Function TheDate(ByVal vYear As Integer, ByVal vMonth As Integer, _
      ByVal vDay As Integer)
     Dim NumDays As Integer
     Select Case vMonth
      Case 1, 3, 5, 7, 8, 10, 12
       NumDays = 31
      Case 2
       If vYear Mod 4 = 0 Then
        If vYear Mod 100 = 0 And vYear Mod 400 <> 0 Then NumDays = 28 Else NumDays = 29
       Else
        NumDays = 28
       End If
      Case 4, 6, 9, 11
       NumDays = 30
      Case Else
       NumDays = 0
     End Select
     If vDay > NumDays Then TheDate = "Error" Else TheDate = DateSerial(vYear, vMonth, vDay)
    End Function
    Seems like a bad way to do it but I can't think of a better way..

  12. #12
    VBAX Regular
    Joined
    Jun 2004
    Location
    Cordova, Alaska
    Posts
    10
    Location
    Hi Matt,
    Your original code worked great for me too using 2003 & American date format.

    Pretty handy routine to have laying around!

    Dan

  13. #13
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Thanks Dan,
    Your posting just reminded me I left this thread open. I'm not quite done with the routine yet, but Ken gave me a lot to think about in an email he sent me. I'll close this one out, and add the KB entry when I get it finished (I'll post a link to the entry from here in case anyone is curious).
    Matt

  14. #14
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Matt,
    I was just looking at your code. This line needs tweaking

    CLL = Format(DateValue(i & "/1/" & yr), "Mmmm yyyy")
    to

    CLL = Format(DateValue("1/" & i & "/" & yr), "Mmmm yyyy")
    for the UK calender, otherwise every month is January... and I prefer the summer months.
    MD
    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'

  15. #15
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Quote Originally Posted by mdmackillop
    ... and I prefer the summer months.
    So... you want him to make it so every month is July then?
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  16. #16
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Haha Malcolm, I was trying to appease the Southern Hemisphere users, January is summer!

    There is a temporary fix at http://www.vbaexpress.com/forum/show...5&postcount=11 for the non-american users, but I'm hoping to work around that

  17. #17
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Seems good to me. I can take my holidays then!
    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'

  18. #18
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Matt,
    I used that fix, but it wasn't catching the heading to each month.
    Malcolm
    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'

  19. #19
    VBAX Contributor Richie(UK)'s Avatar
    Joined
    May 2004
    Location
    UK
    Posts
    188
    Location
    Hi,

    Just a quickie (said the actress to the bishop) - you can add a workbook with a single sheet rather than adding a standard one and then deleting sheets. Like this :
    Set wbk = Workbooks.Add(xlWBATWorksheet)

  20. #20
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Ahhh, I forgot all about that. You can fix it with:
    '  CLL = Format(DateValue(i & "/1/" & yr), "Mmmm yyyy")
      CLL = Format(DateSerial(yr, i, 1), "Mmmm yyyy")

Posting Permissions

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