0

我正在使用 excel,如果单元格中的值在 10+ 列的范围内为 x,我正在尝试删除一列。数据过滤不起作用,因为它会删除行。所以,我需要选择指定行中的单元格包含值 x 的所有列。我正在录制宏并尝试自动执行该操作。欢迎使用宏代码,但不是必需的。谢谢。

4

1 回答 1

2

像这样?将此代码粘贴到模块中。

Sub Sample()
    Dim ws As Worksheet
    Dim Rng As Range
    Dim Rw As Long, i as Long

    '~~> This is the relevant sheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    '~~> This the row where you want to check
    Rw = 1

    With ws
        '~~> I am assuming there are 10 cols. Change as applicable
        For i = 1 To 10
            '~~> UCASE so that it check for x and X
            If UCase(.Cells(Rw, i).Value) = "X" Then
                '~~> Set your range
                If Rng Is Nothing Then
                    Set Rng = .Columns(i)
                Else
                    Set Rng = Union(Rng, .Columns(i))
                End If
            End If
        Next i
    End With

    If Not Rng Is Nothing Then Rng.Delete Shift:=xlRight
End Sub

截屏

在此处输入图像描述

于 2013-04-03T16:06:20.873 回答