0

我需要: 1. 在标题中找到包含“目标”一词的列。(从 P1 到 QI1 范围) 2. 删除标题中没有“目标”的所有其他列。

代码:

Sub Cleanup()
    Dim rng As Range
    With ActiveSheet.Range("P1:QI1")
        Set rng = ActiveSheet.Range("P1:QI1").Find(What:="target", _
            LookAt:=xlPart, MatchCase:=False)
        Do While Not rng Is Nothing
            rng.EntireColumn.Delete
            Set rng = .FindNext
        Loop
    End With
End Sub

上面的代码正在删除所有带有“目标”一词的列。我想反过来。我需要保留这些列。

提前致谢。

4

1 回答 1

0

测试 - 享受:)

Sub test()
Dim erange As Range
Dim str As String
    For Each erange In Range("A1:E1")
      If not erange.Value = "Target" Then
      '  If InStr(erange.Value, "Target") <> 0 Then
            If str <> "" Then
                str = str & "," & erange.EntireColumn.Address
            Else
                str = erange.EntireColumn.Address
            End If
        End If
    Next erange
str = Replace(str, "$", "")
' Delete columns in single shot
Range(str).Delete
End Sub
于 2013-10-28T07:14:54.743 回答