Excel loop

Hi,

Is there a way to enhance the code EE helped me by looking at specific columns.
https://www.experts-exchange.com/questions/29120476/formatting-data.html
Sub FormatDate()
    Dim cel As Range
    
    For Each cel In Range("A2:C" & ActiveSheet.UsedRange.Rows.Count)
        If InStr(1, cel.Text, ".") = 0 And InStr(1, cel.Text, ",") = 0 Then
            If Len(cel.Value2) > 5 Then
                If Not IsDate(cel.Value2) Then
                    cel = Left$(cel.Value2, 4) & "/" & Mid$(cel.Value2, 5, 2) & "/" & Right$(cel.Value2, 2)
                End If
            End If
            cel.NumberFormat = "mm/dd/yyyy"
        End If
    Next
    
End Sub

Open in new window




I just found out there are just way too many columns I need to check.
E, H, K, U, V, AC, AP, AQ, AR, BF, BM...................

Is there a way to loop through those columns?
thanks
mcrmgAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Martin LissOlder than dirtCommented:
Dim rng As Range
Dim lr As Long

lr = ActiveSheet.UsedRange.Rows.Count
Set rng = Union(Range("A2:A" & lr), Range("H2:H" & lr), Range("K2:K" & lr), Range("U2:U" & lr)) 'ETC
For Each cel In rng
...
Next

Open in new window

0
Martin LissOlder than dirtCommented:
I corrected line 5
0
Martin LissOlder than dirtCommented:
Or...
Dim lr As Long
Dim varCols As Variant
Dim intCol As Integer

varCols = Array("A", "H", "K", "U") ' ETC

lr = ActiveSheet.UsedRange.Rows.Count

For intCol = 0 To UBound(varCols) - 1
    For Each cel In Range(varCols(intCol) & "2:" & varCols(intCol) & lr)
    '...
    Next
Next

Open in new window

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
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

mcrmgAuthor Commented:
Have to fix my boss's pc now....I will close the question now. and post back. thank you
0
mcrmgAuthor Commented:
thanks
0
Martin LissOlder than dirtCommented:
I could have waited until you tested so let me know if there are problems, but in any case you’re welcome and I’m glad I was able to help.

If you expand the “Full Biography” section of my profile you’ll find links to some articles I’ve written that may interest you.

Marty - Microsoft MVP 2009 to 2017
              Experts Exchange Most Valuable Expert (MVE) 2015, 2017
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2017
              Experts Exchange Top Expert VBA (current)
0
mcrmgAuthor Commented:
Okay, I am back. I think the only thing I see so far is to remove -1 on line 9. thanks
0
Martin LissOlder than dirtCommented:
Yes, you're correct.
0
mcrmgAuthor Commented:
Thank you very much
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
VBA

From novice to tech pro — start learning today.