简体   繁体   English

在VBA Excel中验证单元格

[英]Validating cells in VBA excel

I am creating a spreadsheet which will be uploaded to a database and there needs to be strict validation. 我正在创建一个电子表格,该电子表格将上传到数据库,并且需要进行严格的验证。

I need to ensure that if cell A2 has text in it, then cells C2,D2,F2,G2 must have data. 我需要确保,如果单元格A2中有文本,则单元格C2,D2,F2,G2必须具有数据。 I would like to do this with colour - if A2 has text then the other cells are highlighted red until they contain text, then the background colour changes back to white. 我想用颜色做到这一点-如果A2包含文本,则其他单元格将突出显示为红色,直到它们包含文本为止,然后背景颜色会变回白色。 (The cells listed are REQUIRED and must contain a value). (列出的单元格是必需的,并且必须包含一个值)。

There will be about 1000+ rows in this spreadsheet and I have attempted this using the data validation but it would involve me going in row by row. 该电子表格中将有大约1000多个行,而我已经尝试使用数据验证来进行此操作,但这需要我逐行进行。

Data validation does not require you to do row by row, by default Excel seems to make it so (by fixing row and column with dollar signs), but that can be easily changed. 数据验证不需要您逐行执行,默认情况下,Excel似乎做到了这一点(通过用美元符号固定行和列),但是可以轻松更改。

What you can do is create a formula in your conditional formatting for the top most row where you want this applied to, now get rid of the dollar signs in front of the row numbers in your formula and extend the range where this applies for to all the thousands of rows that you want it applied to. 您可以做的是以条件格式为要应用于此的最上面一行创建一个公式,现在摆脱公式中行号前面的美元符号,并扩展适用于所有行的范围您要应用到的数千行。

formula in your case for C2 will be: =AND(LEN($A2)>0;LEN(C2)=0) this can be applied for C2:F2000 : 在您的情况下,对于C2的公式为: =AND(LEN($A2)>0;LEN(C2)=0)可以用于C2:F2000:

适用于C2:F2000

Or what you can do is specify this formula for C1 ( =AND(LEN($A1)>0;LEN(C1)=0) ) and apply it to C:F. 或者您可以为C1指定此公式( =AND(LEN($A1)>0;LEN(C1)=0) )并将其应用于C:F。

应用于完整列的条件公式适用于C:F

No need for VBA. 无需VBA。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM