Excel中的条件连接

问题描述 投票:2回答:6

正如您在图像中看到的那样,每列重新排列有3行和1英文字母的1和0。我需要做的是在相应的列值为0时连接每行的英文字母。我该怎么做?

image link

excel vba excel-formula conditional concatenation
6个回答
1
投票

这是一个VBA解决方案,可以处理任意数量的列(假设与每列相关联的字母是标准列标签):

Function ZeroColumns(R As Range) As String
    Dim n As Long
    Dim count As Long
    Dim cols As Variant
    Dim cell As Range

    n = R.Cells.count
    ReDim cols(1 To n)
    For Each cell In R.Cells
        If cell.Value = 0 Then
            count = count + 1
            cols(count) = Split(cell.Address, "$")(1)
        End If
    Next cell
    ReDim Preserve cols(1 To count)
    ZeroColumns = Join(cols, "")
End Function

如果所述假设不成立,则代码不应太难调整。


0
投票

如果您愿意,可以在一个公式中完成所有操作:

=CONCATENATE(IF($A1=0,'A',''),IF($B1=0,'B',''), ...)

或者将中间琴弦放在一个单独的行中,然后将它们连接起来(以节省手指的磨损)。


0
投票

你要去更多的专栏,或者只是你提到的专栏吗?只要列数相对较小(如图片中所示),就可以连接IF函数以实现结果。

这是我做的:

enter image description here

使用该公式将获得与您拥有的结果相似的结果:

enter image description here

假设您在我的工作表中有值,只需将公式=IF(B3=1,"",B$1)&IF(C3=1,"",C$1)&IF(D3=1,"",D$1)&IF(E3=1,"",E$1)&IF(F3=1,"",F$1)&IF(G3=1,"",G$1)&IF(H3=1,"",H$1)&IF(I3=1,"",I$1)&IF(J3=1,"",J$1)粘贴到B7中,然后拖动到B8和B9以获得其余结果。

当然,如果您要为更多列执行此操作,则最好使用VBA。


0
投票

在这里,将此功能添加到模块。然后,您可以通过excel直接调用它。好一个。

Function conc(ref As Range, Optional Separator As String) As String
Dim Cell As Range
Dim Result As String

For Each Cell In ref
    If Cell.Value = 0 Then
    Result = Result & chr(64 + Cell.Column) & Separator
    End If
Next Cell
If Separator <> "" Then conc = Left(Result, Len(Result) - 1) Else: conc = Result
End Function

0
投票

Conditionally Concatenate Row (UDF)

参数

  • SourceRowRange:包含将写入CCROW的值的范围,例如: A, B, C ...必需。
  • CriteriaRowRange:将检查CriteriaValue的范围。需要。
  • CriteriaValue:将检查CriteriaRowRange中的单元格的值。默认是0。可选的。
  • JoinString:将写入CCROW的值之间的值。默认是""。可选的。

'将以下代码复制到标准模块,即在VBE中转到Insert>Module

代码

Function CCROW(ByVal SourceRowRange As Range, ByVal CriteriaRowRange As Range, _
        Optional ByVal CriteriaValue As Variant = 0, _
        Optional ByVal JoinString As String) As String

    Dim vntS As Variant   ' Source Array
    Dim vntC As Variant   ' Criteria Array
    Dim NoC As Long       ' Number of Columns
    Dim j As Long         ' Arrays Column Counter
    Dim strB As String    ' String Builder
    Dim strC As String    ' Criteria String

    ' Calculate number of columns of the narrower Range.
    NoC = WorksheetFunction.Min(SourceRowRange.Columns.count, _
            CriteriaRowRange.Columns.count)

    ' Copy resized (adjust them to same size) Ranges to Arrays.
    vntS = SourceRowRange.Resize(1, NoC)
    vntC = CriteriaRowRange.Resize(1, NoC)

    ' Loop through columns of either Array.
    For j = 1 To NoC
        ' Write current value of Criteria Array to Criteria String.
        strC = vntC(1, j)
        ' Check if Criteria String is NOT empty.
        If strC <> "" Then
            ' Check if Criteria String is equal to Criteria Value.
            If strC = CriteriaValue Then
                ' Check if String Builder is NOT empty.
                If strB <> "" Then  ' NOT empty.
                    strB = strB & JoinString & vntS(1, j)
                  Else              ' IS empty (only once).
                    strB = vntS(1, j)
                End If
            End If
        End If
    Next

    ' Write String Builder to Conditionally Concatenate Row.
    CCROW = strB

End Function

在Excel中的用法

=CCROW(A$1:I$1,A3:I3) ' Result: ADG
=CCROW(A$1:I$1,A4:I4) ' Result: CFI
=CCROW(A$1:I$1,A5:I5) ' Result: DG

如果你添加JoinString

=CCROW(A$1:I$1,A3:I3,,",")   ' Result: A,D,G
=CCROW(A$1:I$1,A3:I3,0,",")  ' Result: A,D,G
=CCROW(A$1:I$1,A3:I3,0,", ") ' Result: A, D, G

如果你改变CriteriaValue

=CCROW(A$1:I$1,A3:I3,1) ' Result: BCEFHI
=CCROW(A$1:I$1,A4:I4,1) ' Result: ABDEGH
=CCROW(A$1:I$1,A5:I5,1) ' Result: ABCEFHI

备注锁定($SourceRowRange的行,以便在复制公式时保持相同。


0
投票

以下数组公式将完成此任务(使用Ctrl + Shift + Enter输入):

=CONCAT(IF($A1:$I1=0,UNICHAR(64+COLUMN($A1:$I1)),""))

对于较旧的Excel版本,请使用旧函数CONCATENATE()CHAR()代替这些函数。

© www.soinside.com 2019 - 2024. All rights reserved.