Excel 如何更改宏以删除;“列名称列表”;从中删除除名称列表以外的所有名称

Excel 如何更改宏以删除;“列名称列表”;从中删除除名称列表以外的所有名称,excel,vba,Excel,Vba,如何更改宏,使其删除列列表,因为它现在可以删除列以外的所有内容 我试过了,但没能得到 谢谢 Sub DeleteColumns() Dim ws As Worksheet Dim ColList As String, ColArray() As String Dim LastCol As Long, i As Long, j As Long Dim boolFound As Boolean Dim delCols As Range On Error GoTo Whoa Applicatio

如何更改宏,使其删除列列表,因为它现在可以删除列以外的所有内容

我试过了,但没能得到

谢谢

Sub DeleteColumns()

Dim ws As Worksheet
Dim ColList As String, ColArray() As String
Dim LastCol As Long, i As Long, j As Long
Dim boolFound As Boolean
Dim delCols As Range

On Error GoTo Whoa

Application.ScreenUpdating = False

'~~> Set your sheet here
Set ws = Sheets("360")

'~~> List of columns you want to keep. You can keep adding or deleting from this.
'~~> Just ensure that the column names are separated by a COMMA
'~~> The names below can be in any case. It doesn't matter
ColList = "{TOKEN:ATTRIBUTE_3}, {TOKEN:ATTRIBUTE_4}"

'~~> Create an array for comparision
ColArray = Split(ColList, ",")

'~~> Get the last column
LastCol = ws.Cells.Find(What:="*", After:=ws.Range("A1"), Lookat:=xlPart, _
LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, _
MatchCase:=False).Column

'~~> Loop through the Cols. Since there are only 100 Columns
'~~> I am not using .Find and .FindNext
'~~> If you are interested in learning how .Find and .Findnext
'~~> works then see this link
'~~> http://siddharthrout.wordpress.com/2011/07/14/find-and-findnext-in-excel-vba/
For i = 1 To LastCol
    boolFound = False
    '~~> Checking of the current cell value is present in the array
    For j = LBound(ColArray) To UBound(ColArray)
        If UCase(Trim(ws.Cells(1, i).Value)) = UCase(Trim(ColArray(j))) Then
            '~~> Match Found
            boolFound = True
            Exit For
        End If
    Next
   '~~> If not match not found
    If boolFound = False Then
        If delCols Is Nothing Then
            Set delCols = ws.Columns(i)
        Else
            Set delCols = Union(delCols, ws.Columns(i))
        End If
    End If
Next i

'~~> Act on columns
If Not delCols Is Nothing Then delCols.Delete

LetsContinue:
    Application.ScreenUpdating = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume LetsContinue

End Sub

你能不能换一行

If boolFound = False Then
检查是否为真

If boolFound = True Then

谢谢你的回复。我尝试了你的建议,但没有成功。“没有成功”并没有告诉我们任何事情。什么意思?原始代码查找不属于ColArray的任何列。它使用boolFound变量进行检查,然后查看boolFound是否为False。通过更改它来检查boolFound是否为true,它将转而查找属于ColArray的列。很抱歉,我缺乏清晰度和清晰度。我删除了我的宏,然后又添加了它,并对其进行了更改,这一次它按预期工作,在我试图找出如何修改代码的过程中,我引入了一个错误。谢谢你的帮助。