如果范围包含任何值,则活动行中单元格范围的数据验证

时间:2013-07-02 16:54:25

标签: vba

我有一个带有语言ID复选框的用户表单。选中时,将填充工作表“输出”上的单元格。这是代码:

Private Sub btnSubmit_Click()

Sheets("output").Activate

NextRow = Application.WorksheetFunction.CountA(Range("A:A")) + 1
    If cbxEnUs Then Cells(NextRow, 3) = "3 - en-us"
    If cbxFr Then Cells(NextRow, 4) = "3 - fr"
    If cbxIt Then Cells(NextRow, 5) = "3 - it"

Sheets("input").Activate

我想将以下数据验证(从记录的宏)应用到接收值的每个单元格。

With Selection.Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertInformation, _
    Operator:=xlBetween, Formula1:="3 - en-us,2 - en-us,1 - en-us"
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = "Incorrect value"
    .InputMessage = "3 = action req'd" & Chr(10) & "2 = in progress" & Chr(10) & "1 = complete"
    .ErrorMessage = "Please review valid input values" & Chr(10) & "and try again"
    .ShowInput = True
    .ShowError = True
End With

如何连接这两段代码?

1 个答案:

答案 0 :(得分:1)

将第二个代码与第一个代码合并,将第一行上的Selection替换为Cells(NextRow, 3)等。

您需要跨行分割您的IF语句:

If cbxEnUs Then 
    Cells(NextRow, 3) = "3 - en-us"
    ' apply validation to this cell
    With Cells(NextRow, 3).Validation
        'etc..
    End With
End if    'etc..