我在excel工作,如果单元格中的值在10+列的范围内是x,我正在尝试删除列。数据过滤不起作用,因为它删除了行。因此,我需要选择指定行中的单元格包含值x的所有列。我正在录制宏并尝试自动执行该操作。宏代码是受欢迎的,但没有必要。谢谢。
答案 0 :(得分: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
<强>截图强>