首页 文章

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

提问于
浏览
0

特定列中的每个单元格都包含“x”或为空 . 我想创建一个查找所有x的子,然后删除同一行右边所有单元格的内容 . 我不确定如何在下面的IF语句中正确选择和清除行的内容 . 任何帮助深表感谢 .

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

1 回答

  • 0

    试试这个 . 顺便说一句,你在Range语句前面缺少一个点,你需要使用With语句 .

    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
    

相关问题