根据单元格值连接每列中的值
Concatenate value in each column based on cell value
您好,我需要根据行是否为空或具有任何值来连接列。
IE。我有这样的数据
我需要这样的数据
问题是我要加入的列将超过 50 个,我必须检查每个列和行中是否有空白或数字。
你能帮我解决这个问题吗?
使用 Microsoft-365 你可以试试-
=TEXTJOIN("; ",TRUE,FILTER(IF($B:$D<>"",$B:$D&":"&$B:$D,""),$A:$A=F2))
如果您要求 VBA 解决方案,请尝试下一个代码。它将处理尽可能多的 Value
列,并且 returns 是最后一列后两列的处理结果。使用数组,即使对于大范围,代码也会非常快:
Sub ConcatenateWithValues()
Dim sh As Worksheet, lastR As Long, lastCol As Long, arr, arrH, arrFin
Dim strConc As String, i As Long, j As Long
Set sh = ActiveSheet 'use here the sheet you need
lastR = sh.Range("A" & sh.Rows.count).End(xlUp).row 'last row on column A:A
lastCol = sh.cells(1, sh.Columns.count).End(xlToLeft).Column 'last column on the first row
arr = sh.Range("A1", sh.cells(lastR, lastCol)).Value 'place the whole range in an array, for faster iteration
arrH = sh.Range("A1", sh.cells(1, lastCol)).Value 'Place headers in an array
ReDim arrFin(1 To UBound(arr), 1 To 2) 'Redim the array to contain the processing result
arrFin(1, 1) = arrH(1, 1): arrFin(1, 2) = "Concatenate_Value"'Place the headers in the array
For i = 2 To UBound(arr)
For j = 2 To lastCol 'build the necessary concatenation
If arr(i, j) <> "" Then strConc = strConc & arrH(1, j) & ":" & arr(i, j) & ";"
Next
arrFin(i, 1) = arr(i, 1): arrFin(i, 2) = left(strConc, Len(strConc) - 1) 'remove the last ";" character
strConc = "" 'reinitialize the string keeping row concatenation
Next i
'Drop the processed array content at once:
sh.cells(1, lastCol + 2).Resize(UBound(arrFin), 2).Value = arrFin
End Sub
您好,我需要根据行是否为空或具有任何值来连接列。
IE。我有这样的数据
我需要这样的数据
问题是我要加入的列将超过 50 个,我必须检查每个列和行中是否有空白或数字。
你能帮我解决这个问题吗?
使用 Microsoft-365 你可以试试-
=TEXTJOIN("; ",TRUE,FILTER(IF($B:$D<>"",$B:$D&":"&$B:$D,""),$A:$A=F2))
如果您要求 VBA 解决方案,请尝试下一个代码。它将处理尽可能多的 Value
列,并且 returns 是最后一列后两列的处理结果。使用数组,即使对于大范围,代码也会非常快:
Sub ConcatenateWithValues()
Dim sh As Worksheet, lastR As Long, lastCol As Long, arr, arrH, arrFin
Dim strConc As String, i As Long, j As Long
Set sh = ActiveSheet 'use here the sheet you need
lastR = sh.Range("A" & sh.Rows.count).End(xlUp).row 'last row on column A:A
lastCol = sh.cells(1, sh.Columns.count).End(xlToLeft).Column 'last column on the first row
arr = sh.Range("A1", sh.cells(lastR, lastCol)).Value 'place the whole range in an array, for faster iteration
arrH = sh.Range("A1", sh.cells(1, lastCol)).Value 'Place headers in an array
ReDim arrFin(1 To UBound(arr), 1 To 2) 'Redim the array to contain the processing result
arrFin(1, 1) = arrH(1, 1): arrFin(1, 2) = "Concatenate_Value"'Place the headers in the array
For i = 2 To UBound(arr)
For j = 2 To lastCol 'build the necessary concatenation
If arr(i, j) <> "" Then strConc = strConc & arrH(1, j) & ":" & arr(i, j) & ";"
Next
arrFin(i, 1) = arr(i, 1): arrFin(i, 2) = left(strConc, Len(strConc) - 1) 'remove the last ";" character
strConc = "" 'reinitialize the string keeping row concatenation
Next i
'Drop the processed array content at once:
sh.cells(1, lastCol + 2).Resize(UBound(arrFin), 2).Value = arrFin
End Sub