Excel 基于每行多个单元格中的值删除行

Excel 基于每行多个单元格中的值删除行,excel,vba,Excel,Vba,下面的功能代码是由本网站的答案拼凑而成的 我觉得它没有它所能表现的那么优雅 代码的目的是检查每行五个单元格的范围,如果这些单元格中没有正值,则删除该行 Sub DeleteRowtest() Dim lr As Integer ' are any of these dims redundant or unnecessary? Incorrect? Dim cval As Variant Dim ArrCt As Integer Dim val As Variant 'the initial r

下面的功能代码是由本网站的答案拼凑而成的

我觉得它没有它所能表现的那么优雅

代码的目的是检查每行五个单元格的范围,如果这些单元格中没有正值,则删除该行

Sub DeleteRowtest()
Dim lr As Integer ' are any of these dims redundant or unnecessary? Incorrect?
Dim cval As Variant
Dim ArrCt As Integer
Dim val As Variant

'the initial redim is there on purpose
' instead of ValArray as Variant then ValArray = array().
' this seems cleaner but which is better?
ReDim ValArray(0) As Integer 

Dim T As Boolean

lr = Cells(Rows.Count, 11).End(xlUp).Row
For i = lr To 2 Step -1
    ArrCt = 0

    'this loop appears to work the way I want,
    ' but am I using .cells and .value correct here?
    For Each cval In Range(Cells(i, 5), Cells(i, 10)).Cells
        ValArray(ArrCt) = cval.Value
        ArrCt = ArrCt + 1
        ReDim Preserve ValArray(ArrCt)
    Next

    'is there a cleaner way than this array and nested loop
    ' to determine the same info and act on it?
    ' (i.e. if all values of cell range are <1 delete row)
    For Each val In ValArray
        If val > 0 Then
            T = True
        End If
    Next

    If T = False Then Rows(i & ":" & i).EntireRow.Delete
    T = False
Next

'finally, are there any errors/broken logic at all here that I dont see?

Range("B2").Select
End Sub
子删除行测试()
Dim lr As Integer“这些Dim中是否有多余或不必要的?不对?
Dim cval作为变体
作为整数的Dim ArrCt
Dim-val作为变体
“最初的重拨是故意的
'而不是将ValArray作为变量,然后使用ValArray=array()。
这看起来更干净,但哪一个更好?
ReDim VALARY(0)作为整数
将T作为布尔值
lr=单元格(Rows.Count,11).结束(xlUp).行
对于i=lr至2步骤-1
ArrCt=0
“这个循环似乎按照我想要的方式工作,
'但是我在这里使用的.cells和.value正确吗?
对于范围内的每个cval(单元(i,5),单元(i,10))。单元
VALARY(ArrCt)=cval.值
ArrCt=ArrCt+1
雷迪姆阵列(ArrCt)
下一个
'有没有比这个数组和嵌套循环更干净的方法
'确定相同的信息并对其采取行动?
'(即,如果单元格范围的所有值均为0,则
T=真
如果结束
下一个
如果T=False,则行(i&“:”&i).EntireRow.Delete
T=假
下一个
最后,这里有没有我看不到的错误/逻辑错误?
范围(“B2”)。选择
端接头

我不认为将数据复制到数组并在检查值之后循环数组有任何好处。
另外,我会小心地使用
integer
类型,因为它只有-32768到32767。
Long
使用起来更安全。
引用单元格/区域时,请使用工作簿/工作表直接指向所需的位置。
例如
单元格(Rows.Count,11)。End(xlUp)。Row
指的是当前活动的工作表,可能不是预期的。

这是我的版本

Option Explicit

Sub DeleteRowtest()
    Dim LastRow As Long, i As Long
    Dim Found As Boolean
    Dim Ws As Worksheet
    Dim aCell As Range

    'turn off screen updating and sheet calculation to improve speed
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    Set Ws = ThisWorkbook.Sheets("Your sheet name") 'set reference to sheet

    With Ws 'all references starting with "." from now on will refer to sheet "Ws"
        LastRow = .Cells(.Rows.Count, 11).End(xlUp).Row 'Find last row

        For i = LastRow To 2 Step -1 'loop rows from last to 2
            Found = False 'reset variable
            For Each aCell In .Range(.Cells(i, 5), .Cells(i, 10))
                If aCell.Value > 0 Then ' this will be true also for text in cells
                'if you want to check for numeric values only you can try this
            '   If aCell.Value > 0 And IsNumeric(aCell.Value) Then
                    Found = True
                    Exit For
                End If
            Next aCell
            If Not Found Then 'no positive value found in row
                .Rows(i & ":" & i).EntireRow.Delete
            End If
        Next i
        .Range("B2").Select
    End With

    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic

End Sub
范围(单元格(i,5),单元格(i,10))。单元格是六个单元格