使用IF函数复制行,如果条件满足,而且不要有空行。

huangapple go评论147阅读模式
英文:

Copy row if condition met using IF function, without blank rows

问题

我对宏和复杂的Excel函数都非常陌生,因此希望这个社区能帮助我。

使用IF函数复制行,如果条件满足,而且不要有空行。1

顶部表格是反转表格。我在列F中添加了一个是/否选项,它会传递到列D中的IF函数,以根据列F显示的y(是)来反转+/-。
然后在下面的"Auto Reversals"表格中,如果"Reversals"中的列D被填充,列C中的IF函数将被填充。
然后,我在其他列中使用了IF函数,所有这些函数都引用了"Auto Reversals"中的列C。如果C被填充,其余的行将被填充以相应的信息。

是否有办法在"Auto Reversals"表中不产生大量空白行?或者是否有更加简化的方法?

我使用IF函数是因为我在VB方面表现不佳,但是这种方式会产生空白行。我只想复制所需的行(即在列F中包含Y而不是N的行)到"Auto Reversals"表中。

英文:

I am very new to macros and complicated Excel functions in general, so I am hoping this community can help me.

使用IF函数复制行,如果条件满足,而且不要有空行。1

The top table is the Reversal Table. I have added a yes/no option in column F which feeds into an IF function in column D to reverse the +/- if column F shows y (Yes).
Then in the lower, Auto Reversals table, an IF function in column C populates if column D in Reversals is populated.
I have then used IF functions in the other columns, all referring to column C in Auto Reversals. If C is populated, the rest of the rows will be populated with the corresponding info.

Is there a way to do this without having lots of blank rows in the auto reversals table? Or a more streamlined way overall?

I am using IF functions as I didn't get on great with the VB, but this way is producing blank rows. I would like only the required rows (ie. Contain Y not N in column F) to be copied into the auto reversals table.

答案1

得分: 1

这是一个用于执行与FILTER()类似功能的自定义函数(UDF)。

'根据一个或多个条件筛选范围`rng`,并返回满足所有条件的所有行。
'示例用法:
'=myfilter(A3:K10,F3:F10="fred")             单个条件
'=myfilter(A3:K10,F3:F10="fred",H3:H10="y")  两个条件
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  '将所有输入数据转换为数组
    ReDim arrOK(1 To UBound(arr, 1))
    
    '查找满足所有条件的行
    'tests()中的每个项目都是一个True/False值的2维数组
    For i = 1 To UBound(arr, 1)
        arrOK(i) = True '默认为True
        For t = 0 To UBound(tests)
            If Not tests(t)(i, 1) Then
                arrOK(i) = False '标记跳过行
                Exit For         '停止测试
            End If
        Next t
        If arrOK(i) = True Then n = n + 1 '如果所有条件都通过,则增加行计数
    Next i
    
    '如果有满足条件的行,填充输出数组
    If n > 0 Then
        ReDim arrout(1 To n, 1 To UBound(arr, 2))
        For i = 1 To UBound(arr)
            If arrOK(i) Then  '这一行是否满足所有条件?
                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, "没有行")
End Function
英文:

Here's a UDF which sort-of does the same thing as 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

huangapple
  • 本文由 发表于 2023年7月24日 19:17:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76753930.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定