选中复选框后,将行复制到新工作表

时间:2016-05-16 23:01:29

标签: vba excel-vba excel

我试图在选中复选框后将所有数据从一行(从A:O列)复制到新工作表。但是,我所拥有的代码也是复制所有以前检查过的行。我不想取消选择之前选中的任何框。

Sub CopyRows()
    Dim LRow As Long, ChkBx As CheckBox, WS2 As Worksheet
    Set WS2 = Worksheets("Sheet2")
    LRow = WS2.Range("A" & Rows.Count).End(xlUp).Row
    For Each ChkBx In ActiveSheet.CheckBoxes
        If ChkBx.Value = 1 Then
            LRow = LRow + 1
            WS2.Cells(LRow, "A").Resize(, 14) = Range("A" & _
            ChkBx.TopLeftCell.Row).Resize(, 14).Value
        End If
    Next
 End Sub

1 个答案:

答案 0 :(得分:0)

请使用ActiveCell.Row尝试以下修改后的代码(假设选中/激活了最后一个复选框行。)

Sub CopyRows()
    Dim LRow As Long, r As Long, ChkBx As CheckBox, WS2 As Worksheet
    Set WS2 = Worksheets("Sheet2")
    r = ActiveCell.Row
    WS2.Cells(r, "A").Resize(, 14) = Range("A" & ChkBx.TopLeftCell.Row).Resize(, 14).Value
End Sub