Excel合并2个公式

时间:2019-03-26 10:44:10

标签: arrays excel if-statement excel-formula formula

我有两个要合并为一个的公式,但不知道该怎么做。我需要过滤出空白单元格并删除重复项。

数据示例:

enter image description here

第一个删除空格的公式是:

=IFERROR(INDEX($A$1:$A$500,SMALL(IF($A$1:$A$500<>"",ROW($A$1:$A$500)-ROW($A$1)+1),ROW(A1))),"")

第二个公式将删除所有重复项。

=IF(COUNTIF(A$1:A1,A1)=1,A1,"")

谢谢!

1 个答案:

答案 0 :(得分:0)

  

以下是我昨天了解的任务的解决方案:

删除“空白”:=substitute(A1," ","")

您的身份=IF(COUNTIF(A$1:A1,A1)=1,A1,"")

一起: =substitute(IF(COUNTIF(A$1:A1,A1)=1,A1,"")," ",""))

仅空格不同的值将显示为不同!

如果替换后的值相等(例如“ b b”和“ bb”)
那么您需要两行,并且替换后需要重新使用。
那么,如果不做进一步的工作就无法将两个公式结合起来。
(例如,vba解决方案:替代,重新​​排序和输出)。

  

以下是我使用vba解决此问题的方法:

Option Explicit

Sub sample_values()
    Range("A1").Value = "Values"
    Range("A2").Value = "2023"
    Range("A3").Value = "2141"
    Range("A4").Value = "2156"
    Range("A5").Value = "2175"
    Range("A6").Value = "2300"
    Range("A7").Value = "23 00"
    Range("A8").Value = "23 0 0  "
    Range("A9").Value = "2181"
    Range("A10").Value = "2188"
    Range("A11").Value = "2204"
    Range("A12").Value = "2207"
    Range("A13").Value = "2211"
    Range("A14").Value = "22 11"
    Range("A15").Value = "2 2 1 1"
    Range("A16").Value = "221  1"
    Range("A17").Value = "221   1"
    Range("A18").Value = "2236"
    Range("A19").Value = "2239"
    Range("A20").Value = "2250"
End Sub

Sub rb_s_ouv()
'remove_blanks_and_sort_and_output_unique_values()

Dim myInput As Range
Dim sortedInput As Range
Dim outputRng As Range
Dim lastRow As Long
Dim cell As Range
Dim uniqueCt As Integer
Dim noBlanks_Column As Boolean

lastRow = Range("A1").SpecialCells(xlCellTypeLastCell).Row

Set myInput = Range(Cells(2, 1), Cells(lastRow, 1))
'Debug.Print myInput.Address

noBlanks_Column = True     'no blanks in an individual column
'noBlanks_Column = False   'no blanks in the original column

'substitute blanks
If noBlanks_Column Then myInput.Offset(-1, 2).Value = "no_blanks"
For Each cell In myInput
    If noBlanks_Column Then
        cell.Offset(0, 2).Value = Replace(cell.Value, " ", "")
    Else
        cell.Value = Replace(cell.Value, " ", "")
    End If
Next cell

'sort input
If noBlanks_Column Then
    myInput.Offset(0, 2).Sort myInput.Offset(0, 2), xlAscending
    Set sortedInput = myInput.Offset(0, 2)
Else
    myInput.Sort myInput, xlAscending
    Set sortedInput = myInput
End If

'output unique values
sortedInput.Offset(-1, 2).Range("A1").Value = "unique values"
Set outputRng = sortedInput.Offset(0, 2).Range("A1")

uniqueCt = 0
For Each cell In sortedInput
    If cell.Value <> cell.Offset(1, 0).Value Then
        outputRng.Offset(uniqueCt, 0).Value = cell
        uniqueCt = uniqueCt + 1
    End If
Next cell

End Sub

“无空白”列的详细信息:

enter image description here