简体   繁体   English

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

[英]Clear Column Data except first Headers |Excel|VBA|

I want to Delete the Column Data of "A" and "B" on button click => except the First Cell of both the Columns A and B which are headers 我想在按钮单击=>上删除“ A”和“ B”的列数据,但列A和B的第一个单元格都是标题

My Sheet : 我的工作表:

片

    Private Sub CommandButton1_Click()

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

   End Sub

Clear only Data [except the first Headers of A:B] 仅清除数据(A:B的第一个标题除外)

Here: 这里:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM