Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA数据验证列表选择_Vba_Excel - Fatal编程技术网

VBA数据验证列表选择

VBA数据验证列表选择,vba,excel,Vba,Excel,我正在编写一个代码嵌入到一个表中。情况是这样的: A1单元的选项为50100和150,A2单元的选项为1000和5000,都有数据验证列表。当我选择单元格A1为50时,A2必须为1000。当我编写代码并进行选择时,始终存在一个错误: 运行时错误“-214741784880010108”:对象“验证”的方法“添加”失败 请分享您的意见,或者如果需要我在这里发布代码来解决这个问题 基本代码如下: Select Case Range("A1").Value Case "50" Ran

我正在编写一个代码嵌入到一个表中。情况是这样的: A1单元的选项为50100和150,A2单元的选项为1000和5000,都有数据验证列表。当我选择单元格A1为50时,A2必须为1000。当我编写代码并进行选择时,始终存在一个错误:

运行时错误“-214741784880010108”:对象“验证”的方法“添加”失败

请分享您的意见,或者如果需要我在这里发布代码来解决这个问题

基本代码如下:

Select Case Range("A1").Value
Case "50"
         Range("A2").ClearContents
        Range("A6").Value2 = "1000"

Case "`100"
         Range("A2").ClearContents
       With Range("A2").Validation
        .Delete
        .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
        :=xlBetween
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With
        Range("A2").Value2 = 1000

将此代码添加到包含要添加验证的单元格的工作表模块中

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Address = "$A$1" Then

        If Target.Value = 50 Then

             Range("A2") = 1000

            ' Add valid values of 1000 allowed only
            With Range("A2").Validation
                .Delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="1000"
                .IgnoreBlank = True
                .InCellDropdown = True
                .InputTitle = ""
                .ErrorTitle = ""
                .InputMessage = ""
                .ErrorMessage = "Enter a valid value"
                .ShowInput = False
                .ShowError = True
            End With

        Else
            ' Add valid values of 1000 and 5000 allowed
            With Range("A2").Validation
                .Delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="1000,5000"
                .IgnoreBlank = True
                .InCellDropdown = True
                .InputTitle = ""
                .ErrorTitle = ""
                .InputMessage = ""
                .ErrorMessage = "Enter a valid value"
                .ShowInput = False
                .ShowError = True
            End With
        End If




    End If

End Sub

欢迎来到堆栈溢出。你需要提供更多的信息。发布您编写的代码怎么样?以下是代码:选择RangeA1.Value Case 50,RangeA2.Value=1000结束选择我认为这应该是一个简单的代码,但在excel工作簿中,当我在A1中选择50时,出现了一个错误,我的excel被压碎,您知道如何调试VBA代码吗。如果没有找到。你需要引用给你的错误。你的代码是否编译。你的代码在哪个模块?嗨,我编辑了我的帖子。我进行了调试,但弹出的信息不足以解决问题。我在VBE中使用更改模式。我看到您现在正在尝试做什么…看到我的答案。以后尽量多提供信息和解释。其他人帮不了你多少忙!嗨,朋友。它确实有用!非常感谢你!但你知道我错在哪一部分吗?您的代码非常有用,但对于这一部分,如果Target.Address=$A$1,那么A1和$A$1之间的区别是什么。你能给我解释一下吗?再次感谢,