[英]VBA to check for blank cells in columns based on value in another column
Given 给定
O 1 2 3 A
A 4 5 6 B
B 7 8 9 D
O 3
C 15
T 18
I'm looking for VBA code to validate that when column A contains a value that the remaining columns also contain values and when it doesn't contain a value, that columns 2 & 5 also contain values but 3 & 4 don't. 我正在寻找VBA代码来验证,当A列包含一个值,其余列也包含值,并且当它不包含值时,第2列和5列也包含值,而3列和4列不包含值。
I've simplified the example, in a real sheet there will be many more columns and rows to check. 我简化了示例,在实际的工作表中将有更多的列和行要检查。
I've considered COUNTIF and INDEX/MATCH and array forumlas but from my understanding these all work on single columns at a time. 我曾经考虑过COUNTIF和INDEX / MATCH以及数组论坛,但是据我了解,它们一次都可以在单个列上运行。
I want to do something like WHEN A1:An<>"" THEN COUNTBLANK(B:E) ELSE COUNTA (C:D) 我想做类似WHEN A1:An <>“” THEN COUNTBLANK(B:E)ELSE COUNTA(C:D)的事情
Is the best way to use autofilter using blanks in A and then countblank and then a second autofilter for values in A. 这是使用自动筛选的最佳方法,先在A中使用空格,再进行计数空白,然后再对A中的值进行第二次自动筛选。
Thanks 谢谢
You can do it with a couple of nested IF formulae as follows: 您可以使用几个嵌套的IF公式来做到这一点,如下所示:
=IF(A1<>"",
"A not empty, "&IF(COUNTBLANK(B1:E1)=0,
"B:E not blank",
"B:E have blanks"),
"A blank, "&IF(AND(COUNTBLANK(B1)+COUNTBLANK(E1)=0,
COUNTBLANK(C1)+COUNTBLANK(D1)=2),
"Columns 2&5 have values and Columns 3&4 don't",
"but condition not met"))
The reason for going down the VBA route is that I want a generic reusable function as opposed to a formula I copy between cells and sheets changing the columns etc along the way ending up with a lot of duplicate code. 之所以选择VBA路由,是因为我想要一个通用的可重用函数,而不是像我在单元格和工作表之间复制的公式那样更改列等,从而导致大量重复代码。
So something that takes a column to test and a value to test it with. 因此,需要用一列进行测试,并使用一个值对其进行测试。 Third parameter would be a range of columns to validate, and the fourth parameter the validation.
第三个参数是要验证的列范围,第四个参数是要验证的列。
I don't want any solution to have the columns hard coded and I don't want intermediate totals at the end of rows. 我不希望任何解决方案对列进行硬编码,也不希望在行尾使用中间总数。 This is fairly easily achieved in Excel itself...
这在Excel本身中很容易实现。
The reason for trying to use countblank is that I can apply it to a range. 尝试使用countblank的原因是我可以将其应用于范围。
After a lot of searching I discovered this (the columns don't match the original example) 经过大量搜索后,我发现了这一点(列与原始示例不匹配)
=SUMPRODUCT((A2:A19<>"")*(B2:D19=""))
=SUMPRODUCT((A2:A19="")*(D2:D19=""))
=SUMPRODUCT((A2:A19="")*(B2:C19<>""))
Nice huh? 很好吧? I just need to convert it into VBA now.
我现在只需要将其转换为VBA。
Thanks 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.