我有一个庞大的电子表格,我需要从中选择某些数据行,其余的可以删除。我想要选择的所有行都以RC开头,后跟6个数字。数字可以不同,因此它们看起来像RC125468和RC212548。电子表格中有空白行,但我不知道这是否会产生影响。要覆盖电子表格中的所有行,我必须循环大约30000。
这是我的代码(它很糟糕,它主要来自我在谷歌上发现的我认为会起作用的东西):
Sub Macro1()
Dim x As Integer
Dim rng As Range
Set rng = Range("A2:A35000")
Set x = x + 1
Do Until x = 30000
If Range(x, 1).Value <> ("RC??????") Then
Delete.Row (x)
End If
Loop
End Sub
我已经尝试过几种不同的东西,但是不断遇到不同的错误,并且无法将某些东西放在一起甚至可以运行,更不用说选择我需要的信息了。任何帮助将不胜感激。
答案 0 :(得分:1)
试试这个:
Dim i as Long
For i = 30000 to 1 step -1
If Not ActiveSheet.Range("A" & i) Like "*RC*" Then
ActiveSheet.Range("A" & i).EntireRow.Delete
End If
Next I
答案 1 :(得分:1)
试试这个:
Sub Macro2()
Dim x As Long
For x = 30000 To 1 Step -1
If Left(Cells(x, 1).Value, 2) <> "RC" Or Not IsNumeric(Right(Cells(x,1).Value),6) Then
Rows(x).Delete
End If
Next x
End Sub
如果单元格不以RC开头,或者如果单元格不是以数字开头,如果6个结束数字不是数字,则删除该行。
答案 2 :(得分:0)
试试这个...
Sub Macro1()
Application.ScreenUpdating = False
Dim x As Integer
Dim rng As Range
Set rng = Range("A2:A35000")
For x = 1 To 30
If Not Cells(x, 1).Value Like "RC*" Then
Rows(x).EntireRow.Delete
x = x - 1
End If
Next x
Application.ScreenUpdating = True
End Sub
这将删除第一个单元格未启动RC的任何行
希望有所帮助
答案 3 :(得分:0)
我的第一次尝试是使用高级过滤器。使用通过VBA调用的Advanced Filter
时,可以将过滤后的范围复制到其他工作表(与在Excel中执行此操作不同)。所以你可以试试:
Option Explicit
Sub SpecialFilter()
Dim myTable As Range
Dim myCriteria As Range
Dim vResults As Variant
Dim WS As Worksheet, wsRes As Worksheet, rRes As Range
Dim LastRow As Long, LastCol As Long
Set wsRes = Worksheets("sheet1")
Set rRes = wsRes.Cells(1, 1)
Set WS = Worksheets("sheet2")
With WS
LastRow = .Cells.Find(what:="*", after:=.Cells(1, 1), _
LookIn:=xlValues, searchorder:=xlByRows, _
searchdirection:=xlPrevious).Row
LastCol = .Cells.Find(what:="*", after:=.Cells(1, 1), _
LookIn:=xlValues, searchorder:=xlByColumns, _
searchdirection:=xlPrevious).Column
Set myTable = .Range(.Cells(1, 1), .Cells(LastRow, LastCol))
Set myCriteria = .Range(.Cells(1, LastCol + 5), .Cells(2, LastCol + 5))
End With
myCriteria(2).Formula = "=AND(LEFT(" & myTable(2, 1).Address(rowabsolute:=False) & _
",2)=""RC"", ISNUMBER(-MID(" & myTable(2, 1).Address(rowabsolute:=False) & _
",3,6)),LEN(" & myTable(2, 1).Address(rowabsolute:=False) & ")=8)"
wsRes.Cells.Clear
myTable.AdvancedFilter _
Action:=xlFilterCopy, _
criteriarange:=myCriteria, _
copytorange:=rRes
myCriteria.Clear
End Sub
但是,使用循环遍历每一行的方法,您需要进行一些更改
"RC######"
您的代码包含修改:
Option Explicit
Sub Macro1()
Dim x As Long
Dim LastRow As Long
'Determine last row in column A
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
'Cycle from last row to first
'Assume row 1 is a column header not to be deleted
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For x = LastRow To 2 Step -1
If Not Cells(x, 1) Like "RC######" Then Rows(x).Delete
Next x
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
尝试两种方法,看看哪种效果更好。
其他说明:
Integer
数据类型,除非您想为其范围之外的数字抛出溢出错误。currentregion
属性,但如果是空行,则不可靠。我展示的方法应该都是可靠的。答案 4 :(得分:0)
构建一个范围,然后一次删除,而不是一次删除一行:
Sub DeleteByCriteria()
Dim MyRange As Range, Cell As Object
For Each Cell In Range("A2:A35000")
If Left(Cell.Value, 2) = "RC" Then
If MyRange Is Nothing Then
Set MyRange = Range(Cell.Address)
Else
Set MyRange = Union(MyRange, Range(Cell.Address))
End If
End If
Next
MyRange.EntireRow.Delete
End Sub
如果你真的想一次一个地去,那么反过来做类似的事情:
For X = 30000 to 1 step -1
这样,每次删除行时,您都不需要从X减1。