SOLVED

Moving Rows with VBA based on time

Copper Contributor

Hello,

 

Please I need help in moving specific row cells in excel with times after 2:00 PM using a Macros  Code.

 

I have a two rows, one has a date and the other has a time (this has both date and time), and I want to automatically move all times that are after 2:00 PM to the right along with its corresponding dates. 

 

I have attached a file with the date and times.

 

I will appreciate it if I can get a Code to do that or a faster way of doing that without going through each time one after the other and moving them manually.

 

Thank you in anticipation of your assistance.

2 Replies
best response confirmed by allyreckerman (Microsoft)
Solution

@Jtherson 

Here is a macro:

Sub MoveCells()
    Dim r As Long
    Dim m As Long
    m = Range("A" & Rows.Count).End(xlUp).Row
    Application.ScreenUpdating = False
    For r = 4 To m
        If Hour(Range("A" & r).Value) >= 12 Then
            Range("C" & r).Resize(, 2).Value = Range("A" & r).Resize(, 2).Value
            Range("A" & r).Resize(, 2).ClearContents
        End If
    Next r
    Range("C4:C" & m).NumberFormat = "dd/mm/yy"
    Range("D4:D" & m).NumberFormat = "hh:mm:ss"
    Application.ScreenUpdating = True
End Sub
Works great! Thank you.
1 best response

Accepted Solutions
best response confirmed by allyreckerman (Microsoft)
Solution

@Jtherson 

Here is a macro:

Sub MoveCells()
    Dim r As Long
    Dim m As Long
    m = Range("A" & Rows.Count).End(xlUp).Row
    Application.ScreenUpdating = False
    For r = 4 To m
        If Hour(Range("A" & r).Value) >= 12 Then
            Range("C" & r).Resize(, 2).Value = Range("A" & r).Resize(, 2).Value
            Range("A" & r).Resize(, 2).ClearContents
        End If
    Next r
    Range("C4:C" & m).NumberFormat = "dd/mm/yy"
    Range("D4:D" & m).NumberFormat = "hh:mm:ss"
    Application.ScreenUpdating = True
End Sub

View solution in original post