首页 \ 问答 \ 如何使用VBA从Excel中的条件格式获取背景颜色(How to get the background color from a Conditional Formatting in Excel using VBA)

如何使用VBA从Excel中的条件格式获取背景颜色(How to get the background color from a Conditional Formatting in Excel using VBA)

我想在我的VBA脚本中使用Excel中的条件格式规则获取单元背景颜色。 我意识到使用Range.Interior.Color属性不具有由Excel应用的条件格式化功能导致的颜色。

我做了一些研究, 在这里发现了很长的路,它编译和运行,但我没有得到指定的颜色[我总是得到(255,255,255)]

我正在使用Excel 2016,我想知道是否有一种更简单的方法来使用某些内置VBA函数或使用任何其他Excel技巧获取此信息。


I would like to obtain the cell background color assigned using a conditional formatting rule in Excel in my VBA script. I realized that using Range.Interior.Color property doesn't have the color resulting of an applied conditional formatting feature from Excel.

I did some research and I found this long way here, it compiles and runs but I don't get the assigned color [I get always (255,255,255)]

I am using Excel 2016 and I am wondering if there is a simpler way to obtain this information using some built-in VBA function or using any other excel trick.


原文:https://stackoverflow.com/questions/45122782
更新时间:2023-07-22 22:07

最满意答案

像这样? 这隐含地将一个装饰器添加到你的方法中(如果你愿意的话,你也可以基于此做一个明确的装饰器):

class Foo(object):
    def __getattribute__(self,name):
        attr = object.__getattribute__(self, name)
        if hasattr(attr, '__call__'):
            def newfunc(*args, **kwargs):
                print('before calling %s' %attr.__name__)
                result = attr(*args, **kwargs)
                print('done calling %s' %attr.__name__)
                return result
            return newfunc
        else:
            return attr

当你现在尝试像这样的东西:

class Bar(Foo):
    def myFunc(self, data):
        print("myFunc: %s"% data)

bar = Bar()
bar.myFunc(5)

你会得到:

before calling myFunc
myFunc:  5
done calling myFunc

Something like this? This implictly adds a decorator to your method (you can also make an explicit decorator based on this if you prefer that):

class Foo(object):
    def __getattribute__(self,name):
        attr = object.__getattribute__(self, name)
        if hasattr(attr, '__call__'):
            def newfunc(*args, **kwargs):
                print('before calling %s' %attr.__name__)
                result = attr(*args, **kwargs)
                print('done calling %s' %attr.__name__)
                return result
            return newfunc
        else:
            return attr

when you now try something like:

class Bar(Foo):
    def myFunc(self, data):
        print("myFunc: %s"% data)

bar = Bar()
bar.myFunc(5)

You'll get:

before calling myFunc
myFunc:  5
done calling myFunc

相关问答

更多
  • 像这样? 这隐含地将一个装饰器添加到你的方法中(如果你愿意的话,你也可以基于此做一个明确的装饰器): class Foo(object): def __getattribute__(self,name): attr = object.__getattribute__(self, name) if hasattr(attr, '__call__'): def newfunc(*args, **kwargs): pr ...
  • 这是一个行连续字符优于打开括号的情况。 ShortName.objects.distinct() \ .filter().values() # looks better 随着方法名称越来越长,方法开始引用参数,这种风格的需求变得越来越明显: return some_collection.get_objects(locator=l5) \ .get_distinct(case_insensitive=True) \ ...
  • 只需在完成旧的计时器后创建一个新计时器。 import requests import threading def check_status(url): status = requests.get(url) if status.status_code != 200: print('faulty') threading.Timer(2.0, check_status, args=('https://www.google.com',)).start() def m ...
  • 您不能覆盖函数调用 ,但可以覆盖函数本身。 从文档 : >>> from unittest.mock import MagicMock >>> thing = ProductionClass() >>> thing.method = MagicMock(return_value=3) >>> thing.method(3, 4, 5, key='value') 3 >>> thing.method.assert_called_with(3, 4, 5, key='value') 所以在你的情况下: ope ...
  • 您可以使client私有并强制子类在Func或Action访问它。 然后你可以在逻辑之前/之后添加你: abstract class CommunicationChannel : IDisposable where Client : class, IDisposable { private Client client; protected TResult WithClient(Func f) { ...
  • 我能看到这项工作的唯一方法是让Af()不使用self.f()而是使用Af(self)代替。 更好的设计是让Af()将递归调用委托给一个单独的方法: class A(object): def __init__(self): self.foo = None def f(self): self._f_recursive() def _f_recursive(self): if not self.foo: print ...
  • 是的,Boo有IQuackFu 。 基本上,您实现了IQuackFu ,它有三种方法: QuackGet :获取属性值时调用 设置属性值时会调用QuackSet QuackInvoke :调用方法时调用 这是一个例子 。 Yes, Boo has IQuackFu. Basically, you implement IQuackFu, which has three methods: QuackGet: gets called when you get a property value QuackSet g ...
  • 你想断言它被调用了多少次,所以使用call_count并使用assert_equal (来自unittest或你正在使用的测试框架中的其他等价物)来验证它等于你期望它被调用的次数: assert_equal(mock.call.method_to_be_mocked.call_count, 2) You want to assert on how many times it was called, so use call_count and use assert_equal (from unittest ...
  • 可能解决方案1: 包装库并使用ReSharper之类的工具查找库的所有用法并替换为包装类。 您还可以利用这个机会来清理第三方库的可能糟糕的界面。 可能的方案2: 虽然TypeMock通常用作测试工具,但它允许您模拟所有内容。 因为它将自身注入代码分析器,所以你可以模拟的东西包括类的私有和静态成员。 作为奖励,任何被覆盖的方法都不需要是虚拟的,因此您可以通过这种方式拦截调用。 我的推荐 我建议你使用解决方案1.包装器很容易理解,它将为你提供一个真正改进代码的好机会。 我甚至建议您将第三方库包装为一般规则。 P ...
  • 目前的PostSharp版本无法做到这一点。 PostSharp通过在CLR加载之前转换程序集来工作。 现在,为了做到这一点,必须做两件事: 必须将组件装入CLR; 你只得到一枪,你必须在这一点上。 转换阶段完成后,您无法进行任何其他修改。 这意味着您无法在运行时修改程序集。 最新版本1.5 CTP 3 消除了这两个限制中的第一个 ,但它是第二个真正的问题。 然而,这是一个非常需要的功能 ,所以请保持眼睛剥落: 用户经常询问是否可以在运行时使用PostSharp,因此在编译时不必知道方面。 部署后更改方面确 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)