在数据不存在的情况下,VBA代码导致Excel 2013无法响应(无借记数据)

时间:2016-03-14 14:32:30

标签: excel-vba excel-2013 vba excel

我想首先说明这是我第一次尝试在Stack Overflow上提问。我想立即跟进说明我是VBA的初学者并且只能根据我在几周内阅读的材料将以下代码拼凑在一起。

我的过程(理想情况下)涉及3个步骤:

  1. 数据从文本文件转储到电子表格中。

  2. 通过运行宏来格式化和清理所述数据。

  3. 然后使用宏将格式化和清理后的数据复制并粘贴到基于数据类别的桶(其他适当命名的电子表格)中。

  4. 第3步是我的问题所在。运行下面的代码后,Excel停止运行。我试图评论出5行代码,但我仍然遇到了一堵砖墙。

    代码:

    Sub EG_LFP()
    
    'clearing tabs and inserting headers
    
    Sheets("EG_Load").Select
    Cells.ClearContents
    
    Sheets("EGPaid").Select
    Cells.ClearContents
    bankreconheaders
    
    Sheets("EGUnpaid").Select
    Cells.ClearContents
    bankreconheaders
    
    Sheets("EGDebits").Select
    Cells.ClearContents
    bankreconheaders
    
    Sheets("EGCredits").Select
    Cells.ClearContents
    bankreconheaders
    
    
    
    Sheets("EGImport").Activate
    erow = Sheets("EGImport").Cells(Rows.Count, 6).End(xlUp).Row
    Range("B1", Cells(erow, "H")).Select
    Selection.Copy
    Sheets("EG_Load").Select
    Range("B1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    Sheets("EGImport").Select
    Range("P:P").Select
    Selection.Copy
    Sheets("EG_Load").Select
    Range("P1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    
    erows = Sheets("EG_Load").Cells(Rows.Count, 6).End(xlUp).Row
        Range("A1").Select
        Range("A1").Value = "1"
        Range("A2").Select
        Range("A2").Value = "2"
        Range("A1:A2").Select
    
    Selection.AutoFill Destination:=Range("A1", Cells(erows, 1)), Type:=xlFillDefault
    
    Sheets("EGImport").Select
    Range("I1", Cells(erows, "O")).Select
    Selection.Copy
    Sheets("EG_Load").Select
    Cells(erows + 1, "b").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    Sheets("EGImport").Select
    Range("P1", Cells(erows, "P")).Select
    Selection.Copy
    Sheets("EG_Load").Select
    Cells(erows + 1, "P").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    
    erows = Sheets("EG_Load").Cells(Rows.Count, 6).End(xlUp).Row
        Range("A1").Select
        ActiveCell.FormulaR1C1 = "1"
        Range("A2").Select
        ActiveCell.FormulaR1C1 = "2"
        Range("A1:A2").Select
        Selection.AutoFill Destination:=Range("A1", Cells(erows, 1)), Type:=xlFillDefault
    
    
    Cells.Select
    
    Selection.Replace What:="  /  /  ", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    
    
    Columns("A:P").Select
    Selection.AutoFilter
        'ActiveWorkbook.Worksheets("EG_Load").AutoFilter.Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("EG_Load").AutoFilter.Sort.SortFields.Add Key:= _
            Range("E:E"), SortOn:=xlSortOnValues, Order:=xlDescending, _
            DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("EG_Load").AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
    '        .Apply
        End With
    
    ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A:$P"), , xlYes).Name = _
            "Table4"
        Columns("A:P").Select
        ActiveSheet.ListObjects("Table4").Range.AutoFilter Field:=5, Criteria1:="="
        Range("Table4").Select
        'Range("B74429").Activate
        'Selection.EntireRow.Delete
        Range("Table4[[#Headers],[Column1]]").Select
        ActiveWorkbook.Worksheets("EG_Load").ListObjects("Table4").Sort.SortFields. _
            Clear
        ActiveSheet.ShowAllData
        ActiveWorkbook.Worksheets("EG_Load").ListObjects("Table4").Sort.SortFields. _
            Clear
        ActiveWorkbook.Worksheets("EG_Load").ListObjects("Table4").Sort.SortFields.Add _
            Key:=Range("Table4[[#All],[1]]"), SortOn:=xlSortOnValues, Order:= _
            xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("EG_Load").ListObjects("Table4").Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        ActiveSheet.ListObjects("Table4").Range.AutoFilter Field:=16
        ActiveWorkbook.Worksheets("EG_Load").ListObjects("Table4").Sort.SortFields. _
            Clear
        ActiveWorkbook.Worksheets("EG_Load").ListObjects("Table4").Sort.SortFields.Add _
            Key:=Range("Table4[[#All],[Category]]"), SortOn:=xlSortOnValues, Order:= _
            xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("EG_Load").ListObjects("Table4").Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        ActiveSheet.ListObjects("Table4").Range.AutoFilter Field:=16, Criteria1:= _
            "Consolidated"
        Range("Table4").Select
        'Range("M14").Activate
        'Selection.EntireRow.Delete
        Range("Table4[[#Headers],[Column5]]").Select
        ActiveSheet.ShowAllData
        ActiveWorkbook.Worksheets("EG_Load").ListObjects("Table4").Sort.SortFields. _
            Clear
        ActiveWorkbook.Worksheets("EG_Load").ListObjects("Table4").Sort.SortFields.Add _
            Key:=Range("Table4[[#All],[1]]"), SortOn:=xlSortOnValues, Order:= _
            xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("EG_Load").ListObjects("Table4").Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        ActiveWorkbook.Worksheets("EG_Load").ListObjects("Table4").Sort.SortFields. _
            Clear
        ActiveWorkbook.Worksheets("EG_Load").ListObjects("Table4").Sort.SortFields.Add _
            Key:=Range("Table4[[#All],[Category]]"), SortOn:=xlSortOnValues, Order:= _
            xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("EG_Load").ListObjects("Table4").Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    
    'Credits
    
    ActiveSheet.ListObjects("Table4").Range.AutoFilter Field:=16, Criteria1:= _"Credits"
        Range("Table4").Select
        Selection.Copy
        Sheets("EGCredits").Select
        Range("A2").Select
        ActiveSheet.Paste
    
    'Debits
    
    Sheets("EG_Load").Select
    ActiveSheet.ListObjects("Table4").Range.AutoFilter Field:=16, Criteria1:= _"Debits"
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("EGDebits").Select
        Range("A2").Select
        On Error GoTo skipdebits
        ActiveSheet.Paste
    skipdebits:
        ActiveSheet.ShowAllData
    
    'Paid
    ActiveSheet.Paste
    Sheets("EG_Load").Select
    ActiveSheet.ListObjects("Table4").Range.AutoFilter Field:=16, Criteria1:= _"Paid Only"
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("EGPaid").Select
        Range("A2").Select
        ActiveSheet.Paste
    
    'Unpaid
    Sheets("EG_Load").Select
    ActiveSheet.ListObjects("Table4").Range.AutoFilter Field:=16, Criteria1:= _"Unpaid Only"
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("EGUnpaid").Select
        Range("A2").Select
        ActiveSheet.Paste
    
    End Sub
    

0 个答案:

没有答案