如何按标题名称删除列

时间:2016-02-25 03:37:18

标签: vba excel-vba excel

错误类型在以下行

上不匹配

vMatch = Application.Match(vHeaders(i), Sheets(Sht).Rows(lColumn), 0)

调试时,代码显示vMatch = Empty我缺少什么?

Sub TestArray()
    Dim vHeaders() As Variant
    Dim vMatch As Variant
    Dim i As Long
    Dim Sht As Worksheet
    Dim lColumn As Long

    Set Sht = ActiveWorkbook.Sheets("Data")

    lColumn = Sht.UsedRange.Columns.Count

    vHeaders = Array("Branch", "Account#", "Route Name", "Driver Number", _
                  "Reference2", "Reference3", "Stop Number", "Phone", "Delivery Time", _
                  "Stop Close Time", "POD Contact Name", "Latitude", "Longitude", _
                  "Status", "Service", "ASN Create Date", "ASN Date", "StopID", _
                  "Load Scan", "Delivery Scan", "Exception", "Exception Time")


    For i = LBound(vHeaders) To UBound(vHeaders) Step 1
        vMatch = Application.Match(vHeaders(i), Sheets(Sht).Rows(lColumn), 0)
        If IsNumeric(vMatch) Then Sheets(Sht).Columns(vMatch).delete
    Next i

End Sub

1 个答案:

答案 0 :(得分:3)

只需替换此行

    vMatch = Application.Match(vHeaders(i), Sheets(Sht).Rows(lColumn), 0)
    If IsNumeric(vMatch) Then Sheets(Sht).Columns(vMatch).delete

使用

    vMatch = Application.Match(vHeaders(i), Sht.Rows(1), 0)
    If IsNumeric(vMatch) Then Sht.Columns(vMatch).Delete

也不需要以下代码和声明

lColumn = Sht.UsedRange.Columns.Count