C. Rad
C. Rad

Reputation: 33

vba calculate days between dates in two cells, only return a value if both cells have a date

I have the below code to calculate days between dates in two cells, but it returns an error value if column X has no date. I want the code to only return a value if there is a date in column X, but I don't know how to do that. I am new to VBA. Thanks in advance for any help you can give.

Sub CalcDays()
Dim LastRow As Long
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet
Set ws = wb.Sheets("Sheet2")

LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

With Range("AA2:AA" & LastRow)
    [AA:AA] = [X:X-Z:Z]
End With

End Sub

Upvotes: 3

Views: 92

Answers (1)

JNevill
JNevill

Reputation: 50034

You could do something like:

[AA:AA] = [if(X:X="", "", if(Z:Z="", "", X:X-Z:Z))]

Upvotes: 2

Related Questions