VBA宏崩溃Excel
嗨,各位社区工作者 我正在运行一个宏来删除包含某个值的整行。该代码在小数据集上运行良好,但在当前数据集(约22000条记录)上,它始终会崩溃Excel(2010)。代码如下。除了将数据分割成更小的块并一次又一次地运行宏之外,我不知道该怎么办 感谢您的帮助,以下是代码:VBA宏崩溃Excel,excel,crash,excel-2010,vba,Excel,Crash,Excel 2010,Vba,嗨,各位社区工作者 我正在运行一个宏来删除包含某个值的整行。该代码在小数据集上运行良好,但在当前数据集(约22000条记录)上,它始终会崩溃Excel(2010)。代码如下。除了将数据分割成更小的块并一次又一次地运行宏之外,我不知道该怎么办 感谢您的帮助,以下是代码: Sub CleanOcc() 'Row counting Dim Firstrow As Long Dim Lastrow As Long Dim Lrow As Long Dim Lrow2 As Long With She
Sub CleanOcc()
'Row counting
Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim Lrow2 As Long
With Sheets("Occ_Prep")
'Cleans the occ_prep sheet ready for upload (Column and value can be changed)
Sheets("Occ_Prep").Activate
'Set the first and last row to loop through
Firstrow = .UsedRange.Cells(1).Row
Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
'We loop from Lastrow to Firstrow (bottom to top)
For Lrow2 = Lastrow To Firstrow Step -1
'We check the values in the A column in this example
With .Cells(Lrow2, "K")
If Not IsError(.Value) Then
If .Value = "0" Then .EntireRow.Delete
'This will delete each row with the Value "ron"
'in Column A, case sensitive.
End If
End With
Next Lrow2
End With
End Sub
同意Siddharth的意见,自动筛选是一条路要走。这应该快得多
Option Explicit
Sub delrows()
Dim ws As Worksheet
Dim LR As Long
Dim rng As Range, frng As Range
Application.ScreenUpdating = False
Set ws = Sheets("dataset") '<-- Change this to name of your worksheet
With ws
LR = .Range("A" & Rows.Count).End(xlUp).Row
.AutoFilterMode = False
Set rng = .Range("A1:C" & LR) '<-- Assuming K is the last column
rng.AutoFilter 3, "0" '<-- 11 referes to Column K
Set frng = rng.Offset(1, 0).SpecialCells(xlCellTypeVisible) '<-- Don't delete the header
frng.EntireRow.Delete
.AutoFilterMode = False
End With
Application.ScreenUpdating = True
End Sub
选项显式
子行()
将ws设置为工作表
变暗LR为长
变暗rng As范围,frng As范围
Application.ScreenUpdating=False
设置ws=Sheets(“数据集”)”同意Siddharth注释自动筛选是一个好办法。这应该快得多
Option Explicit
Sub delrows()
Dim ws As Worksheet
Dim LR As Long
Dim rng As Range, frng As Range
Application.ScreenUpdating = False
Set ws = Sheets("dataset") '<-- Change this to name of your worksheet
With ws
LR = .Range("A" & Rows.Count).End(xlUp).Row
.AutoFilterMode = False
Set rng = .Range("A1:C" & LR) '<-- Assuming K is the last column
rng.AutoFilter 3, "0" '<-- 11 referes to Column K
Set frng = rng.Offset(1, 0).SpecialCells(xlCellTypeVisible) '<-- Don't delete the header
frng.EntireRow.Delete
.AutoFilterMode = False
End With
Application.ScreenUpdating = True
End Sub
选项显式
子行()
将ws设置为工作表
变暗LR为长
变暗rng As范围,frng As范围
Application.ScreenUpdating=False
设置ws=Sheets(“dataset”)“Excel和大量数据不混合,它不是真正的数据库:\n这是查找最后一个单元格的一种非常错误的方法。另请参见此处我使用delrange
对象删除相关行。简单地修改它以适应你的需要。另一种方式。使用自动筛选在0
上筛选列K,然后删除相关行。请参见本演示如何使用过滤范围。只需修改它就可以删除您的rangeExcel,并且真正大量的数据不要混合,它并不是真正意义上的数据库:\n这是查找最后一个单元格的一种非常错误的方法。另请参见此处我使用delrange
对象删除相关行。简单地修改它以适应你的需要。另一种方式。使用自动筛选在0
上筛选列K,然后删除相关行。请参见本演示如何使用过滤范围。只需修改它即可删除您的范围