Excel 宏可以工作,但速度太慢

Excel 宏可以工作,但速度太慢,excel,vba,Excel,Vba,我发现这个宏可以完成这项工作,但它需要很长时间才能执行。我想把它移到第4行到第350行,而不是所有的行。另外,我希望它不询问哪张表,而是执行表名数据。宏用于删除空行 Sub DeleteBlankRows() Dim wks As Worksheet Dim lngLastRow As Long, lngLastCol As Long, lngIdx As Long, _ lngColCounter As Long Dim blnAllBlank As B

我发现这个宏可以完成这项工作,但它需要很长时间才能执行。我想把它移到第4行到第350行,而不是所有的行。另外,我希望它不询问哪张表,而是执行表名数据。宏用于删除空行

Sub DeleteBlankRows()
    Dim wks As Worksheet
    Dim lngLastRow As Long, lngLastCol As Long, lngIdx As Long, _
        lngColCounter As Long
    Dim blnAllBlank As Boolean
    Dim UserInputSheet As String
    
    UserInputSheet = Application.InputBox("Enter the name of the sheet which you wish to remove empty rows from")
    
    Set wks = Worksheets(UserInputSheet)
    
    With wks
        'Now that our sheet is defined, we'll find the last row and last column
        lngLastRow = .Cells.Find(What:="*", LookIn:=xlFormulas, _
                                 SearchOrder:=xlByRows, _
                                 SearchDirection:=xlPrevious).Row
        lngLastCol = .Cells.Find(What:="*", LookIn:=xlFormulas, _
                                 SearchOrder:=xlByColumns, _
                                 SearchDirection:=xlPrevious).Column
    
        'Since we need to delete rows, we start from the bottom and move up
        For lngIdx = lngLastRow To 1 Step -1
    
            'Start by setting a flag to immediately stop checking
            'if a cell is NOT blank and initializing the column counter
            blnAllBlank = True
            lngColCounter = 2
    
            'Check cells from left to right while the flag is True
            'and the we are within the farthest-right column
            While blnAllBlank And lngColCounter <= lngLastCol
    
                'If the cell is NOT blank, trip the flag and exit the loop
                If .Cells(lngIdx, lngColCounter) <> "" Then
                    blnAllBlank = False
                Else
                    lngColCounter = lngColCounter + 1
                End If
    
            Wend
    
            'Delete the row if the blnBlank variable is True
            If blnAllBlank Then
                .Rows(lngIdx).Delete
            End If
    
        Next lngIdx
    End With
    
    MsgBox "Blank rows have been deleted."

End Sub
子删除blankrows()
将工作作为工作表
暗淡的lngLastRow和长的lngLastCol和长的lngIdx_
lngColCounter尽可能长
将空白设置为布尔值
Dim UserInputSheet作为字符串
UserInputSheet=Application.InputBox(“输入要从中删除空行的工作表的名称”)
Set wks=工作表(UserInputSheet)
有工作
'现在我们的工作表已经定义,我们将找到最后一行和最后一列
lngLastRow=.Cells.Find(What:=“*”,LookIn:=xlFormulas_
搜索顺序:=xlByRows_
搜索方向:=xlPrevious).行
lngLastCol=.Cells.Find(What:=“*”,LookIn:=xlFormulas_
SearchOrder:=xlByColumns_
SearchDirection:=xlPrevious).列
'因为我们需要删除行,所以我们从底部开始向上移动
对于LNGIX=lngLastRow到1步骤-1
'首先设置一个标志以立即停止检查
'如果单元格不为空并初始化列计数器
blnAllBlank=True
lngColCounter=2
'标记为真时,从左到右检查单元格
“我们就在最右边的一列

当blnAllBlank和LNGCOLTER为空时,如果该行中的所有单元格都为空,则似乎要删除该行

Dim rng As Range
Dim i As Long
Dim MyRow As Range, DeleteRange As Range
Dim MiF As WorksheetFunction

Set MiF = WorksheetFunction

Set rng = Range("C7:M34") 'define range here

For i = 1 To rng.Rows.Count
    If MiF.CountA(rng.Rows(i)) = 0 Then
        If DeleteRange Is Nothing Then
            Set DeleteRange = rng.Rows(i) 'first time, we can't use Union
        Else
            'we unite all complete blank rows into one final "big" range
            Set DeleteRange = Union(DeleteRange, rng.Rows(i))
        End If
    End If
Next i

'we delete if something is found
If Not DeleteRange Is Nothing Then DeleteRange.EntireRow.Delete

'clear variables
Set DeleteRange = Nothing
Set rng = Nothing
Set MiF = Nothing
实际代码检查datarange中每行和每列的每个单元格

您可以通过检查整行是否为空来提高速度。例如:

黄色单元格是空白单元格 红细胞是完整的空白行(该行中的所有细胞都是空白)

此代码将删除红色行,这意味着它将删除整行,但仅当所有单元格都为空时

Dim rng As Range
Dim i As Long
Dim MyRow As Range, DeleteRange As Range
Dim MiF As WorksheetFunction

Set MiF = WorksheetFunction

Set rng = Range("C7:M34") 'define range here

For i = 1 To rng.Rows.Count
    If MiF.CountA(rng.Rows(i)) = 0 Then
        If DeleteRange Is Nothing Then
            Set DeleteRange = rng.Rows(i) 'first time, we can't use Union
        Else
            'we unite all complete blank rows into one final "big" range
            Set DeleteRange = Union(DeleteRange, rng.Rows(i))
        End If
    End If
Next i

'we delete if something is found
If Not DeleteRange Is Nothing Then DeleteRange.EntireRow.Delete

'clear variables
Set DeleteRange = Nothing
Set rng = Nothing
Set MiF = Nothing
执行代码后,输出如下所示


在这个循环中,LNGIX=lngLastRow的
到第1步-1
lngLastRow
定义为350,将
1
定义为
4
,因此它从
350
运行到
4
,而不是
设置工作表(UserInputSheet)
写下工作表名
设置工作表=工作表(“Sheet1”)
您希望在上执行它。如果您只想删除空行,为什么不对工作表进行排序?这将将空白行移到底部,从而“删除”它们。或者
Set wks=ActiveSheet
@Andreas,因为排序将对数据进行排序,如果这不是他想要的,则会把一切都搞糟。排序数据并不是删除完全不同的空白。在不了解任何工作表或数据的情况下,排序似乎是完成工作的更简单方法。我更愿意让OP来设置数据的OK/Not OK边界。我尝试了PEH解决方案,但什么都不做。它完成了工作,部分空白行仍然存在!你说的部分是什么意思?它可以做前3张桌子,但不能做下2张。嗨,我找到了原因,它工作了!谢谢