Vba 如果不喜欢,则删除列

Vba 如果不喜欢,则删除列,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

我在一个名为“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

如果Not…Like为true,则不会删除任何列。我试过了,有没有引用;同样的结果。你知道这里出了什么问题吗?

据我所知,你的条件从句的形式是

if (A) and (B) and (C) and (D) then
And Not Cells(1, iCntr).Value <> "CloseBal"
And Not Cells(1, iCntr).Value <> "Totals"
如果A、B、C或D中的任何一个为false,则不执行删除

你的一些条件是

if (A) and (B) and (C) and (D) then
And Not Cells(1, iCntr).Value <> "CloseBal"
And Not Cells(1, iCntr).Value <> "Totals"
其中一个条件肯定是错误的


因此,我认为整个条款肯定是错误的,这意味着任何列都不会被删除。不是这样吗?

据我所知,你的条件从句的形式如下

if (A) and (B) and (C) and (D) then
And Not Cells(1, iCntr).Value <> "CloseBal"
And Not Cells(1, iCntr).Value <> "Totals"
如果A、B、C或D中的任何一个为false,则不执行删除

你的一些条件是

if (A) and (B) and (C) and (D) then
And Not Cells(1, iCntr).Value <> "CloseBal"
And Not Cells(1, iCntr).Value <> "Totals"
其中一个条件肯定是错误的


因此,我认为整个条款肯定是错误的,这意味着任何列都不会被删除。不是这样吗?

首先,让我们澄清一下你想要什么。据我所知,如果列不包含任何列出的标题,则您希望删除该列

“*”
PIDList
CloseBal
Totals
作为一列的标题是不合逻辑的。因此,在
IF
语句中使用
和“
会使子句为false。尝试使用
或“

此外,请反向尝试:

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

*有
意味着
不等于
,也就是说
”等于“
”。这使得你的从句反过来也起作用。

首先,让我们澄清你想要什么。据我所知,如果列不包含任何列出的标题,则您希望删除该列

“*”
PIDList
CloseBal
Totals
作为一列的标题是不合逻辑的。因此,在
IF
语句中使用
和“
会使子句为false。尝试使用
“或“

此外,请反向尝试:

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

*有
意味着
不等于
,也就是说
”等于“
”。这使得你的子句反过来也起作用。

好吧,我想你必须用两个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
如果不是像“*”这样的PIDList&单元格(1,iCntr)。值&“*”则
如果单元格(1,iCntr)。值“增量”和_
单元格(1,iCntr)。值“CloseBal”和_
单元格(1,iCntr)。值“总计”和_
单元格(1,iCntr)。值“DESC1”和_
单元格(1,iCntr)。值为“ASOFDATE”,然后
列(iCntr)。删除
如果结束
如果结束
下一个

这对我来说很有用。

好吧,我想你应该用两个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
如果不是像“*”这样的PIDList&单元格(1,iCntr)。值&“*”则
如果单元格(1,iCntr)。值“增量”和_
单元格(1,iCntr)。值“CloseBal”和_
单元格(1,iCntr)。值“总计”和_
单元格(1,iCntr)。值“DESC1”和_
单元格(1,iCntr)。值为“ASOFDATE”,然后
列(iCntr)。删除
如果结束
如果结束
下一个

这对我很有用。

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

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
Sub-foo()
暗iCntr与长iCntr相同
作为字符串的Dim val
Dim PIDList()作为变量
暗淡如长
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”、“Delta”、“CloseBal”、“Totals”、“DESC1”、“AsOfDate”)将所有值添加到此数组中
LastCol=ActiveSheet.Cells(1,ActiveSheet.Columns.Count)。End(xlToLeft)。Column
对于iCntr=LastCol至1步骤-1
val=“未找到”将变量设置为未找到
currentcol=ActiveSheet.Cells(1,iCntr).Value'获取用于比较的列标题的值
对于x=LBound(PIDList)到UBound(PIDList)
如果currentcol=PIDList(x),则“遍历数组以查看是否找到匹配项”
val=“找到”
如果结束
下一个x
如果val“找到”,则“如果未找到匹配项,则删除”
ActiveSheet.Columns(iCntr).entireclumn.Delete
val=“”为下一个循环重置变量
如果结束
下一个iCntr
端接头

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

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
Sub-foo()
暗iCntr与长iCntr相同
作为字符串的Dim val
Dim PIDList()作为变量
暗淡如长
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”、“Delta”、“CloseBal”、“Totals”、“DESC1”、“AsOfDate”)将所有值添加到此数组中
LastCol=ActiveSheet.Cells(1,ActiveSheet.Columns.Count)。End(xlToLeft)。Column
对于iCntr=LastCol至1步骤-1
val=“未找到”将变量设置为未找到
currentcol=ActiveSheet.Cells(1,iCntr).Value'获取用于比较的列标题的值
对于x=LBound(PIDList)到UBound(PIDList)
如果currentcol=PIDList(x),则“遍历数组以查看是否找到匹配项”
val=“找到”
如果结束
下一个x
如果val“找到”,则“如果未找到匹配项,则删除”
ActiveSheet.Columns(iCntr).entireclumn.Delete
val=“”为下一个循环重置变量
如果结束
下一个iCntr
端接头

我想你把它倒过来了
而不是像“*”和Cells(1,iCntr)那样的PIDList。Value&“*”
好的,我刚刚试过。它仍然删除零列。在我现在看到的场景中,应该删除10列。PIDList包含什么?这是存储值的单元格的命名区域吗?是的,是的,这就是“PIDList”。我想我一开始不会澄清这一点。我刚刚更新了我原来的帖子。我想你已经把它倒过来了
而不是像“*”&Cells(1,iCntr)。Value&“*”
好的,我刚刚试过了。它仍然删除零列。在我现在看到的场景中,应该有10列