excel 如果条件满足,则使用IF函数复制行,不包含空行

dm7nw8vv  于 2023-08-08  发布在  其他
关注(0)|答案(1)|浏览(114)

我对宏和复杂的Excel函数很陌生,所以我希望这个社区能帮助我。


1
最上面的表是冲销表。我在F列中添加了一个yes/no选项,该选项输入到D列中的IF函数中,以在F列显示y(是)时反转+/-。然后,在下方的“自动反转”表中,如果填充了“反转”中的列D,则填充列C中的IF函数。然后我在其他列中使用了IF函数,所有这些都引用了自动反转中的C列。如果填充了C,则其余行将填充相应的信息。

有没有一种方法可以做到这一点,而不会在自动反转表中有很多空白行?或者是整体上更精简的方式?

我使用IF函数,因为我没有得到与VB的伟大,但这种方式是产生空白行。我只想要所需的行(即。在列F中包含Y而不是N),以复制到自动反转表中。

wkftcu5l

wkftcu5l1#

下面是一个UDF,它的功能与FILTER()类似

'Filter range `rng` according one or more tests and return all
'  rows which pass all the tests.
'Example usage:
'   =myfilter(A3:K10,F3:F10="fred")             one test
'   =myfilter(A3:K10,F3:F10="fred",H3:H10="y")  two tests
Function MyFilter(rng As Range, ParamArray tests())
    Dim rw As Range, i As Long, n As Long, c As Long, rOut As Long, arr, arrout()
    Dim arrOK() As Boolean, t As Long, anyRows As Boolean
    
    arr = rng.Value  'all input data as array
    ReDim arrOK(1 To UBound(arr, 1))
    
    'Find which rows satisfy all the tests
    'Each item in `tests()` is a 2-d array of True/False values
    For i = 1 To UBound(arr, 1)
        arrOK(i) = True 'defaults to true
        For t = 0 To UBound(tests)
            If Not tests(t)(i, 1) Then
                arrOK(i) = False 'flag skip row
                Exit For         'stop testing
            End If
        Next t
        If arrOK(i) = True Then n = n + 1 'increase row count if all tests passed
    Next i
    
    'if any good rows, populate the array for output
    If n > 0 Then
        ReDim arrout(1 To n, 1 To UBound(arr, 2))
        For i = 1 To UBound(arr)
            If arrOK(i) Then  'this row passed all tests?
                rOut = rOut + 1
                For c = 1 To UBound(arr, 2)
                    arrout(rOut, c) = arr(i, c)
                Next c
            End If
        Next i
    End If
    MyFilter = IIf(n > 0, arrout, "No rows")
End Function

字符串

相关问题