简体   繁体   中英

How do I write VBA code such that my formula returns the value in the selected cell?

I am currently working on setting up a spreadsheet at work in order to track project status. I can get so far as to have my function return "N/A" when the value in the selected cell equals zero, however I want the code to return the value found in the corresponding cell. (ie: AWARD(U7) = value of cell U7, AWARD(U8) = value of cell U8 instead of what I have written, which returns the value of cell U7 every time.

My code is as follows:

Public Function AWARD(awarddate As Double)

    If awarddate = 0 Then
        result = "N/A"
    Else
        result = ActiveSheet.Range("U7").Text
    End If

    AWARD = result

End Function

Something like:

Public Function AWARD(awarddate As Range)
    Dim result
    If awarddate.Value = 0 Then
        result = "N/A"
    Else
        result = awarddate.Value
    End If

    AWARD = result

End Function

Or

Public Function AWARD(awarddate As Range
    AWARD = IIF(awarddate.Value = 0, "N/A", awarddate.Value)
End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM