Help with incremental loop

IOO

New Member
Joined
Apr 11, 2020
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hello, I'm hoping someone could help me with my code. I'm trying to autofill/complete blank cells after last used cell in column A until target year value is reached.
Years are entered in col A from another script, what I need to accomplish is if the last year value in col A is less than current year (as of this post current year is 2021), complete the cells down until year value = 2021.

Here is my current code, but it only fills the next empty cell after the last year entered. Fir instance in the attached test sheet, it will only populate 2017 after 2016. What I would like to see is populate the cells after 2016 until the value is = 2021. Please note, the last year entered in col A is dynamic. I would really appreciate the help. Thanks.

VBA Code:
Sub test()
Dim LR As Long
Dim counter As Integer, yearGap As Integer
Dim lastCellvalue As Range


lastRow = Worksheets("test").Range("A" & Rows.Count).End(xlUp).Row
Set lastCellvalue = Worksheets("test").Range("A" & lastRow)
yearGap = Year(Now()) - lastCellvalue
    counter = 0
        Do While counter < yearGap
            If lastCellvalue.Value < Year(Now()) Then
                lastCellvalue.Offset(1).Value = lastCellvalue.Value + 1
                counter = counter + 1
            End If
        Loop
End Sub

When I run it, it only populates the next row and exits. Please help. Attached is
 

Attachments

  • 2021-05-18_20-20-09.jpg
    2021-05-18_20-20-09.jpg
    25.3 KB · Views: 8

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi, an Excel basics VBA demonstration as a beginner starter :​
VBA Code:
Sub Demo1()
    Dim Rg As Range, Y%
    Set Rg = [Test!A1].End(xlDown)
    Y = Year(Date)
    If Rg.Value2 < Y Then Rg.AutoFill Rg.Resize(Y - Rg.Value2 + 1), 2
    Set Rg = Nothing
End Sub
As a reminder nothing is 'dynamic' under Excel …​
 
Last edited:
  • Like
Reactions: IOO
Upvote 0
Try
VBA Code:
Sub test()
Dim LR As Long
Dim counter As Integer, yearGap As Integer
Dim lastCellvalue As Range


LR = Worksheets("test").Range("A" & Rows.Count).End(xlUp).Row
Set lastCellvalue = Worksheets("test").Range("A" & LR)
yearGap = Year(Now()) - lastCellvalue
counter = 0
Do While counter < yearGap
    counter = counter + 1
    Worksheets("test").Range("A" & LR + counter) = lastCellvalue + counter
Loop

End Sub
 
Upvote 0
Solution
Try
VBA Code:
Sub test()
Dim LR As Long
Dim counter As Integer, yearGap As Integer
Dim lastCellvalue As Range


LR = Worksheets("test").Range("A" & Rows.Count).End(xlUp).Row
Set lastCellvalue = Worksheets("test").Range("A" & LR)
yearGap = Year(Now()) - lastCellvalue
counter = 0
Do While counter < yearGap
    counter = counter + 1
    Worksheets("test").Range("A" & LR + counter) = lastCellvalue + counter
Loop

End Sub
Thank you Zot! Just the tweak I needed, works perfectly. Thanks for the help!
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,535
Members
449,037
Latest member
tmmotairi

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