How can I 'date format' numerous Excel VBA userform text boxes in a single loop ?

Hi Experts,

I'm creating a user form in Excel VBA and part of the form consists of 12 text boxes which I want to format as date fields dd/mm/yyyy. See attached screenshot.

My code for the first text box is...

' Format the Enquiry date field
   With Me.teEnquiry
      If Not IsDate(.Value) Then
         MsgBox "Invalid date"
      Else
         .Value = Format(.Value, "dd/mm/yyyy")
      End If
   End With

Is there a quicker method of doing this rather than formatting each of the 12 text boxes individually, given that they all have different names ?

Thanks
Toco
TocogroupAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

FamousMortimerCommented:
Hi,

This code will loop through each control on userform1
Sub LoopThruControls()
    Dim ctrl As Control
    For Each ctrl In UserForm1.Controls
        MsgBox ctrl.Name
    Next ctrl
End Sub

Open in new window

If, for example, all of your textboxes names start with 'te' you can use this

Sub ValidateDates()
    Dim ctrl As Control
    For Each ctrl In UserForm1.Controls
        If UCase(Left(ctrl.Name, 2)) = "TE" Then
            With ctrl
                If Not IsDate(.Value) Then
                    MsgBox "Invalid date"
                Else
                    .Value = Format(.Value, "dd/mm/yyyy")
                End If
            End With
        End If
    Next ctrl
End Sub

Open in new window

0
TocogroupAuthor Commented:
I was nearly there with your catch-all code.

Unfortunately I have text boxes which aren't date fields. However, the ones in my example are all enclosed in a frame object called frDelivery. Is there any way I can change your code slightly to just check the text boxes in the frame rather than the whole user form ?
0
FamousMortimerCommented:
Yes, you can change...

For Each ctrl In UserForm1.Controls

to

For Each ctrl In UserForm1.frDelivery.Controls

A frame is a control container as well as a form.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TocogroupAuthor Commented:
Excellent. I learned a lot from that and it works perfectly.
Many thanks
Toco
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.