首页 \ 问答 \ 使用vba创建“色阶”(避免条件格式化)(Creating a “color scale” using vba (avoiding conditional formatting))

使用vba创建“色阶”(避免条件格式化)(Creating a “color scale” using vba (avoiding conditional formatting))

我正在寻找一种方法来通过VBA代码将颜色比例应用于一组单元格, 而不是通过应用某些条件格式化...我想将它们应用为静态颜色(InteriorColor)

我搜查了大量的Excel网站,谷歌和stackoverflow,并没有发现:(

对于我的情况,如果你看看下面的图片:

http://i.imgur.com/j8ov4FJ.png

你可以看到我已经给它一个色阶,在这个例子中,尽管我已经通过条件格式完成了色阶。 我想通过VBA创建色彩比例, 但它必须避免使用条件格式 ,我希望将内部颜色分配给单元格,以便颜色是静态的,这使得它们在所有移动式Excel查看器中都可见,速度更快,不会改变,如果我是删除任何数字/行。

以下是一些示例数据只需将其保存在csv中,然后在Excel中打开它即可查看Excel中的数据:P

Data 1 (Yes there are blanks),Data 2,Data 3,Data 4,Data 5,Data 6
155.7321504,144.6395913,1,-4,-9.3844,0.255813953
113.0646481,120.1609771,5,-2,-2.5874,0.088082902
126.7759917,125.3691519,2,0,-0.0004,0.107843137
,0,7,,,0.035714286
123.0716084,118.0409686,4,0,0.3236,0.118881119
132.4137536,126.5740362,3,-2,-3.8814,0.090909091
70,105.9874422,6,-1,-0.3234,0.103896104

我在python中使用以下代码,但显然我不能在VBA中使用此代码 ,下面的代码成功地将十六进制颜色分配给50个颜色的预定义数组中的数字,所以它非常准确。

def mapValues(values):
    nValues = np.asarray(values, dtype="|S8")
    mask = (nValues != '')
    maskedValues = [float(i.split('%')[0]) for i in nValues[mask]]
    colorMap = np.array(['#F8696B', '#F86E6C', '#F8736D', '#F8786E', '#F97E6F', '#F98370', '#F98871', '#FA8E72', '#FA9373', '#FA9874', '#FA9E75', '#FBA376', '#FBA877', '#FBAD78', '#FCB379', '#FCB87A', '#FCBD7B', '#FCC37C', '#FDC87D', '#FDCD7E', '#FDD37F', '#FED880', '#FEDD81', '#FEE382', '#FEE883', '#FCEB84', '#F6E984', '#F0E784', '#E9E583', '#E3E383', '#DCE182', '#D6E082', '#D0DE82', '#C9DC81', '#C3DA81', '#BDD881', '#B6D680', '#B0D580', '#AAD380', '#A3D17F', '#9DCF7F', '#96CD7E', '#90CB7E', '#8ACA7E', '#83C87D', '#7DC67D', '#77C47D', '#70C27C', '#6AC07C', '#63BE7B'])
    _, bins = np.histogram(maskedValues, 49)
    try:
        mapped = np.digitize(maskedValues, bins)
    except:
        mapped = int(0)
    nValues[mask] = colorMap[mapped - 1]
    nValues[~mask] = "#808080"
    return nValues.tolist()

任何人有任何想法或有任何人使用VBA之前完成此任务。


I'm looking for a way to apply a color scale to a set of cells via VBA code but not by applying some conditional formatting... I want to apply them as static colors (InteriorColor)

I've searched plenty of excel sites, google and stackoverflow and found nothing :(

For my situation if you look at the following picture:

http://i.imgur.com/j8ov4FJ.png

You can see I've given it a color scale, in this example though I have done the color scale via Conditional formatting. I want to create the color scale via VBA but it must avoid using conditional formatting, I want to assign interior colors to the cells so that the colors are static which makes them visible on all mobile excel viewers, faster, won't change if I was to remove any numbers/rows.

Here are some example data Just save it in a csv and open it in excel to see the data in excel :P:

Data 1 (Yes there are blanks),Data 2,Data 3,Data 4,Data 5,Data 6
155.7321504,144.6395913,1,-4,-9.3844,0.255813953
113.0646481,120.1609771,5,-2,-2.5874,0.088082902
126.7759917,125.3691519,2,0,-0.0004,0.107843137
,0,7,,,0.035714286
123.0716084,118.0409686,4,0,0.3236,0.118881119
132.4137536,126.5740362,3,-2,-3.8814,0.090909091
70,105.9874422,6,-1,-0.3234,0.103896104

I do use the following in python but obviously I can't use this code in VBA, the following code successfully assigns hex colors to the numbers from a predefined array of 50 colors so it's pretty accurate.

def mapValues(values):
    nValues = np.asarray(values, dtype="|S8")
    mask = (nValues != '')
    maskedValues = [float(i.split('%')[0]) for i in nValues[mask]]
    colorMap = np.array(['#F8696B', '#F86E6C', '#F8736D', '#F8786E', '#F97E6F', '#F98370', '#F98871', '#FA8E72', '#FA9373', '#FA9874', '#FA9E75', '#FBA376', '#FBA877', '#FBAD78', '#FCB379', '#FCB87A', '#FCBD7B', '#FCC37C', '#FDC87D', '#FDCD7E', '#FDD37F', '#FED880', '#FEDD81', '#FEE382', '#FEE883', '#FCEB84', '#F6E984', '#F0E784', '#E9E583', '#E3E383', '#DCE182', '#D6E082', '#D0DE82', '#C9DC81', '#C3DA81', '#BDD881', '#B6D680', '#B0D580', '#AAD380', '#A3D17F', '#9DCF7F', '#96CD7E', '#90CB7E', '#8ACA7E', '#83C87D', '#7DC67D', '#77C47D', '#70C27C', '#6AC07C', '#63BE7B'])
    _, bins = np.histogram(maskedValues, 49)
    try:
        mapped = np.digitize(maskedValues, bins)
    except:
        mapped = int(0)
    nValues[mask] = colorMap[mapped - 1]
    nValues[~mask] = "#808080"
    return nValues.tolist()

Anyone have any ideas or has anyone done this before with VBA.


原文:https://stackoverflow.com/questions/28217226
更新时间:2023-02-19 20:02

最满意答案

可以吗? 是。 吗? 这实际上取决于,但除非您正在编写一个全天候运行的碎片整理程序,否则很可能(1)系统将缓存大量磁盘访问,并且(2)您的消费者计算机将在硬盘驱动器之前被替换失败。 但是,在任何一种情况下,如果您不必访问磁盘,那么显然不会。 :)


Can it? Yes. Will it? It really depends, but unless you're writing a defragmenter that runs 24/7, it's likely that (1) the system will cache a lot of the disk accesses, and (2) your consumer's computer will be replaced before the hard drive ever fails. In either case, though, if you don't have to access the disk, then obviously just don't. :)

相关问答

更多
  • 迈钻 金钻系列40G硬盘。< Hard disk sentinel Hard Disk Sentinel能够检测硬盘状态、健康程度、以及性能其中包括每个硬盘的温度、S.M.A.R.T值等。本工具同时还可以测试硬盘的实时传输率以便用来做为性能测试或者硬盘是否有隐含问题的功能。< http://www.sy2012.com/Software/Catalog141/9045.html http://www.sy2012.com/Software/Catalog141/9045.html 名称:Hard D ...
  • 在1TB硬盘中,价格为100美元的速度是比太空更有价值的资源。 这不值得。 编辑: 嗯,你应该说从盘片上抓取100个字节的压缩数据会更快,然后解压缩它,然后将它发送到系统,而不是抓取800字节的未压缩数据并将其发送到系统,因为寻道时间太慢了。 这似乎是一个聪明的做法,但我敢打赌,如果权衡最终值得,硬盘制造商已经采用这种技术,而硬盘的速度是他们尽管事实。 但是谁知道,你可能正在做些什么! In the world of 1TB drives for $100 speed is a much more valu ...
  • 可以吗? 是。 会吗? 这实际上取决于,但除非您正在编写一个全天候运行的碎片整理程序,否则很可能(1)系统将缓存大量磁盘访问,并且(2)您的消费者计算机将在硬盘驱动器之前被替换失败。 但是,在任何一种情况下,如果您不必访问磁盘,那么显然不会。 :) Can it? Yes. Will it? It really depends, but unless you're writing a defragmenter that runs 24/7, it's likely that (1) the system w ...
  • 我从另一个主题得到答案。 基本上,我们正在创建Microsoft VHD(虚拟硬盘)并使用RoboCopy填充文件并运送VHD。 请参阅: 解压缩太慢,无法传输许多文件 I got the answer from a different thread. Basically, we are creating a Microsoft VHD (virtual Hard Disk) and filling in the files with RoboCopy and shipping the VHD. See: ...
  • 似乎我无法将实际和最大磁盘使用量集成到一个脚本中,因此我将它们分成两个。 对于实际的磁盘使用情况,我使用了以下脚本: Get-VM -VMName * | Select-Object VMid | Get-VHD | Select-Object Path, Size, FileSize 对于最大分配内存,我使用了以下脚本: Measure-VM -Name * | select-object -property VMName, TotalDiskAllocation It seems that I am ...
  • 根据您尝试使用Measure-VM,我假设您使用的是Hyper-V。 我在我的一个Hyper-V脚本中使用类似的东西: (Get-VM dechiro1).HardDrives | ForEach { $GetVhd = Get-VHD -Path $_.Path [pscustomobject]@{ Name = $_.Name Type = $GetVhd.VhdType ProvisionedGB = ($GetVhd.Size / 1G ...
  • 1)Windows确实将最近读取的文件缓存在内存中。 这本书的Windows内部包含了一个关于它是如何工作的极好的描述。 现代版本的Windows还使用了一种名为SuperFetch的技术,它将尝试抢先将磁盘内容提取到基于使用历史记录的内存中,并将ReadyBoost缓存到闪存驱动器,从而实现更快的随机访问。 所有这些都会提高初始运行后从磁盘访问数据的速度。 2)目录真的不会影响磁盘上的布局。 碎片整理您的驱动器将文件数据分组在一起。 Windows Vista将自动对磁盘进行碎片整理。 理想情况下,您希望 ...
  • 非WMI更容易。 您可以使用GetLogicalDriveStrings ( https://msdn.microsoft.com/en-us/library/windows/desktop/aa364975 ( v= GetLogicalDriveStrings ) GetLogicalDriveStrings )获取系统中的所有驱动器。 接下来使用GetDiskFreeSpace ( https://msdn.microsoft.com/en-us/library/windows/desktop/aa3 ...
  • 按DriveType过滤。 类型3 =本地磁盘。 gwmi win32_logicaldisk -filter "drivetype = 3" | Select Name Filter by DriveType. Type 3 = local disks. gwmi win32_logicaldisk -filter "drivetype = 3" | Select Name
  • 我发现了一个用VB.NET编写的硬盘活动监视器示例应用程序。 这很简单,所以你应该没有问题将它转换为c#。 我的想法是为“LogicalDisk”使用两个性能计数器,“磁盘读取字节数/秒”和“磁盘写入字节数/秒” ReadCounter = New PerformanceCounter("LogicalDisk", "Disk Read Bytes/sec", "_Total") WriteCounter = New PerformanceCounter("LogicalDisk", "Disk Wr ...

相关文章

更多

最新问答

更多
  • 您如何使用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)