[英]Excel VBA Columns B and C OR Column D required
I have a spreadsheet which is used enterprise-wide. 我有一个用于企业范围的电子表格。 I am trying to put in checks such that certain fields are required.
我试图检查,以便某些字段是必需的。 Specifically, columns B (last name) AND C (first name) are required, OR Column D (Organization) is required.
具体来说,必填列B(姓)和C(姓),或列D(组织)。 However, B, C, and D cannot all three be filled in. If the row has any data at all in it, B and C or D are required.
但是,B,C和D不能全部填写。如果该行中根本没有任何数据,则需要B和C或D。
My idea is to put in a button to run this macro. 我的想法是放一个按钮来运行此宏。 That I can do.
我能做的。
I've tried many things at this point. 此时,我已经尝试了很多事情。 I can include the spreadsheet in case anyone can offer any insight.
我可以包括电子表格,以防任何人可以提供任何见解。 I had a macro that worked on a test sheet, but does not work on this sheet, if that will help at all.
我有一个可以在测试表上工作的宏,但是在这个工作表上不起作用,如果有帮助的话。 Here is the macro
这是宏
Sub CheckVal2()
Dim ws As Worksheet
Dim wsCurr As Worksheet
Dim cel As Range
Dim lngLastRow As Long
Dim lngRow As Long
For Each ws In Worksheets
If Left$(ws.Name, 7) = "Current" Then
Set wsCurr = ws
Exit For
End If
Next
With wsCurr
lngLastRow = .Range("B5000").End(xlUp).Row
For lngRow = 2 To lngLastRow
For Each cel In .Range("B" & lngRow & ":E" & lngRow)
If cel = "" Then
MsgBox "First and Last Name or HCO must be populated."
Cancel = True
Exit Sub
End If
If cel <> "" Then
If .Cells(lngRow, "D") = "" Then
If .Cells(lngRow, "B") = "" Or _
.Cells(lngRow, "C") = "" Then
MsgBox "First and Last Name or HCO must be populated."
Cancel = True
Exit Sub
End If
End If
End If
Next
Next
End With
'
End Sub
Once you get past whatever is causing the error trying to access wsCurr (which I suspect is just a case of the worksheet not existing), you should modify your code as follows: 一旦克服了导致尝试访问wsCurr的错误的原因(我怀疑这只是工作表的一种情况),您应该按以下方式修改代码:
With wsCurr
lngLastRow = .Range("E5000").End(xlUp).Row
For lngRow = 2 To lngLastRow
'First check whether first/last name has been consistently advised
If (.Cells(lngRow, 2) = "") <> _
(.Cells(lngRow, 3) = "") Then
MsgBox "Row " & lngRow & " - First Name and Last Name must both be advised or both be blank"
Cancel = True ' I assume this is a global variable?
Exit Sub
End If
'Now check that last name has not been advised if HCO has been, and vice-versa
If (.Cells(lngRow, 2) = "") = _
(.Cells(lngRow, 4) = "") Then
MsgBox "Row " & lngRow & " - First and Last Name, or HCO, must be populated but not both."
Cancel = True
Exit Sub
End If
Next
End With
This will get around the existing problem with your tests, which (as far as I can tell) aren't allowing for the case where all three columns have been advised. 这将解决您的测试存在的问题,(就我所知)这不允许在建议使用所有三列的情况下进行。
I also changed the column on which lngLastRow was being set because, if it is set based on column B and the last row(s) of your data only contained values in column C and/or D, those final row(s) would not be being tested. 我还更改了设置lngLastRow的列,因为如果它是基于B列设置的,并且数据的最后一行仅包含C列和/或D列中的值,那么这些最后一行将不会被测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.