问题
I have got a problem with the unique value in Excel.
I used the advice from this query:
Extract unique value from the range in Excel
used the following module:
Public Function unikue(rng As Range)
Dim arr, c As Collection, r As Range
Dim nCall As Long, nColl As Long
Dim i As Long
Set c = New Collection
nCall = Application.Caller.Count
On Error Resume Next
For Each r In rng
c.Add r.Text, CStr(r.Text)
Next r
On Error GoTo 0
nColl = c.Count
If nCall > nColl Then
ReDim arr(1 To nCall, 1 To 1)
For i = 1 To nCall
arr(i, 1) = ""
Next i
Else
ReDim arr(1 To nColl, 1 To 1)
End If
For i = 1 To nColl
arr(i, 1) = c.Item(i)
Next i
unikue = arr
End Function
And typed the following formula in the cell V15:
{=UNIKUE(U3:U6)}
Unfortunately instead of an address, which I want I am getting the prevailing N/A+N/A+N/A values, which I want to get rid of.
I want to have only the address string (bounded red) appearing in the V15 column.
Apart from the module I tried also created the name manager, where I input this formula:
=OFFSET(Sheet1!$U$3,0,0,COUNTA(Sheet1!$U:$U)-2,1)
and next allocated another formula including this name in the cell V16
=IFERROR(INDEX(List,MATCH(0,COUNTIF($V$23:$V23,List),0)),"")
but unfortunately, I am getting the blank cell instead.
How can I solve this problem? In the cell V15, I want to have the only one string, which is other than N/A+N/A+N/A.
回答1:
Please copy the next function in a module:
Function UniqueStr(rng As Range) As String
Dim El As Variant
For Each El In rng.Value
If InStr(El, "N/A") = 0 Then UniqueStr = El: Exit Function
Next
End Function
But, if you will have more then one "address valid" in the processed range, the function will return only the first one...
Then, write a formula in the cell you need to receive the returned string:
=uniqueStr(U3:U6)
After writing the function name, you can select with the cursor the range you need to be processed.
If you need to use an array function, there are two possibilities:
If you need it to increment each range to be processed row (I mean if the first formula will analize "U3:U6", the next row formula will process "U4:U7". The function will use relative reference.
If you need that all the cells having the array formula to process the same "U3:U6" string, please select it in the formula you wrote and press F4
. In this way the function will use Absolute reference... It will look like this:
=uniqueStr($U$3:$U$6)
来源:https://stackoverflow.com/questions/62342608/unique-range-in-excel-doesnt-return-the-unique-record-but-all-other-records