excel - 是否能够在保留相邻列的值的同时拆分单元格?

标签 excel vba excel-formula

第一个表中的 ID 列在每个单元格中包含多个需要拆分的值。然而,独特的问题是将 [name] 和 [description] 信息按 ID 保留到新表中。

excel image .

以下 VBA 代码执行转置粘贴选项。这就是我开始使用 Chr(10) 或新行作为分隔符分割单元格的方法:

Sub splitText()
'splits Text active cell using ALT+10 char as separator
Dim splitVals As Variant
Dim totalVals As Long

splitVals = Split(ActiveCell.Value, Chr(10))
totalVals = UBound(splitVals)
Range(Cells(ActiveCell.Row, ActiveCell.Column + 1), Cells(ActiveCell.Row, ActiveCell.Column + 1 + totalVals)).Value = splitVals

End Sub

除此之外,我仍在寻找想法。

最佳答案

也许这会有所帮助:

Sub splitText()
    'splits Text active cell using ALT+10 char as separator
    Dim splitVals As Variant
    Dim lngRow As Long, lngEl As Long

    With Sheet2
        'Range A2:A5
        For lngRow = 5 To 2 Step -1
            splitVals = Split(.Range("A" & lngRow).Value, Chr(10))
            'the first value
            .Range("A" & lngRow).Value = splitVals(0)
            'remaining values
            For lngEl = 1 To UBound(splitVals)
                .Rows(lngRow + lngEl).Insert
                .Range("A" & lngRow + lngEl).Value = splitVals(lngEl)
                .Range("B" & lngRow + lngEl & ":C" & lngRow + lngEl).Value = .Range("B" & lngRow & ":C" & lngRow).Value
            Next lngEl
        Next lngRow
    End With
End Sub

根据需要更改工作表代码/名称和范围。

之前:

enter image description here

之后:

enter image description here

关于excel - 是否能够在保留相邻列的值的同时拆分单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57591667/

相关文章:

c# - EPPlus - 如何使用模板

vba - 如何在vba中比较时间

Excel问题: How to find duplicate string of six digits in a cell?

Excel - Sumproduct来自同一列的两个数组

excel - 在两张纸的两列中查找重复项

excel - Microsoft Excel - 如何自动填充列中的公式?

excel - 值 0 未显示在单元格中。为什么?

VBA Excel结束如果没有 block 如果错误

excel - 在 VBA 中更快速地编写每个语句

excel - 如何使用 VBA 定义单元格中的字符是数字、字母还是特殊字符?