单击删除按钮“ btnS2”时,将范围剪切,粘贴和上移一行

时间:2018-07-05 02:44:43

标签: excel vba excel-vba

我已经创建了一个宏,该宏将数据添加到列表中,在该列表数据旁边创建了一个按钮以创建一个按钮,单击该子按钮允许您将其删除,并另外创建了一个btnS2()实际删除数据的子按钮。我已经做好了所有工作,除非有人从列表中间删除数据,我需要此空白才能由其下面的数据填充或“向上移动”。我什至几乎完成了此操作,但是我似乎无法正确粘贴它,因此这可能是一个非常简单的问题。请帮忙。

Sub btnS2()
Dim b As Object
Dim r As Long, c As Long, firstrow As Long
Dim cell As Range

With Worksheets("Skill Summary")
    Set b = .Buttons(Application.Caller)  'references button
    With b.TopLeftCell                  'returns row and col of button pushed
        r = .row
        c = .Column - 1
    End With

    Set cell = .Cells(r, c)
    cell.ClearContents
    Call buttonGenerator_skillSummary
    Call shiftup(cell)

End With
End Sub

所以这是我的一个选择范围的对象,将其剪切并粘贴到上方一行。我已经经理让除倒数第二行Worksheets("Skill Summary").selection.Offset(-1, 0).Paste

以外的所有内容正常工作
Sub shiftup(cell As Range)
Dim sel As Range
Dim firstrow As Long
Dim lastrow As Long

firstrow = cell.row + 1
lastrow = 1000

Set sel = Range("B" & firstrow & ":" & "B" & lastrow)
With sel
    .Select
    .Cut
End With
Worksheets("Skill Summary").selection.Offset(-1, 0).Paste
End Sub

1 个答案:

答案 0 :(得分:1)

使用可选的Destination参数尝试Range.Cut

Sub shiftup(cell As Range)

    Dim firstrow As Long, lastrow As Long

    firstrow = cell.row + 1
    lastrow = 1000

    with cell.parent.Range("B" & firstrow & ":B" & lastrow)
        .cut destination:=.offset(-1, 0)
    end with

End Sub