简体   繁体   English

VBA清除单元格旁边行的内容

[英]VBA clear content of rows next to cells

Each cell in a specific column either contains an 'x' or is empty. 特定列中的每个单元格都包含一个“ x”或为空。 I want to create a sub which finds all the x's and then delete the content of all cells to the right in that same row. 我想创建一个找到所有x的子,然后删除同一行右边所有单元格的内容。 I am not sure how to properly select and clearContents of the rows in the IF statement below. 我不确定如何在下面的IF语句中正确选择和清除行的内容。 Any help is much appreciated. 任何帮助深表感谢。

    Sub clearContents()
        With Sheets("Main")
            Dim c As Range
            For Each c In Range("B1:B23")
                If c.Value = "x" Then
                    c.Offset(0, 1).End(xlToRight).clearContents
                End If
            Next c
         End With
    End Sub

Try this. 尝试这个。 By the way you were missing a dot in front of the Range which you need for your With statement. 顺便说一句,您在With语句所需的Range前面缺少一个点。

Sub clearContents()
    Dim c As Range, c1 As Long
    With Sheets("Main")
        For Each c In .Range("B1:B23")
            If c.Value = "x" Then
                c1 = Cells(c.Row, Columns.Count).End(xlToLeft).Column
                If c1 > 2 Then c.Offset(0, 1).Resize(, c1 - 2).clearContents
            End If
        Next c
     End With
End Sub

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

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