Highlight Current Date in Date Picker

Dorian Grey 1 Reputation point
2022-09-25T09:44:36.43+00:00

Greetings, I created a date picker from scratch using User Form with numerous Macros. Fortunately, it all works great. I only need one more component to it. I would like the current date illuminated.

Of all the macros I think I know the one I want to add it to, or I think I would need a whole new macro like if have for the button that highlights the button currently selected. The two macros I'm basing my theory around are below.

The back color I'm hoping to have the code "btn.BackColor = VBA.RGB(241, 239, 145)

Thank you so much!

Macro which command buttons (dates) are selected.

Sub reset_color()   
  
Dim i As Integer   
  
Dim btn As MSForms.CommandButton   
  
   
  
For i = 1 To 42   
  
Set btn = Me.Controls("CommandButton" & i)   
  
If btn.Caption = "" Then   
  
btn.Enabled = False   
  
btn.BackColor = VBA.RGB(115, 197, 219)   
  
Else   
  
btn.Enabled = True   
  
btn.BackColor = VBA.RGB(63, 243, 243)   
  
   
  
End If   
  
Next i   
  
   
  
End Sub  

Alternately a whole new macro created very similar to this one.

Sub highlight_Date(dt As Date)   
  
Dim i As Integer   
  
Dim btn As MSForms.CommandButton   
  
   
  
For i = 1 To 42   
  
Set btn = Me.Controls("CommandButton" & i)   
  
If VBA.CStr(VBA.Day(dt)) = btn.Caption Then   
  
   
  
btn.BackColor = vbWhite   
  
   
  
End If   
  
Next i   
  
End Sub  
Developer technologies | Visual Basic for Applications
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2022-09-25T18:42:32.82+00:00

    To avoid duplicates, maybe you should create a helper function (similar to your highlight_Date), which has a colour parameter:

    Sub highlight_Date( ByVal dt As Date, ByVal colour As OLE_COLOR)  
        . . .  
        btn.BackColor = colour  
        . . .  
    End Sub  
    

    You can use it to highlight the current and selected dates using different colours.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.