简体   繁体   English

数据验证码:指定目标范围

[英]Data Validation code: specify target range

I have an excel worksheet with a table where column headers are time intervals and row headers are people's names. 我有一个带有表的excel工作表,其中列标题是时间间隔,行标题是人的名字。 Each cell in the middle has a dropdown list. 中间的每个单元格都有一个下拉列表。 I inputted the code below so that the user can select more than one option from the dropdown within each cell. 我在下面输入了代码,以便用户可以从每个单元格的下拉列表中选择多个选项。

The code works fine however the entire sheet is being affected by it rather than just the target range. 该代码可以正常工作,但是整个工作表都将受到它的影响,而不仅仅是目标范围。 For example, when I try to edit a cell in the column header just by hitting backspace on the cell and inputting new text...the cell formats like this: "6:30am, 7:30am" or "old value, new value" 例如,当我尝试通过单击单元格上的退格键并输入新文本来编辑列标题中的单元格时,像这样的单元格格式:“ 6:30 am,7:30 am”或“旧值,新值” ”

Option Explicit

 Private Sub Worksheet_Change(ByVal Target As Range)
 ' To Select Multiple Items from a Drop Down List in Excel

  Dim Oldvalue As String
  Dim Newvalue As String

On Error GoTo Exitsub
If Target.Range("E10:V600") Is Nothing Then
GoTo Exitsub
Else: If Target.Value = "" Then GoTo Exitsub Else
    Application.EnableEvents = False
    Newvalue = Target.Value
    Application.Undo
    Oldvalue = Target.Value
    If Oldvalue = "" Then
        Target.Value = Newvalue
    Else
        Target.Value = Oldvalue & ", " & Newvalue
End If
End If
Application.EnableEvents = True

Exitsub:
Application.EnableEvents = True

End Sub

I can avoid this by right clicking on the cell, hitting clear contents, exiting the cell, then clicking the cell again to edit it. 我可以通过右键单击该单元格,清除内容,退出该单元格,然后再次单击该单元格进行编辑来避免这种情况。 But I want to prevent this issue. 但是我想防止这个问题。 Any idea where my code went wrong? 知道我的代码出错了吗? The target range is the correct range of cells I need the code to work on. 目标范围是我需要处理代码的单元格的正确范围。 Thanks in advance for your help 在此先感谢您的帮助

This line: If Target.Range("E10:V600") Is Nothing Then is not correct syntax. 这行代码: If Target.Range("E10:V600") Is Nothing Then语法不正确。 Or maybe the syntax compiles, but it will not produce what you want. 也许语法可以编译,但不会产生所需的内容。

Change it to this: If Not Instersect(Target, Me.Range("E10:V600")) Is Nothing Then 更改为: If Not Instersect(Target, Me.Range("E10:V600")) Is Nothing Then

So full code becomes ption Explicit 因此,完整的代码将成为ption显式的

 Private Sub Worksheet_Change(ByVal Target As Range)
     ' To Select Multiple Items from a Drop Down List in Excel

    Dim Oldvalue As String
    Dim Newvalue As String

    If Not Instersect(Target, Me.Range("E10:V600")) Is Nothing Then

        If Len(Target.Value) Then 

            Application.EnableEvents = False
            Newvalue = Target.Value

            Application.Undo

            Oldvalue = Target.Value

            If Oldvalue = "" Then
                Target.Value = Newvalue
            Else
                Target.Value = Oldvalue & ", " & Newvalue
            End If

            Application.EnableEvents = True   

        End If

    End If

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM