excel - VBA:从工作表中删除信息
问题描述
我想知道是否有更有效的方法来清洁我的床单。我的代码需要很长时间才能运行(我有 5000 次观察):
Dim Num_Ligne As Long
Num_Ligne = 8
While Cells(Num_Ligne, 3) <> ""
ActiveSheet.Cells(Num_Ligne, 3).Value = ""
ActiveSheet.Cells(Num_Ligne, 4).Value = ""
ActiveSheet.Cells(Num_Ligne, 5).Value = ""
ActiveSheet.Cells(Num_Ligne, 6).Value = ""
ActiveSheet.Cells(Num_Ligne, 7).Value = ""
ActiveSheet.Cells(Num_Ligne, 8).Value = ""
ActiveSheet.Cells(Num_Ligne, 9).Value = ""
ActiveSheet.Cells(Num_Ligne, 10).Value = ""
Num_Ligne = Num_Ligne + 1
Wend
谢谢您的帮助 !
解决方案
清除范围的内容
要求
- 清除范围的内容。
OP的解决方案
- 循环遍历每个单元格并测试它是否为空。然后循环遍历每一列并清除其内容。对于 5000 条记录,如果单元格为空白,它会测试 5000 次,并清除单元格的内容 40.000 次,这在我的机器上大约需要 10 秒。
解决方案
- 创建对所有这些单元格的引用并一次性清除内容。
- 第一个解决方案是评论中 BigBen 解决方案的“风味”,它被广泛使用(流行)并且基于
End
属性。这个(我的)解决方案使用Resize
('flavor')。 - 第二种解决方案是更可靠的版本,它使用
Find
仅在过滤工作表时可能会失败的方法。End
如果工作表上的最后一个单元格被占用(极不可能),如果最后一行小于指定的第一行(不太可能),并且如果存在隐藏行(不太可能),则该解决方案将另外失败。 - 第三种解决方案是对第二种解决方案的一种研究。
- 如需更多见解,请研究传奇的 Siddharth Rout对Error in find last used cell in Excel with VBA的回答。
编码
Option Explicit
Sub RangeClearContentsEnd()
Const Cols As String = "C:J"
Const FirstRow As Long = 8
With ActiveSheet.Columns(Cols)
Dim LastRow As Long
LastRow = .Columns(1).Cells(.Rows.Count).End(xlUp).Row
.Rows(FirstRow).Resize(LastRow - FirstRow + 1).ClearContents
End With
End Sub
Sub RangeClearContentsFind()
Const Cols As String = "C:J"
Const FirstRow As Long = 8
With ActiveSheet.Columns(Cols).Rows(FirstRow)
Dim cel As Range
Set cel = .Resize(.Worksheet.Rows.Count - FirstRow + 1, 1) _
.Find("*", , xlFormulas, , , xlPrevious)
If Not cel Is Nothing Then
.Resize(cel.Row - FirstRow + 1).ClearContents
End If
End With
End Sub
Sub RangeClearContentsFindStudy()
Const Cols As String = "C:J"
Const FirstRow As Long = 8
' Define the first row (range) of the range.
Dim rrg As Range: Set rrg = ActiveSheet.Columns(Cols).Rows(FirstRow)
Debug.Print "First Row of the Range : " & rrg.Address(0, 0)
' Define the range referring to the first column from
' the specified first row to the bottom-most row of the worksheet.
Dim frg As Range
Set frg = rrg.Resize(ActiveSheet.Rows.Count - FirstRow + 1, 1)
Debug.Print "First Column to the Bottom: " & frg.Address(0, 0)
' Attempt to find the last non-empty cell in it.
Dim lCell As Range
Set lCell = frg.Find("*", , xlFormulas, , , xlPrevious)
' Validate the last non-empty cell.
If Not lCell Is Nothing Then
Debug.Print "Last Non-Empty Cell: " & lCell.Address(0, 0)
' Define the range from the first specified row
' to the last non-empty cell's row.
Dim drg As Range: Set drg = rrg.Resize(lCell.Row - FirstRow + 1)
Debug.Print "Delete Range: " & drg.Address(0, 0)
' Clear its contents.
drg.ClearContents
Else
Debug.Print "Nothing cleared."
End If
End Sub
推荐阅读
- scala - 在尝试遍历消费者记录时,每个循环中的 Scala 中的错误
- kotlin - JSON 解析错误:语法错误,期望 {,实际错误,pos 0,fastjson-version 1.2.57 使用 RoyalPay SDK 时
- amazon-s3 - S3 浏览器客户端不会显示存储桶根文件夹中的对象
- android - 在 RxJava 中传递参数时顺序运行 Observables
- javascript - 使用 Nuxt 和 Vuetify 的头像选择器
- c++ - 以泛型函数为参数的遍历函数
- java - 如何在 Fire Store 中查询集合的所有文档
- visual-studio-code - 无论如何移动 Visual Studio Code 中的搜索框?
- mysql - docker mysql wordpress 端口无法连接
- python - 使用单例是答案吗?