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

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
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
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
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,214,635
Messages
6,120,660
Members
448,975
Latest member
sweeberry

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