2016-02-25 77 views
1

錯誤類型不匹配上以下行如何刪除通過在頭列名

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 

回答

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