代码之家  ›  专栏  ›  技术社区  ›  Aaron Parisi

为什么.autoFill会导致Excel崩溃?

  •  1
  • Aaron Parisi  · 技术社区  · 5 年前

    我有一个电子表格,这里有一小段:

    enter image description here

    理想情况下,当我向colc添加一个日期时,不同的列会自动将日期填充到最后一行的截止日期。这是我的代码(其中也包含一些排序内容,工作正常);在我定义 lastDrag 我认为问题在于:

    Private Sub Worksheet_Change(ByVal Target As Range)
    'On Error Resume Next
    
    Dim firstRow As Long
    Dim insRow As Long
    Dim lastRow As Long
    
    If Not Intersect(Target, Range("A:AC")) Is Nothing Then
    
        With ActiveWorkbook.ActiveSheet
    
            lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    
            .Sort.SortFields.Clear
    
            .Sort.SortFields.Add(Range("AC1:AC" & lastRow), _
                xlSortOnCellColor, xlDescending, , xlSortNormal).SortOnValue.Color = RGB(191, 191, 191)
    
            ' ^^ sorts the "gray" (closed) exchanges at the bottom)
    
            .Sort.SortFields.Add Key:=.Range("AC1:AC" & lastRow), _
                SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    
            ' ^^ sorts closed files by file close date
    
            .Sort.SortFields.Add Key:=.Range("C1:C" & lastRow), _
                SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    
            ' ^^ sorts open files by RQ close date
            ' THIS IS WHERE CONDITIONS SHOULD BE
            ' IF no id has been entered, sort by...
            ' IF id has been entered, sort by...
    
            .Sort.SortFields.Add(Range("K1:K" & lastRow), _
                SortOn:=xlSortOnCellColor, Order:=xlDescending, DataOption:=xlSortNormal).SortOnValue.Color = xlNone
    
            ' ^^ makes sure that the non-colored rows are sorted??
    
            With .Sort
                .SetRange Range("A1:AC" & lastRow)
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
    
            lastDrag = .Cells(.Rows.Count, "C").End(xlUp).Row
    
            Range("D2").Select
            Selection.AutoFill Destination:=Range("D2:D" & lastDrag), Type:=xlFillDefault
    '        ^^ this seems to work but it loops forever...
    
    
        End With
    
    End If
    
    End Sub
    

    目前,excel似乎一直在自动填充,直到崩溃。为什么?

    有没有办法让它一次性自动填充列d、e、h、j等(即一堆不相邻的列)?我有些东西像 Range("D2,E2,H2..." & lastDrag)...

    2 回复  |  直到 5 年前
        1
  •  1
  •   Michal    5 年前

    想想你的代码在做什么——一旦在A:AC列中检测到更改,你的代码就会被触发去做一些事情。当它做某件事时,它会在a:ac列中做,创建一个连续的循环,这个循环最终会崩溃。检测到工作表事件后,在开始数据操作之前,必须告诉Excel停止检测新事件,直到代码完成。

    你需要添加 Application.EnableEvents = False 紧接着 IF 声明并再次打开 Application.EnableEvents = True 就在你离开潜艇之前。

        2
  •  2
  •   Mathieu Guindon    5 年前
    Private Sub Worksheet_Change(ByVal Target As Range)
    

    每次工作表更改时都会触发此工作表事件不管是用户触发的或者你自己的密码。你需要设置 EnableEvents False 防止再次进入,然后返回 True 完成后-无论是否引发错误:

    Private Sub Worksheet_Change(ByVal Target As Range)
        On Error GoTo ErrHandler
        Application.EnableEvents = False
    
        '...code...
    
    CleanExit:
        Application.EnableEvents = True
        Exit Sub
    ErrHandler:
        Stop 'debug me
        Resume CleanExit
    End Sub
    

    当您从另一个过程调用一个过程时,新的过程是 上到 调用堆栈 当程序退出时,它会得到 被逮捕的 /从调用堆栈中移除通常这不是问题,但如果事情变得递归和失控,最终调用堆栈无法再深入,vba运行时将崩溃。在这种情况下,将宿主应用程序(excel)与它一起取下。