想要根据工作表中的字符串删除列

时间:2017-01-12 08:54:05

标签: excel vba

制作了这段代码,但无法通过已定义标题的工作表循环来删除感谢人员。

Sub deleteCol()

On Error Resume Next
Dim Coldellr As Long
Dim colval As String
Dim wbCurrent As Workbook
Dim wsCurrent As Worksheet
Dim nLastCol, i As Integer
Dim LngLp As Long

Coldellr = Sheets("Coldel").Cells(Rows.Count, "A").End(xlUp).row 'Define LastRow

Set wbCurrent = ActiveWorkbook
Set wsCurrent = wbCurrent.ActiveSheet
'This next variable will get the column number of the very last column that has data in it, so we can use it in a loop later
nLastCol = wsCurrent.Cells.Find("*", LookIn:=xlValues, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column

'This loop will go through each column header and delete the column if the header contains "Percent Margin of Error"
For i = nLastCol To 1 Step -1
  For LngLp = 1 To Coldellr
  Set colval = Sheets("Coldel").Range("a" & LngLp).Value

    If InStr(1, wsCurrent.Cells(1, i).Value, colval, vbTextCompare) > 0 Then
        wsCurrent.Columns(i).Delete Shift:=xlShiftToLeft
    End If
Next i

End Sub

1 个答案:

答案 0 :(得分:0)

使用您的代码为每个工作表添加一个循环。应该工作。

timeupdate