【问题标题】:How can I merge contents of 2 cells into 1 and shift the remaining columns in Excel?如何将 2 个单元格的内容合并为 1 个并在 Excel 中移动剩余的列?
【发布时间】:2014-10-01 17:01:23
【问题描述】:

我正在尝试将 excel 中同一行中的单元格合并为 1 个单元格,然后将剩余的单元格向左移动。

例子:

内容 |样品 |更多 |更多2

所以我想合并“Content”和“Sample”并将“More”和“More2”单元格向左移动,这样结果就是。

内容示例 |更多 |更多2

我找到了一个合并 2 列的代码,但是它将 2 列合并为 1,但它仍然占用了第二列。所以我必须“取消合并”并删除第二列并将“更多”和“更多2”向左移动。

这是我的代码:

    Sub MergeOneCell()
    'Updateby20140128
    Dim Rng As Range
    Dim WorkRng As Range
    Dim Sigh As String
    On Error Resume Next
    xTitleId = "KutoolsforExcel"
    Set WorkRng = Application.Selection
    Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
    Sigh = Application.InputBox("Symbol merge", xTitleId, ",", Type:=2)
    xOut = ""
    Application.DisplayAlerts = False
    For Each Rng In WorkRng
    xOut = xOut & Rng.Value & Sigh
    Next
    With WorkRng
    .Merge
    .Value = VBA.Left(xOut, VBA.Len(xOut) - 1)
    End With
    Application.DisplayAlerts = True
    End Sub

任何帮助将不胜感激。我有大量数据要处理,能够做到这一点将为我节省数小时。到目前为止,我还没有找到任何解决方案。

【问题讨论】:

    标签: excel merge cell


    【解决方案1】:

    为什么不像这样循环遍历数据

    'Find the last row of data
    finalRow = cells(65000,1).end(xlup).row
    for i = 1 to finalRow
      cells(i,1) = cells(i,1) & " " & cells(i,2)
      cells(i,2) = cells(i,3)
      cells(i,3) = cells(i,4)
    next i
    

    这假定Content | Sample | More | More2 位于 A、B、C 和 D 列中,并且数据从第 1 行开始。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-11
      • 2023-03-02
      • 2011-03-24
      • 1970-01-01
      • 2020-04-24
      • 1970-01-01
      • 2022-12-07
      相关资源
      最近更新 更多