Add automatic rows with date

ahmed elashkar

New Member
Joined
Dec 22, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello

I have a table with the following data

Screenshot 2022-12-22 093634.png


I want when I open the file Excel to check the date in the last row, if it less than today's date then adds rows automatically with dates until it reaches today's date and copy the function from the other cells to the cells in the new rows.

Would you please help me as i have no experience with VBA.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Why not manually ?
Just to select the last row and drag down until today.
 
Upvote 0
Why not manually ?
Just to select the last row and drag down until today.
The sheet will run in the background catching the data from our machine's server, I don't want to rely on human to update it daily
 
Upvote 0
Try.

You should put the code in the ThisWorKbook of VBA.
VBA Code:
Private Sub Workbook_Open()
    Call SbOpenCopy
End Sub

Sub SbOpenCopy()
    Dim S1 As Worksheet
    Set S1 = Worksheets("sheet1") 'Set the sheet
    
    Dim R1 As Range
    Set R1 = Range("A1")  'Set the Date column
    
    Dim R2 As Range
    Set R2 = Range("B1:D1") 'Set the columns to copy
    
    Dim R3 As Range
    Set R3 = R1.End(xlDown).End(xlDown).End(xlUp)
    
    Set R2 = R2.Offset(R3.Row - 1, 0)
    
    Dim myDay, myToday
    myDay = R3.Value
    myToday = Date
    
    Do Until myDay >= myToday
        i = i + 1
        R3.Offset(i, 0) = myDay + 1
        R2.Offset(i - 1, 0).Copy
        R2.Offset(i, 0).PasteSpecial
        myDay = myDay + 1
    Loop
End Sub
 
Upvote 0
VBA Code:
Private Sub Workbook_Open()

Dim Ldate As Range
Set Ldate = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp)
Dim i As Double

If Ldate.Value <> Date Then

Do Until Ldate.Value = Date

Sheets("Sheet1").Range("A" & Ldate.Row + 1).Value = Ldate + 1
Set Ldate = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp)

Loop

End If
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,844
Messages
6,127,245
Members
449,372
Latest member
charlottedv

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top