Sunny
Sunny

Reputation: 105

How to Autofill dynamic column in VBA?

I have column K with a bunch of dates and I want to get the number of days between K's date and today's date inserted in column O.

My code for the first row is:

 Range("O2").Value = Date - Range("K2").Value

How can I repeat this code for the rest of the column? Also, keep in mind that the length of populated cells in column K is dynamic and always changing.

Thanks to all that can help in advance!

Upvotes: 0

Views: 143

Answers (2)

bobopalmtree
bobopalmtree

Reputation: 26

Range("O2").AutoFill Range("O2:O" & Cells(Rows.Count, "K").End(xlUp).Row)

That is if you have the formula already in cell O2.

Upvotes: 0

user2140173
user2140173

Reputation:

before:

enter image description here

Sub Main()
    Dim i As Long
    For i = 1 To Range("K" & Rows.Count).End(xlUp).Row
        Range("O" & i) = DateDiff("d", Now, Range("K" & i))
    Next i
End Sub

after:

enter image description here

Upvotes: 1

Related Questions