Skip to main content
  1. About
  2. For Teams
Asked
Viewed 502 times
0

my program does what I expect to do. I'm not happy with the last loop.

Option Explicit
Public Sub calendar()
Dim i, j

Dim mDay As Date
For i = 1 To 12
    Cells(1, i + 1).Value = MonthName(i)
    For j = 2 To 32
        If IsDate(j - 1 & "/" & i & "/" & Year(Date)) Then
           mDay = CDate(j - 1 & "/" & i & "/" & Year(Date))
            Cells(j, i + 1).Value = mDay
            If Weekday(mDay) = 1 Then
                Cells(j, i + 1).Interior.Color = vbRed
                ElseIf Weekday(mDay) = 7 Then
                Cells(j, i + 1).Interior.Color = vbYellow
                Else
                Cells(j, i + 1).ClearFormats
            End If
                      Cells(j, i + 1).Value = Format(mDay, "DDDD")
                    
        End If
    Next j
Next I
For i = 1 To 31
    Cells(i + 1, 1).Value = i
Next i
End Sub


I already have a loop that counts to 31 but if I put it in there it would be executed 12 times. is there a smarter way to do it?

5
  • 1
    I don't understand your question. What do you mean by "smarter?"
    Robert Harvey
    –  Robert Harvey
    2021-08-18 14:02:45 +00:00
    Commented Aug 18, 2021 at 14:02
  • Let me rephrase it. How would you solve the problem? I automatically assume it'd be smarter . :) In my guts, it feels wrong to have an additional loop´.
    webwohnraum
    –  webwohnraum
    2021-08-18 14:13:51 +00:00
    Commented Aug 18, 2021 at 14:13
  • 1
    Your approach looks fine to me.
    Robert Harvey
    –  Robert Harvey
    2021-08-18 14:14:28 +00:00
    Commented Aug 18, 2021 at 14:14
  • Fine that's an answer I can live. I can't improve the code. I'm here to learn and improve. (Appart from dim i,j which is terrible)
    webwohnraum
    –  webwohnraum
    2021-08-18 14:17:43 +00:00
    Commented Aug 18, 2021 at 14:17
  • 1
    dim i,j is terrible indeed, I would replace it by dim index_month, index_day as integer :-)
    Dominique
    –  Dominique
    2021-08-19 06:35:08 +00:00
    Commented Aug 19, 2021 at 6:35

2 Answers 2

1

I will allocate the value into an array then write into the worksheet 1 time, should be faster that way. (Read/Write to/from cells are expensive operation)

Then use conditional formatting for Sunday and Saturday:

Public Sub calendar()
    Dim i As Long, j As Long

    Dim outputArr() As Variant
    ReDim outputArr(1 To 32, 1 To 13) As Variant

    For i = 1 To 12
        outputArr(1, i + 1) = MonthName(i)
        For j = 2 To 32
            If IsDate(j - 1 & "/" & i & "/" & Year(Date)) Then
                outputArr(j, i + 1) = Format(DateSerial(Year(Date), i, j - 1), "DDDD")
            End If
        Next j
    Next i
    
    For i = 1 To 31
        outputArr(i + 1, 1) = i
    Next i
    
    Dim calendarRng As Range
    Set calendarRng = Range("A1").Resize(32, 13)
    
    Dim formatSunday As FormatCondition
    Set formatSunday = calendarRng.FormatConditions.Add(xlCellValue, xlEqual, Formula1:="=" & Chr(34) & Format(Date - Weekday(Date, vbSunday) + 1, "DDDD") & Chr(34))
    formatSunday.Interior.Color = vbRed
    
    Dim formatSaturday As FormatCondition
    Set formatSaturday = calendarRng.FormatConditions.Add(xlCellValue, xlEqual, Formula1:="=" & Chr(34) & Format(Date - Weekday(Date, vbSaturday) + 1, "DDDD") & Chr(34))
    formatSaturday.Interior.Color = vbYellow
    
    calendarRng.Value = outputArr
End Sub
Sign up to request clarification or add additional context in comments.

7 Comments

thanx works fine Minor issue you swapped the colors of sunday/saturday :) and minor Bug: It only works in US in other languages you have to change the code (""Sunday"") to (""whatever"") but it is easy to change the conditional formating
@webwohnraum oops sorry about that, I'll edit it for completeness.
@webwohnraum I thought Sunday is red and Saturday is Yellow? Anyway it's easy for you to make the change. I have changed the Formula1 argument to take in the Format return of Sun/Sat so it should work for other language now too.
Nah it was my error. I typed formatSunday = ... =""Saturady""") :facepalm: I'm sorry about that
Question though I don't get the Date - Weekday(Date, vbSaturday)+1 How does that work? isn't date the date of today? When I debug.Print Date - Weekday(Date, vbSaturday) + 1I get 14.08.2021
|
0

try to move the statement inside the inner loop and check for i = 1 to only let it execute once.

Option Explicit
Public Sub calendar()
Dim i, j

Dim mDay As Date
For i = 1 To 12
    Cells(1, i + 1).Value = MonthName(i)
    For j = 2 To 32
        If IsDate(j - 1 & "/" & i & "/" & Year(Date)) Then
           mDay = CDate(j - 1 & "/" & i & "/" & Year(Date))
            Cells(j, i + 1).Value = mDay
            If Weekday(mDay) = 1 Then
                Cells(j, i + 1).Interior.Color = vbRed
                ElseIf Weekday(mDay) = 7 Then
                Cells(j, i + 1).Interior.Color = vbYellow
                Else
                Cells(j, i + 1).ClearFormats
            End If
                      Cells(j, i + 1).Value = Format(mDay, "DDDD")
                    
        End If
        if i = 1 then cells(j ,1).value = j - 1
    Next j
Next I
'For i = 1 To 31
'    Cells(i + 1, 1).Value = i
'Next i
End Sub

7 Comments

No explanation provided.
added explanation
How's the time used. ( it doesn't matter in this case-just out of curiosity) If it would be 1 Million lines would the additional if check take longer instead of the Additional loop?
@webwohnraum: The best way to find that out is to measure the code's performance with a timer or profiler.
@webwohnraum I think 2 individual loops would be faster at 12 columns and 1 million rows, as you will do the if 12 million times, rather than 1 million in a separate loop. If you're after speed, nested loops are usually things you want to avoid, instead of 12*31 use rather 1 loop to loop through 365 days)
|

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.