VBA,清除选定行从列A到J的内容

时间:2019-04-15 11:39:51

标签: excel vba

我需要帮助来创建一个宏,该宏将清除所选行的内容,但仅清除A至J列,我可以清除整行,但这并不是很有用,因为该行上有多个表。

请帮助

谢谢!

3 个答案:

答案 0 :(得分:1)

Application.Intersect(Selection.EntireRow, Selection.Worksheet.Columns("A:J")).ClearContents

答案 1 :(得分:1)

一行:

Option Explicit

Sub test()

    Dim RowNo As Long

    With ThisWorkbook.Worksheets("Sheet1") '<- Change sheet name if needed

        RowNo = Selection.Row '<- Get row number

        .Range("A" & RowNo & ":J" & RowNo).ClearContents '<- Clear range A to J of the selected line

    End With

End Sub

多行:

Option Explicit

Sub test()

    Dim rng As Range

    With ThisWorkbook.Worksheets("Sheet1") '<- Change sheet name if needed

        For Each rng In Selection.Rows
            .Range("A" & rng.Row & ":J" & rng.Row).ClearContents
        Next

    End With

End Sub

答案 2 :(得分:0)

您可以遍历selectin中的每个单元格,并清除其内容:

Sub test()
  Dim R As Range
  For Each R In Selection:
    If R.Column <= 10 Then
    R.ClearContents
    End If
  Next
End Sub

Column小于10意味着位于列AJ之间。