I am trying to delete all cells =0 in a column in my spreadsheet and "summon" the values which don't to the top of the column.
我试图删除电子表格中一列中的所有单元格= 0,并“召唤”不在列顶部的值。
I am currently using
我目前正在使用
Dim row_index As Integer
Dim col_index As Integer
row_index = 7
col_index = 16
Application.ScreenUpdating = False 'turns off screen updates
While Cells(row_index, col_index) <> ""
If Cells(row_index, col_index) = 0 Then
Cells(row_index, col_index).Delete
Else
row_index = row_index + 1
End If
Wend
Application.ScreenUpdating = True 'turns screen updates back on
But even with screen updating off it is very slow as the datasets are between 500-3500 points. Is there a better way to do this or any other tips to speed it up?
但即使屏幕更新,它也非常慢,因为数据集在500-3500点之间。有没有更好的方法来做到这一点或任何其他提示,以加快它?
Thanks
Edit: there are a few solutions on the web but they all seem to involve blanking cells or deleting rows. I only want to delete cells and then shift cells up.
编辑:网上有一些解决方案,但它们似乎都涉及消隐单元格或删除行。我只想删除单元格然后向上移动单元格。
4 个解决方案
#1
8
Deleting cells in a loop can really be very slow. What you could do is identify the cells that you want to delete in a loop and then delete them in one go after the loop. Try this.
删除循环中的单元格确实非常慢。您可以做的是在循环中识别要删除的单元格,然后在循环后一次删除它们。试试这个。
Option Explicit
Sub Sample()
Dim row_index As Long, lRow As Long, i As Long
Dim ws As Worksheet
Dim delRange As Range
'~~> Change this to the relevant worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
row_index = 7
Application.ScreenUpdating = False
With ws
lRow = .Range("P" & .Rows.Count).End(xlUp).Row
For i = row_index To lRow
If .Range("P" & i).Value <> "" And .Range("P" & i).Value = 0 Then
If delRange Is Nothing Then
Set delRange = .Range("P" & i)
Else
Set delRange = Union(delRange, .Range("P" & i))
End If
End If
Next
End With
If Not delRange Is Nothing Then delRange.Delete shift:=xlUp
Application.ScreenUpdating = True
End Sub
#2
2
Autofilter solution
Dim rng1 As Range
Set rng1 = Range([p7], Cells(Rows.Count, "p").End(xlUp))
ActiveSheet.AutoFilterMode = False
With rng1
.AutoFilter Field:=1, Criteria1:="0"
.Delete xlUp
End With
#3
-1
To speed things up, you probably also want to turn auto calculation off while you do the update:
为了加快速度,您可能还希望在执行更新时关闭自动计算:
Application.Calculation = xlCalculationManual
Then change it back to automatic when you are done:
完成后将其更改为自动:
Application.Calculation = xlCalculationAutomatic
#4
-2
Yes, there is:
就在这里:
Sub DoMAcro()
Dim lastRow As Integer
lastRow = Cells(1000, 16).End(xlUp).Row
Range(Cells(7, 16), Cells(lastRow, 16)).Replace What:="0", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End Sub
#1
8
Deleting cells in a loop can really be very slow. What you could do is identify the cells that you want to delete in a loop and then delete them in one go after the loop. Try this.
删除循环中的单元格确实非常慢。您可以做的是在循环中识别要删除的单元格,然后在循环后一次删除它们。试试这个。
Option Explicit
Sub Sample()
Dim row_index As Long, lRow As Long, i As Long
Dim ws As Worksheet
Dim delRange As Range
'~~> Change this to the relevant worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
row_index = 7
Application.ScreenUpdating = False
With ws
lRow = .Range("P" & .Rows.Count).End(xlUp).Row
For i = row_index To lRow
If .Range("P" & i).Value <> "" And .Range("P" & i).Value = 0 Then
If delRange Is Nothing Then
Set delRange = .Range("P" & i)
Else
Set delRange = Union(delRange, .Range("P" & i))
End If
End If
Next
End With
If Not delRange Is Nothing Then delRange.Delete shift:=xlUp
Application.ScreenUpdating = True
End Sub
#2
2
Autofilter solution
Dim rng1 As Range
Set rng1 = Range([p7], Cells(Rows.Count, "p").End(xlUp))
ActiveSheet.AutoFilterMode = False
With rng1
.AutoFilter Field:=1, Criteria1:="0"
.Delete xlUp
End With
#3
-1
To speed things up, you probably also want to turn auto calculation off while you do the update:
为了加快速度,您可能还希望在执行更新时关闭自动计算:
Application.Calculation = xlCalculationManual
Then change it back to automatic when you are done:
完成后将其更改为自动:
Application.Calculation = xlCalculationAutomatic
#4
-2
Yes, there is:
就在这里:
Sub DoMAcro()
Dim lastRow As Integer
lastRow = Cells(1000, 16).End(xlUp).Row
Range(Cells(7, 16), Cells(lastRow, 16)).Replace What:="0", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End Sub