简体   繁体   中英

Copy the value in a range of cells to another cell if only one cell in the range has a value

I have a range of cells M4:M18. In that range of cells is a formula referencing another table, there will only be one date in this range of cells. I'm trying to get that one date to populate into cell H12. This is the formula that I have in H12:

=IF(COUNTA(M4:M18)=1,INDEX(M4:M18,MATCH("*",M4:M18,0)),"")

I've also tried:

=IF(COUNTIF(M4:M18,"?")=1,INDEX(M4:M18,MATCH("?",M4:M18,0)),"")

This formula returns a black cell in H12. I read a previous post regarding this same issue, but I can't seem to get it to work. The first cell in this range could be empty.

Any help would be greatly appreciated.

Thanks

M4:M18 ='Step 2 Triage_Tbl';$Q3 and are formatted as m/d/yy;;""

and

OneDate UDF

In VBE (ALT+F11) insert a module into your worksheet and copy the code into it.

In Excel in cell H12 type =OneDate(M4:M18) . You might need to format H12 as Date if not already formatted.

Option Explicit

Function OneDate(CheckRange As Range) As Date
    Dim rng As Range
    For Each rng In CheckRange
        If IsDate(rng.Value) Then OneDate = rng.Value: Exit For
    Next
End Function

You might need to adjust the Macro settings for this to work:

File > Options > Trust Center > Trust Center Settings > Macro Settings

在此处输入图像描述

To return only one (1) date in a range M4:M18 , try..........

In N2 , enter formula:

=IF(COUNT(M4:M18)=1,INDEX(M4:M18,MATCH(1,INDEX(0+ISNUMBER(M4:M18),0),0)),"")

在此处输入图像描述

Say M4:M18 is completely filled with formulas, but only one of the formulas gives data, the others, null. This will locate the data:

=MATCH(TRUE,M4:M18<>"",0)

and this will retrieve the data:

=INDEX(M4:M18,MATCH(TRUE,M4:M18<>"",0))

在此处输入图像描述

This works for text or numbers or dates.

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