VBA剪切整行并粘贴更改后的下一列

时间:2018-11-12 06:43:29

标签: excel vba insert rows

当前,VBA在值更改时插入新行。如何更改,使其仅向下移动一列而不插入新行。我正在尝试将所有内容保持在一页上。

例如:

Example

Sub InsertRowsAtValueChange() Dim rng As Range Dim WorkRng As Range On Error Resume Next xTitleId = "KutoolsforExcel" Set WorkRng = Application.Selection Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8) Application.ScreenUpdating = False For i = WorkRng.Rows.Count To 2 Step -1 If WorkRng.Cells(i, 1).Value <> WorkRng.Cells(i - 1, 1).Value Then WorkRng.Cells(i, 1).EntireRow.Insert End If Next Application.ScreenUpdating = True End Sub

1 个答案:

答案 0 :(得分:0)

很难理解要粘贴整行的位置。如果复制整个行然后尝试将其粘贴到B列,则会收到错误消息。也许下面的代码会为您提供帮助。目标是您希望整行放置的位置。

Dim ws as Worksheet

Set ws = Worksheets("Sheet1")

    WorkRng.Cells(i, 1).EntireRow.Copy Destination:=ws.Cells(i, 1)