Calculate date by each row

carlrubber

New Member
Joined
Oct 15, 2016
Messages
48
Code:
For j = 4 To lastrow1
If Sheet3.Cells(j + 1, "H") <> "" Then
If Sheet3.Cells(j + 1, "I") = "" Then
Range(j + 1, "I").Formula = "=DATE(YEAR([I]&j+1),MONTH([I]&j+1)+18,DAY([I]&j+1))"
End If
End If

would anyone help how to make the code works?
thz
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

baitmaster

Well-known Member
Joined
Mar 12, 2009
Messages
2,042
Code:
For j = 4 To lastrow1
    If Sheet3.Cells(j + 1, "H") <> "" And Sheet3.Cells(j + 1, "I") = "" Then
        Sheet3.Cells(j + 1, "I").FormulaR1C1 = "=DATE(YEAR(RC[-1]),MONTH(RC[-1])+18,DAY(RC[-1]))"
    End If
 
Upvote 0

pjmorris

Well-known Member
Joined
Aug 2, 2012
Messages
2,102
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I ended up with this, which actually does something, but since it creates a circular reference it seems pretty pointless?

Code:
Sub test()
    Dim j As Integer
    Dim lastrow1 As Integer
    
    lastrow1 = 20
    
    For j = 4 To lastrow1
        If Sheet3.Cells(j + 1, "H") <> "" Then
            If Sheet3.Cells(j + 1, "I") = "" Then
                Sheet3.Cells(j + 1, "I").Formula = "=DATE(YEAR(I" & j + 1 & "),MONTH(I" & j + 1 & ")+18,DAY(I" & j + 1 & "))"
            End If
        End If
    Next j
End Sub

Worth noting the indenting as baitmaster has also done - it will make reading your code much easier.

HTH
 
Upvote 0

friel300

Board Regular
Joined
Jan 22, 2008
Messages
69
You havnt given us much to go on, so this is just a guess.

like this?

Code:
Sub Macro_Name()
    Dim lastrow1 As Integer                                         'to declare the lastrow1 Variable


    lastrow1 = Sheet3.Range("H" & Sheet3.Rows.Count).End(xlUp).Row  'to populate the lastrow1 Variable


For j = 4 To lastrow1
        If Sheet3.Cells(j + 1, 8) <> "" Then
            If Sheet3.Cells(j + 1, 9) = "" Then
                Range("I" & j + 1).Formula = "=DATE(YEAR(H" & j + 1 & "),MONTH(H" & j + 1 & ")+18,DAY(H" & j + 1 & "))" 'cells refrenced was also the cell that being filled in changed ref to COL H
            End If
        End If
    Next
End Sub                                                             'Macro needs to be contained in Sub / End sub

Hope this helps
 
Upvote 0

Forum statistics

Threads
1,191,040
Messages
5,984,295
Members
439,882
Latest member
gerdc

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
Top