如果单元格值为x,则删除列

我在Excel中工作,我试图删除一列,如果单元格中的值是X超过10 +列的范围。 数据过滤不起作用,因为它会删除行。所以,我需要select指定行中的单元格包含值x的所有列。 我正在录制macros并试图自动执行此操作。 macros代码是受欢迎的,但没有必要。谢谢。

喜欢这个? 将此代码粘贴到模块中。

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 

截图

在这里输入图像说明