简体   繁体   English

需要Excel VBA列B和C或列D

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

相关问题 将数据从B,C,D列移到A列-Excel - Move data from columns B,C,D.. to column A - excel 我有一个 Excel 表,它有 3 列 AB C 列,我希望 D 列是 RGB(A,B,C) 的颜色 - I have an Excel sheet that has 3 columns A B C columns and I want the D column to be the color of RGB(A,B,C) Excel IF A1是否存在于B列中,则C = D - Excel IF A1 exist in column B, then C = D 如果B和C列都为空白,则Excel VBA删除整行 - Excel VBA delete entire row if both columns B and C are blank VBA Excel 2013在ActiveCell中的“ C”和“ D”列中写入,然后复制并粘贴,直到“ A”列更改 - VBA Excel 2013 Write in ActiveCells in columns “C” and “D” then copy and paste until column “A” changes VBA-然后在C列中对行进行计数,然后用方程式填充A,B和P列 - VBA - Count Rows then in Column C then fill Columns A, B and P with Equations Excel公式将A列和B列的值转移到C和D空单元格 - Excel Formula to shift value of Column A and B to C and D empty cells Excel查找列A和列B之间的差异,并将其填充到列C中 - Excel find differences between columns A and B and populate it in Column C 将所有@mentions和#hashtags从A列复制到Excel中的B和C列 - Copying all @mentions and #hashtags from column A to Columns B and C in Excel VBA Excel-从列C提取数据并放入列D - VBA Excel - Extract data from Column C and place into Column D
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM