简体   繁体   English

如何根据单元格内部颜色过滤Excel单元格?

[英]How to filter Excel cells based on cell interior color?

I would like to filter rows in big excel sheet - I would like to show only cells which has specific interior color (for example yellow). 我想过滤大Excel工作表中的行-我只想显示具有特定内部颜色(例如黄色)的单元格。 Is there any simple way to do it? 有什么简单的方法吗? My idea is writing a VBA function (something simillar is here : 我的想法是编写VBA函数(类似的东西在这里

*public function kolory(komorka as range)
kolory-komorka.interior.color
end function*

When I have this function, I am able to generate specific code for each color and then use it to filter it (I can even expand it by adding select case stucture which will translate this code to human readable information). 使用此功能后,我可以为每种颜色生成特定的代码,然后使用它进行过滤(我甚至可以通过添加选择案例结构来扩展它,以将该代码转换为人类可读的信息)。 Unfortunately Excel doesn't see my function (although macros are enabled) and in many situation I am not able to use code written in VB. 不幸的是,Excel没有看到我的功能(尽管启用了宏),并且在许多情况下,我无法使用VB编写的代码。

Do you mean that it doesn't show up in the Macros window? 您是说它没有出现在“宏”窗口中吗? Do you want it to show up as a worksheet function/user defined function? 您是否希望将其显示为工作表功能/用户定义功能? If you want to use it as a UDF you need to have a return type and place the code in a regular module. 如果要将其用作UDF,则需要具有返回类型并将代码放置在常规模块中。 the code would look like 代码看起来像

public function kolory(komorka as range) as integer
    dim cellColor as integer
    cellColor = komorka.interior.color
    kolory = cellColor
end function

If you are trying to use this as a stand alone routine you can't because you require a non-optional input ( komorka as range ). 如果您尝试将其作为独立的例程使用,则不能这样做,因为您需要非可选的输入( komorka as range )。 It needs to be called from some containing function/sub that can pass it the komorka value. 需要从某些包含其可以传递komorka值的函数/子程序中调用它。 Once you have something that can call your kolory function you can use it in your code. 一旦有了可以调用kolory函数的东西,就可以在代码中使用它。 The code would look the same as above. 该代码将与上面相同。

This is the correct syntax for your function: 这是函数的正确语法:

Public Function kolory(komorka As Range) As Long
    kolory = komorka.Interior.Color
End Function

ie get rid of those * and replace - with = . 即摆脱那些*并用=替换-

In your Excel sheet, to return the color of cell A1, type =kolory(A1) in some other cell. 在Excel工作表中,要返回单元格A1的颜色,请在其他某个单元格中键入=kolory(A1)

Of course, if you're just writing VBA code, you don't need function kolory since it's just a wrapper for komorka.Interior.Color ... 当然,如果您只是编写VBA代码,则不需要功能kolory因为它只是komorka.Interior.Color的包装。

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

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