清除列数据,但第一个标题除外| Excel | VBA |

时间:2019-05-26 13:09:48

标签: excel vba

我要删除按钮单击=>上的“ A”和“ B”列数据,但列A和B的第一个单元格均为标题

我的工作表:

Sheet

    Private Sub CommandButton1_Click()

        Dim myRange As Range
        Set myRange = ThisWorkbook.Worksheets("Sheet2").Range("A:B")
        myRange.Clear

   End Sub

仅清除数据(A:B的第一个标题除外)

1 个答案:

答案 0 :(得分:2)

这里:

Option Explicit
Private Sub CommandButton1_Click()

    Dim myRange As Range
    Dim LastRow As Long 'declare a long variable to find the last row

    With ThisWorkbook.Sheets("Sheet2")
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row 'last row on column A
        Set myRange = .Range("A2:B" & LastRow) 'this way you avoid headers and clear everything
        myRange.Clear
    End With

End Sub