使用vba创建“色阶”(避免条件格式化)(Creating a “color scale” using vba (avoiding conditional formatting))
我正在寻找一种方法来通过VBA代码将颜色比例应用于一组单元格, 而不是通过应用某些条件格式化...我想将它们应用为静态颜色(InteriorColor)
我搜查了大量的Excel网站,谷歌和stackoverflow,并没有发现:(
对于我的情况,如果你看看下面的图片:
你可以看到我已经给它一个色阶,在这个例子中,尽管我已经通过条件格式完成了色阶。 我想通过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:
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
最满意答案
可以吗? 是。 会吗? 这实际上取决于,但除非您正在编写一个全天候运行的碎片整理程序,否则很可能(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. :)
相关问答
更多-
Hard Disk Sentinel 测试的准确吗[2023-08-04]
迈钻 金钻系列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将自动对磁盘进行碎片整理。 理想情况下,您希望 ...
-
如何在C ++中使用WMI获取硬盘所有逻辑驱动器的可用空间?(How to get free space of all logical drives of hard disk using WMI in C++?)[2022-06-03]
非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 ... -
发出检索硬盘卷的问题(Issue retrieving hard-disk volumes)[2023-05-06]
按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 ...