简体   繁体   English

如何使此Excel宏循环仅通过可见的过滤数据?

[英]How can I make this Excel macro loop through only visible filtered data?

I have very little experience with VBA and I'm now stumped by what I'm trying to accomplish with a macro. 我对VBA的经验很少,现在对我要通过宏完成的工作感到困惑。 Excel 2010. Excel 2010。

I have 3 relevant columns. 我有3个相关的专栏。 B, C, and AD. B,C和AD。 (Columns 2, 3, and 30) My data is filtered down to about 30 rows, almost none of which are contiguous (about 500 rows total). (第2、3和30列)我的数据被过滤到大约30行,几乎没有连续的(总共约500行)。

I want the following to happen: 我希望发生以下情况:

A formula is entered in ONLY THE VISIBLE ROWS in column AD, which will look at the value in column B of that same row and check for that value in all of the VISIBLE CELLS in column C. It cannot look at all of the cells in column C, only the visible ones. 仅在AD列中的“可见行”中输入一个公式,该公式将查看同一行B列中的值,并在C列的所有可见单元格中检查该值。它无法查看其中的所有单元格C列,只有可见的。

If the value from column B in that row is found anywhere in the VISIBLE CELLS in column C, then "True" should be returned in column AD. 如果在C列的可见单元格中的任何位置找到该行B列的值,则应在AD列中返回“ True”。 I don't care about what is returned when the value is not found, as I will be filtering for the "True" values only. 我不在乎找不到该值时返回什么,因为我将仅过滤“ True”值。 As an added requirement, if the first 3 characters of the value in column B are "010" I need it to return a value of "True" in column AD. 作为附加要求,如果B列中值的前3个字符为“ 010”,我需要它在AD列中返回值“ True”。 I then need this formula copied down column AD for each VISIBLE row. 然后,我需要将该公式复制到每个可视行的AD列下。

Right now, I have a formula that will conduct the search in column C for the value in column B. (found on stackoverflow) 现在,我有一个公式,它将在C列中搜索B列中的值。(在stackoverflow上找到)

=NOT(ISNA(VLOOKUP(B4,C:C,1,0))))

This provides a "True" in column AD when the value from column B is found somewhere in column C. With the "010" constraint, the formula looks like this: 当在C列的某处找到B列的值时,这将在AD列中提供“ True”。使用“ 010”约束,公式如下所示:

=IF(LEFT(B4,3)="010","True",NOT(ISNA(VLOOKUP(B4,C:C,1,0))))

I am having a problem in that this looks at even the hidden (filtered out) rows. 我有一个问题,就是它甚至可以查看隐藏(过滤出的)行。 Every one of my values in column B will appear in column C at some point, so I'm only getting "True" for all my entries. 我在B列中的每个值都会在某个时候出现在C列中,因此我对所有输入都只获得“ True”。

I think there must be a better way to do this than just having a macro paste the formula down (even considering I can't get the formula to work). 我认为必须有一个更好的方法,而不是仅将宏粘贴到公式中(即使考虑到我无法使公式起作用)。 So, 2 questions: 因此,有2个问题:

  1. Is the formula the right way to go in this case, and, if so, can anyone tell me how to get it to only search the visible cells in column C? 在这种情况下,公式是正确的方法吗?如果是这样,有人可以告诉我如何仅在C列中搜索可见的单元格吗?
  2. If code is the best way (I'm guessing it is), can anyone show me an example of code that might work? 如果代码是最好的方法(我猜是这样),那么有人可以给我看一个可能有效的代码示例吗?

You're almost there. 你快到了。 On its own, COUNTIF does not have this capability, but if you throw user-defined functions (UDF functions) into the mix, you have an elegant solution. COUNTIF本身不具备此功能,但是如果将用户定义的函数(UDF函数)放入混合中,则可以找到一个不错的解决方案。

Add the following code to a module in your code-behind. 将以下代码添加到后台代码中的模块中。

Function Vis(Rin As Range) As Range
    'Returns the subset of Rin that is visible
    Dim Cell As Range
    Application.Volatile
    Set Vis = Nothing
    For Each Cell In Rin
        If Not (Cell.EntireRow.Hidden Or Cell.EntireColumn.Hidden) Then
            If Vis Is Nothing Then
                Set Vis = Cell
            Else
                Set Vis = Union(Vis, Cell)
            End If
        End If
    Next Cell

End Function


Function COUNTIFv(Rin As Range, Condition As Variant) As Long
    'Same as Excel COUNTIF worksheet function, except does not count
    'cells that are hidden
    Dim A As Range
    Dim Csum As Long
    Csum = 0

    For Each A In Vis(Rin).Areas
        Csum = Csum + WorksheetFunction.CountIf(A, Condition)
    Next A

    COUNTIFv = Csum
End Function

Now you can use the new COUNTIFv() function to do the same as count, but only include visible cells. 现在,您可以使用新的COUNTIFv()函数执行与count相同的操作,但只包含可见的单元格。 This code example was sampled from Damon Ostrander's answer to a similar question , so you will probably need to tweak it slightly. 此代码示例摘自Damon Ostrander对类似问题的回答 ,因此您可能需要稍作调整。 You can either use the COUNTIFv function in the macro itself, or modify the VLOOKUP function in a similar fashion to use the worksheet function example you have already. 您可以在宏本身中使用COUNTIFv函数,或以类似的方式修改VLOOKUP函数以使用已经存在的工作表函数示例。 Neither method is really better than the other, so either should work for you. 哪一种方法都不比另一种方法真正好,所以每种方法都应该对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM