【问题标题】:Excel: Copying text from 1 cell and ADDING it to the top of the cell next to itExcel:从 1 个单元格复制文本并将其添加到其旁边单元格的顶部
【发布时间】:2018-09-05 07:14:37
【问题描述】:

我有以下场景:

我有大约 200 行数据和两列。第一列包含“最新更新”。第二列包含“存档更新”

例子:

我希望能够选择一个 CELL,然后单击一个宏按钮。这样做是在该单元格中获取文本并将其剪切到右侧的单元格,在该单元格内容的顶部插入内容。

右侧单元格中的最终结果将是来自所选单元格(例如 B2)的 TEXT + 来自原始单元格(例如 C2)的 TEXT

谢谢!

【问题讨论】:

  • 到目前为止,您研究/尝试了什么,为什么不起作用?

标签: vba excel text copy


【解决方案1】:

这样就可以了。它在设置为columnChecked 的列上循环,如果它右侧的单元格中的顶部条目(“存档”)与“当前”条目不匹配,则将该条目插入到相邻的“存档”单元格中。您可以/可能必须调整 sheetName、columnChecked、rowStart 和 rowEnd 以适应您的数据。

Option Explicit

Sub test()

    Dim sheetName As String
    sheetName = "Sheet1"

    Dim columnChecked As String
    columnChecked = "B"

    Dim rowStart As Long
    rowStart = 2

    Dim rowEnd As Long
    rowEnd = sheets(sheetName).Range(columnChecked & Rows.count).End(xlUp).row


    Dim cell As Range
    For Each cell In sheets(sheetName).Range(columnChecked & rowStart & ":" & columnChecked & rowEnd).Cells
        If Split(cell.offset(0, 1).Value2, Chr(10))(0) <> cell.Value2 Then
            cell.offset(0, 1).Value2 = cell.Value2 & Chr(10) & cell.offset(0, 1).Value2
        End If
    Next cell

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    相关资源
    最近更新 更多