Excel Nested Loop for datevalue

I have the following code from a previous question which works great. Formats a column and fills the date. How would I get this to do the same to an addition column as well, column T for example. Nested Loop?

Sub ConvertYYYYMMDDToDate()
    Dim i As Long, c As Range
   
    Columns("C").NumberFormat = "dd/mm/yyyy;@"

    For i = 1 To Range("D1048576").End(xlUp).Row
        If IsNumeric(Range("D" & i).Value) And Len(Range("D" & i).Value) = 8 Then
            Set c = Range("D" & i)
            Range("C" & i).Value = DateValue(Left(c.Value, 4) & "/" & Mid(c.Value, 5, 2) & "/" & Right(c.Value, 2))
        End If
    Next i
End Sub


This what I have so far. No sure how to nest the 2 loops.

Dim i As Long, j As Long, c As Range, T As Range
   
    Columns("D").NumberFormat = "dd/mm/yyyy;@"
    Columns("T").NumberFormat = "dd/mm/yyyy;@"
ExpressMan1Asked:
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 again :)

You mean that you want column T to also have the same date as column C?

if so then you can try this

Sub ConvertYYYYMMDDToDate()
    Dim i As Long, c As Range
    
    Columns("C").NumberFormat = "dd/mm/yyyy;@"
    Columns("T").NumberFormat = "dd/mm/yyyy;@"

    For i = 1 To Range("D1048576").End(xlUp).Row
        If IsNumeric(Range("D" & i).Value) And Len(Range("D" & i).Value) = 8 Then
            Set c = Range("D" & i)
            Range("C" & i).Value = DateValue(Left(c.Value, 4) & "/" & Mid(c.Value, 5, 2) & "/" & Right(c.Value, 2))
            Range("T" & i).Value = DateValue(Left(c.Value, 4) & "/" & Mid(c.Value, 5, 2) & "/" & Right(c.Value, 2))
        End If
    Next i
End Sub

Open in new window

0
ExpressMan1Author Commented:
Actually there is another column "U" formatted as 20130830 and I need to show the adjacent column "T" date formatted as "dd/mm/yyyy;@"

So I need to have column "c" updated from the value of column "d", and column "t" updated from the value of column "u".
0
FamousMortimerCommented:
ahhh...

Sub ConvertYYYYMMDDToDate()
    Dim i As Long, c As Range
    
    Columns("C").NumberFormat = "dd/mm/yyyy;@"
    Columns("T").NumberFormat = "dd/mm/yyyy;@"

    For i = 1 To Range("D1048576").End(xlUp).Row
        If IsNumeric(Range("D" & i).Value) And Len(Range("D" & i).Value) = 8 Then
            Set c = Range("D" & i)
            Range("C" & i).Value = DateValue(Left(c.Value, 4) & "/" & Mid(c.Value, 5, 2) & "/" & Right(c.Value, 2))
            Set c = Range("U" & i)
            Range("T" & i).Value = DateValue(Left(c.Value, 4) & "/" & Mid(c.Value, 5, 2) & "/" & Right(c.Value, 2))
        End If
    Next i
End Sub

Open in new window

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

ExpressMan1Author Commented:
Perfect again. I was working on it and was close. I'm learning.

Thanks again!
0
FamousMortimerCommented:
No problem.  Learning is the fun part!

Let me know if I can help you again or if you have any more questions.
0
ExpressMan1Author Commented:
Small problem. Getting error "Auto fill Method of Range Class Failed".  Changed some column letters.

Dim i As Long, d As Range
   
    Columns("D").NumberFormat = "dd/mm/yyyy;@"
    Columns("T").NumberFormat = "dd/mm/yyyy;@"
    Columns("DX").NumberFormat = "dd/mm/yyyy;@"
   
    For i = 1 To Range("E1048576").End(xlUp).Row
        If IsNumeric(Range("E" & i).Value) And Len(Range("E" & i).Value) = 8 Then
            Set d = Range("E" & i)
            Range("D" & i).Value = DateValue(Left(d.Value, 4) & "/" & Mid(d.Value, 5, 2) & "/" & Right(d.Value, 2))
            Set d = Range("U" & i)
            Range("T" & i).Value = DateValue(Left(t.Value, 4) & "/" & Mid(t.Value, 5, 2) & "/" & Right(t.Value, 2))
            Set d = Range("DY" & i)
            Range("DX" & i).Value = DateValue(Left(dx.Value, 4) & "/" & Mid(dx.Value, 5, 2) & "/" & Right(dx.Value, 2))
        End If
    Next i
0
ExpressMan1Author Commented:
Let me know if I should I post a new question?
0
FamousMortimerCommented:
try this and let me know

Sub test()
    Dim i As Long, d As Range
    
    Columns("D").NumberFormat = "dd/mm/yyyy;@"
    Columns("T").NumberFormat = "dd/mm/yyyy;@"
    Columns("DX").NumberFormat = "dd/mm/yyyy;@"
        
    For i = 1 To Range("E1048576").End(xlUp).Row
        If IsNumeric(Range("E" & i).Value) And Len(Range("E" & i).Value) = 8 Then
            Set d = Range("E" & i)
            Range("D" & i).Value = DateValue(Left(d.Value, 4) & "/" & Mid(d.Value, 5, 2) & "/" & Right(d.Value, 2))
        End If
        
        If IsNumeric(Range("U" & i).Value) And Len(Range("U" & i).Value) = 8 Then
            Set d = Range("U" & i)
            Range("T" & i).Value = DateValue(Left(t.Value, 4) & "/" & Mid(t.Value, 5, 2) & "/" & Right(t.Value, 2))
        End If
        
        If IsNumeric(Range("DY" & i).Value) And Len(Range("DY" & i).Value) = 8 Then
            Set d = Range("DY" & i)
            Range("DX" & i).Value = DateValue(Left(dx.Value, 4) & "/" & Mid(dx.Value, 5, 2) & "/" & Right(dx.Value, 2))
        End If
    Next i
End Sub

Open in new window

0
ExpressMan1Author Commented:
Getting "Object Required" error on,

Range("T" & i).Value = DateValue(Left(t.Value, 4) & "/" & Mid(t.Value, 5, 2) & "/" & Right(t.Value, 2))
0
FamousMortimerCommented:
oops, looks like the range variable got changed to the column name.


Sub test()
    Dim i As Long, d As Range
   
    Columns("D").NumberFormat = "dd/mm/yyyy;@"
    Columns("T").NumberFormat = "dd/mm/yyyy;@"
    Columns("DX").NumberFormat = "dd/mm/yyyy;@"
       
    For i = 1 To Range("E1048576").End(xlUp).Row
        If IsNumeric(Range("E" & i).Value) And Len(Range("E" & i).Value) = 8 Then
            Set d = Range("E" & i)
            Range("D" & i).Value = DateValue(Left(d.Value, 4) & "/" & Mid(d.Value, 5, 2) & "/" & Right(d.Value, 2))
        End If
       
        If IsNumeric(Range("U" & i).Value) And Len(Range("U" & i).Value) = 8 Then
            Set d = Range("U" & i)
            Range("T" & i).Value = DateValue(Left(d.Value, 4) & "/" & Mid(d.Value, 5, 2) & "/" & Right(d.Value, 2))
        End If
       
        If IsNumeric(Range("DY" & i).Value) And Len(Range("DY" & i).Value) = 8 Then
            Set d = Range("DY" & i)
            Range("DX" & i).Value = DateValue(Left(d.Value, 4) & "/" & Mid(d.Value, 5, 2) & "/" & Right(d.Value, 2))
        End If
    Next i
End Sub
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
ExpressMan1Author Commented:
That was it. Many thanks.
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.