为什么我的数组不包含预先存在的下拉值?

时间:2018-11-19 15:54:47

标签: excel vba excel-vba

我正在尝试剪切主文件。 P和R列的某些行中有数据,有些则没有。我想告诉VBA,如果该行中有数据,请不要创建下拉列表,但是如果没有数据,请为管理器创建一个下拉选项。

如何更改我的数据验证模块,使其显示为if there is data already in there, ignore creating a drop down, but create a drop down option for any blank cells in those two columns

这是我的原始模块:

Sub DataValidation()

    lastrow = ActiveWorkbook.Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row

    With Range("P2:P" & lastrow).Validation
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, _
                Formula1:="Yes - Regularly Works Eligible Shift,No - Does Not Regularly Work Eligible Shift"
        .IgnoreBlank = True
        .InCellDropdown = True
    End With

    With Range("R2:R" & lastrow).Validation
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                Operator:=xlBetween, Formula1:="8%,10%,12%,15%"
        .IgnoreBlank = True
        .InCellDropdown = True
    End With

End Sub

2 个答案:

答案 0 :(得分:2)

尝试如下代码将“ only ”仅定位到这些空白单元格:

Sub DataValidation()

    lastrow = ActiveWorkbook.Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row

    With Range("P2:P" & lastrow).SpecialCells(xlCellTypeBlanks).Validation
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
        Operator:=xlBetween, Formula1:="Yes - Regularly Works Eligible Shift,No - Does Not Regularly Work Eligible Shift"
        .IgnoreBlank = True
        .InCellDropdown = True
    End With

    With Range("R2:R" & lastrow).SpecialCells(xlCellTypeBlanks).Validation
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
        Operator:=xlBetween, Formula1:="8%,10%,12%,15%"
        .IgnoreBlank = True
        .InCellDropdown = True
    End With

End Sub

编辑:有一个错误要陷阱。如果整个Range("P2:P" & lastRow)为空,则代码将返回错误。这是因为.SpecialCells方法调用将返回Range对象,但至少要包含1个单元格。

一种解决方法是使用错误处理,并将范围存储到变量中。如果所有单元格都是空白,则它们都需要验证。否则,仅验证空白的单元格(如果您 truly 从未预见到这种情况,则可以跳过此步骤)。下面的代码:

Sub DataValidation()
    Dim rng As Range

    lastrow = ActiveWorkbook.Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row

    On Error Resume Next
    Set rng = Range("P2:P" & lastrow).SpecialCells(xlCellTypeBlanks)
    On Error GoTo 0

    If rng Is Nothing Then
        With Range("P2:P" & lastrow).Validation
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
            Operator:=xlBetween, Formula1:="Yes - Regularly Works Eligible Shift,No - Does Not Regularly Work Eligible Shift"
            .IgnoreBlank = True
            .InCellDropdown = True
        End With
    Else
        With rng.Validation
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
            Operator:=xlBetween, Formula1:="Yes - Regularly Works Eligible Shift,No - Does Not Regularly Work Eligible Shift"
            .IgnoreBlank = True
            .InCellDropdown = True
        End With
    End If

    'Reset to reuse with the R column
    Set rng = Nothing

    On Error Resume Next
    Set rng = Range("R2:R" & lastrow).SpecialCells(xlCellTypeBlanks)
    On Error GoTo 0

    If rng Is Nothing Then
        With Range("R2:R" & lastrow).Validation
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
            Operator:=xlBetween, Formula1:="8%,10%,12%,15%"
            .IgnoreBlank = True
            .InCellDropdown = True
        End With
    Else
        With rng.Validation
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
            Operator:=xlBetween, Formula1:="8%,10%,12%,15%"
            .IgnoreBlank = True
            .InCellDropdown = True
        End With
    End If

End Sub

答案 1 :(得分:0)

您可以这样做

With Range("I1:I" & lastrow).SpecialCells(xlCellTypeBlanks).Validation
 .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
 Operator:=xlBetween, Formula1:="8%,10%,12%,15%"
 .IgnoreBlank = True
 .InCellDropdown = True
End With