简体   繁体   English

VBA根据另一列中的值检查列中的空白单元格

[英]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.

相关问题 根据另一列中的值为列的空白单元格着色 - Coloring blank cells of a column based on value in another column VBA代码可根据条件复制特定列中的单元格并将其粘贴(值)到另一张工作表中特定列中的单元格 - VBA code to copy cells in specific column and paste (value) it to cells in specific column on another Sheet based on conditions 根据另一列中单元格的值突出显示单元格 - Highlight cells based on the value of cells in another column 根据另一列合并Vba中的单元格 - Merge cells in Vba based on another column VBA尝试检查列中的值以及是否将另一个单元格值复制到新列 - VBA Trying to check for a value in column and if there copy another cells value to a new column VBA检查列中的空单元格和打印值 - VBA check for empty cells in column and print value 使用VBA根据另一个单元格的值合并单元格 - merge cells based on value of another cell with VBA 根据另一列中单元格的值有条件地格式化多列中的所有单元格 - Conditional Formatting all cells in multiple columns based of the value of the cells in another column 仅当另一列中的单元格不为空白时,VBA颜色才会填充区域中的单元格 - VBA color fill cell in a range only if cells in another column are not blank 将列的单元格更改为基于其他列单元格的表? - Changing a column's cells to a table based on another columns cells?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM