如果不喜欢那么删除列

时间:2017-12-12 16:28:08

标签: vba excel-vba excel

我在一个单元格中有几个值,名为'PIDList',如下:

'I.A.3', 'I.A.4', 'I.O.9', 'I.U.3', 'I.U.4', 'I.U.6', 'O.D.1', 'O.D.2'

我正在尝试遍历另一个工作表中的列,如果标题不在上面的列表中,请删除该列。

'Find the last used column in a Row: row 1 in this example
Dim LastCol As Integer
With ActiveSheet
    LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
End With

Dim lColumn As Long
Dim iCntr As Long
lColumn = LastCol
For iCntr = lColumn To 1 Step -1
    If Not Cells(1, iCntr).Value Like "*" & PIDList & "*" And Cells(1, iCntr).Value <> "Delta" And Not Cells(1, iCntr).Value <> "CloseBal" _
        And Not Cells(1, iCntr).Value <> "Totals" And Not Cells(1, iCntr).Value <> "DESC1" And Not Cells(1, iCntr).Value <> "AsOfDate" Then
        Columns(iCntr).Delete
    End If
    Debug.Print Cells(1, iCntr).Value
Next

在专栏中,我有这样的数据:

I.A.3   I.A.4   I.O.9   I.U.3   I.U.4   I.U.6   O.D.1   O.D.2   O.D.3   O.D.4   O.D.5   O.D.6

如果Not ... Like为true,则不会删除任何列。我在有和没有引号的情况下尝试过它;同样的结果。知道这里有什么问题吗?

4 个答案:

答案 0 :(得分:2)

据我了解,您的条件子句的格式为

if (A) and (B) and (C) and (D) then

如果A,B,C或D中的任何一个为假,则不执行删除。

您的某些条件属于

形式
And Not Cells(1, iCntr).Value <> "CloseBal"
And Not Cells(1, iCntr).Value <> "Totals"

可翻译为

And Cells(1, iCntr).Value = "CloseBal"
And Cells(1, iCntr).Value = "Totals"

其中一个条件肯定是假的。

所以我认为整个子句都是假的,这意味着没有列被删除。那不是这样吗?

答案 1 :(得分:1)

首先,让我们澄清你想要的东西。据我了解,如果包含任何列出的标题,您想要删除列。

"*"PIDListCloseBalTotals作为一列的标题是不合逻辑的。因此,在"AND"语句中使用IF会使该子句为false。尝试使用"OR"

另外,请尝试倒退:

If header = "*" OR header = PIDList OR header = CloseBal OR header = Totals
then print "header match"
else
delete column
End if

* NOT'<>'表示NOT NOTEQUAL TO,即'IS EQUAL TO'。这使你的条款反之亦然。

答案 2 :(得分:1)

稍微不同的方法是,这将删除任何与数组中的值不匹配的列:

Sub foo()
Dim iCntr As Long
Dim val As String
Dim PIDList() As Variant
Dim LastCol As Long
PIDList = Array("I.A.3", "I.A.4", "I.O.9", "I.U.3", "I.U.4", "I.U.6", "O.D.1", "O.D.2", "Delta", "CloseBal", "Totals", "DESC1", "AsOfDate") 'add all values to keep to this array
LastCol = ActiveSheet.Cells(1, ActiveSheet.Columns.Count).End(xlToLeft).Column
For iCntr = LastCol To 1 Step -1
    val = "Not Found" 'set variable as not found
    currentcol = ActiveSheet.Cells(1, iCntr).Value 'get the value of the column header for comparison
    For x = LBound(PIDList) To UBound(PIDList)
    If currentcol = PIDList(x) Then 'loop through array to see if match is found
        val = "Found"
    End If
    Next x
    If val <> "Found" Then 'if match is not found then delete
        ActiveSheet.Columns(iCntr).EntireColumn.Delete
        val = "" 'reset variable for next loop
    End If
Next iCntr
End Sub

答案 3 :(得分:0)

好的,我想你必须用2个IF语句做到这一点:

    If Not PIDList Like "*" & Cells(1, iCntr).Value & "*" Then
        If Cells(1, iCntr).Value <> "Delta" And _
            Cells(1, iCntr).Value <> "CloseBal" And _
            Cells(1, iCntr).Value <> "Totals" And _
            Cells(1, iCntr).Value <> "DESC1" And _
            Cells(1, iCntr).Value <> "ASOFDATE" Then
                Columns(iCntr).Delete
        End If
    End If
Next

这适合我。