Hi everyone, need any help from forum guru's here.

Attached is the sample excel file with the code. MySampleDBs.xlsm

I use vba code in my attached sampledb that updates sheet "data" if there are newer data in sheets "new" according to the latest dates, basically it checks dates in sheet "data" and compares it with sheet "new", if there is newer dates in "new", it copies the whole column with the newer date and pastes it next to last used column in "data".

However the current code only adds a single column from the new ones, and loop copies only on that column instead of pasting to the next column after it.

I need the code to copy the latest dated column and loop until all new dated columns have been copied.

Please help out. Thanks.

Sub UpdateColumns()
Dim ws As Worksheet
Dim mDates As Range

Sheets("data").Select

Set ws = Sheets("data")

'[mranges] refer to a named range in sheets "data", it refers to the top row header
With ws
Set mDates = [mranges].End(xlToRight)
End With

Sheets("new").Select

    Dim i As Long, _
        LastCol As Long

    LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
    
    For i = LastCol To 1 Step -1
       If (Cells(1, i).Value) = mDates + 1 Then
            Cells(1, i).EntireColumn.Copy Destination:= _
                Sheets("Data").Range("f1")
        Exit For
        End If
    Next i
    
     For i = LastCol To -1 Step 1
       If (Cells(1, i).Value) = mDates + 2 Then
            Cells(1, i).EntireColumn.Copy Destination:= _
                Sheets("Data").Range("g1")
        Exit For
        End If
    Next i

  For i = LastCol To -1 Step 1
       If (Cells(1, i).Value) = mDates + 3 Then
            Cells(1, i).EntireColumn.Copy Destination:= _
                Sheets("Data").Range("h1")
        Exit For
        End If
    Next i

Sheets("Data").Select
    
End Sub