Application.Undo引发错误并转到ExitSub

时间:2019-07-04 07:51:04

标签: excel vba excel-2016

我正在Excel宏中进行Excel下拉菜单中的多项选择。遇到了以下代码以进行多次选择

<QuerySet>

正如我从互联网上了解到的,这或多或少是多选的标准代码。但是我在这里遇到了一些问题。

当我更改下拉列表中的值时,以下行引起了一些问题:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub

If Target.Column = 7 And (Target.Row >= 1 And Target.Row <= 5000) Then
  If Target.SpecialCells(xlCellTypeAllValidation) 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
        If InStr(1, Oldvalue, Newvalue) = 0 Then
            Target.value = Oldvalue & " # " & Newvaluae
      Else:
        Target.value = Oldvalue
      End If
    End If
  End If
End If
Application.EnableEvents = True
Exitsub:
MsgBox "Error"
Application.EnableEvents = True
End Sub

并将控件发送到Application.Undo ,我通过在其中放置MsgBox进行了检查。

任何人都可以指出为什么此代码不起作用吗?我正在使用MS Excel 2016,如果有帮助的话

编辑:

我注意到代码中的一个错误,并对其进行了更正,我认为这是该行为的原因。更改后它确实可以工作一到两次,但是再次出现了相同的问题。 我更改了以下行:

ExitSub

对此

Target.value = Oldvalue & " # " & Newvaluae

我真的不明白这种代码行为的原因。如果有人可以阐明一下,那就太好了。

1 个答案:

答案 0 :(得分:0)

好吧,我在代码中发现了错误。这是一个很小的问题。应该是:

Target.value = Oldvalue & " # " & Newvalue

代替

Target.value = Oldvalue & " # " & Newvaluae

最后一个参数拼写错误。

相关问题