excel 多列合并为一列,用逗号分隔

yhqotfr8  于 2023-01-27  发布在  其他
关注(0)|答案(4)|浏览(347)

我有2k+行的文件,数据如下:

我想把它改为一行一列,数据用逗号分隔。
我找到了vba代码,它可以完成这项工作,但我必须分别选择每一行和每一列,它会将空白单元格显示为一堆逗号,我不会显示。
我的代码:

Sub Columns_to_rows()
'
' Columns to rows Makro
'
Dim rng As Range
Dim InputRng As Range, OutRng As Range
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Range :", xTitleId, InputRng.Address, Type:=8)
Set OutRng = Application.InputBox("Out put to (single cell):", xTitleId, Type:=8)
outStr = ""
For Each rng In InputRng
    If outStr = "" Then
        outStr = rng.Value
    Else
        outStr = outStr & "," & rng.Value
    End If
Next
OutRng.Value = outStr
End Sub
wi3ka0sx

wi3ka0sx1#

使用现有代码作为起点,可以使用TEXTJOIN函数公式构建回复,然后删除公式,只留下响应:

Sub Columns_to_rows()
    '
    ' Columns to rows Macro
    '
    Dim rng As Range, ofst As Long
    Dim InputRng As Range, OutRng As Range
    Set InputRng = Application.Selection
    Set InputRng = Application.InputBox("Range :", xTitleId, InputRng.Address, Type:=8)
    Set OutRng = Application.InputBox("Out put to (single cell):", xTitleId, Type:=8)
    
    ofst = 0
    
    For Each rng In InputRng.Rows
        OutRng.Offset(ofst).Formula = "=TextJoin("","", True, " & rng.Address & ")"
        ofst = ofst + 1
    Next
    OutRng.Resize(ofst).Value = OutRng.Resize(ofst).Value
    
End Sub
7fhtutme

7fhtutme2#

由于您有TEXTJOIN,因此可以使用以下公式代替VBA:
=BYROW(A1:G3,LAMBDA(r,TEXTJOIN(",",TRUE,r)))
必须传递要为其输入连接值的区域的位置。
如果要继续使用VBA解决方案,可以使用此函数:

Sub mergeColumnsToOneRowEach(rgStart As Range, rgTarget As Range)

Dim rgSource As Range
Set rgSource = rgStart.CurrentRegion

Dim arrSource As Variant
arrSource = rgSource.Value

Dim arrTarget As Variant
ReDim arrTarget(1 To UBound(arrSource, 1))

Dim i As Long, j As Long
For i = 1 To UBound(arrSource, 1)
    For j = 1 To UBound(arrSource, 2)
        If LenB(arrSource(i, j)) > 0 Then
            arrTarget(i) = arrTarget(i) & arrSource(i, j) & ", "
        End If
    Next
Next

'add apstroph to the start, so that text is inserted
'remove comma at the end
For i = 1 To UBound(arrTarget)
    arrTarget(i) = "'" & Left(arrTarget(i), Len(arrTarget(i)) - 2)
Next
        
rgTarget.Resize(UBound(arrTarget, 1), 1).Value = Application.Transpose(arrTarget)

End Sub

您必须传递应该处理的区域的第一个单元格以及要放置新内容的目标单元格。
我使用数组来迭代--这比查看单元格值要快得多。

hgb9j2n6

hgb9j2n63#

与Ike的解决方案稍有不同。只需选择要合并的范围并运行宏。原始数据将被清除,并被最左边列中的合并值所取代。

Sub mergeCols()
    Dim separator As String
    separator = ", "
    
    Dim arr() As Variant
    arr = Selection
    For i = 1 To UBound(arr)
        Dim rowString As String
        rowString = vbNullString
        For j = 1 To UBound(arr, 2)
            Dim cellVal As String
            cellVal = arr(i, j)
            If Not cellVal = vbNullString Then rowString = rowString & cellVal & separator
            arr(i, j) = vbNullString
        Next j
        Debug.Print rowString
        If Not rowString = vbNullString Then arr(i, 1) = Left(rowString, Len(rowString) - Len(separator))
    Next i
    Selection = arr
End Sub
rjzwgtxy

rjzwgtxy4#

因为我没有TEXTJOIN函数,我尝试如下:

Sub test()
Dim rg As Range: Dim col As Integer: Dim i As Integer
Set rg = ActiveSheet.UsedRange 'change if needed
col = Range(Split(rg.Address, ":")(1)).Column + 3
    For i = 1 To rg.Rows.Count
        Cells(i, col).Value = Join(Application.Transpose( _
        Application.Transpose(rg.Rows(i).SpecialCells(xlConstants))), ", ")
    Next i
End Sub

范围输入是活动工作表的使用范围。
结果将放在数据偏移量为(0,3)的最后一列。
该代码假定在一行数据中的中间没有空白单元格。
例如:
A1、B1、C1、D1---〉全部包含值---〉正确数据。
A1、C1、D1有数值,B1为空---〉数据不正确。

相关问题