Question : Access other cell data on current row in an Excel Function

I need to build a User Defined Function that can tell what row it is on and access the contents of other specified cells on that same row.  I don't know how to reference the current row or the contents of other cells on this row in a formula.

My goal is to have the formula pick up date/time values from specified columns on the current row and concatenate them with some formatted literal text.  I know I can do this with the CONCATENATE function but I need to include some conditional logic in the concatenation. Please see the attached screenshot.  So I believe I must write my own function.

I tried the attached function code but each row gets a 1/11/1998 (from A3) instead of the actual contents of the A column, current row (see screenshot). I assume this is because you can't use the Range().Select in a function, but I'm not sure.

My 1st question is how can a function access cell values from columns I specify on the current row?  NOTE: The columns will be hard coded in the function.

My 2nd question is how can I preserve the Date/Time formatting in my function output string.  The Concatenate function returns numeric values for the date/times.  I need the actual date time strings.  Again, please see the screenshot.

I've also attached the sample XLS file from the screenshot for your convenience.

Thanks for your help!
- Robert
Code Snippet:
1:
2:
3:
4:
Function BuildMemo() As String
    Range("A" & (ActiveCell.Row)).Select
    BuildMemo = Range("A" & (ActiveCell.Row)).Text
End Function

Answer : Access other cell data on current row in an Excel Function

Hello Robert,

You can use the Caller method to get the cell where the function is called from.

Regards,

Patrick
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
Function BuildMemo() As String
 
    Dim rng As Range
    Dim r As Long
    Dim ws As Worksheet
 
    Set rng = Application.Caller
    Set ws = rng.Parent
    r = rng.Row
 
    With ws
        BuildMemo = Join(Array(.Cells(r, "A").Text, .Cells(r, "B").Text, .Cells(r, "B").Text), " ")
    End With
 
End Function
Random Solutions  
 
programming4us programming4us