【问题标题】:Search for next non-blank cell, adding row and moving it with the cell next to it搜索下一个非空白单元格,添加行并将其与旁边的单元格一起移动
【发布时间】:2015-08-06 17:42:04
【问题描述】:

我正在寻找整理小工作簿的帮助。 我要删除一些列,但在删除它们之前,我需要将单元格中的值移动到不同的列。 A 列和 C 列保留(具有无法删除的值),但 B 列和 C 列应该被删除。 B 列和 C 列中的某些行中包含值。我正在寻找一个代码来查找 B 列中的值并将其移动到 A 列,然后从 C 列中获取与 B 中的值相同的行中的值并将其移动到 D 列。我已经管理了让它在 B 列中查找非空白单元格并将其移动到 A 列中的新行,但我正在努力让 C 列中的对应值与它一起移动。我会很感激任何帮助。 到目前为止我的代码:

    Sub InsR()

Sheets("sheet1").Select
Dim LR2 As Long, cell2 As Range, rng2 As Range
With Sheets("sheet1")
    LR2 = .Range("B" & Rows.Count).End(xlUp).Row
    For Each cell2 In .Range("B2:B" & LR2)
        If cell2.Value <> "" Then
            If rng2 Is Nothing Then
                Set rng2 = cell2

            End If

        End If
    Next cell2
    rng2.Cut
ActiveCell.EntireRow.Insert
End With

End Sub

【问题讨论】:

  • 我认为您的意思是“A 列和 D 列保留”而不是“A 列和 C 列保留”

标签: vba excel


【解决方案1】:

替换

    If cell2.Value <> "" Then
        If rng2 Is Nothing Then
            Set rng2 = cell2

        End If

    End If

这样的代码应该可以做到

   If cell2.Value <> "" Then

        ' cell2 is a cell in column B

        ' Set cell in column A equal to column B
        cell2.Offset(0, -1).Value = cell2.Value


        ' Set cell in column D equal to column C
        cell2.Offset(0, 2).Value = cell2.Offset(0, 1).Value

    End If

【讨论】:

    猜你喜欢
    • 2023-02-23
    • 2022-01-26
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多