VBA-撤消组合框更改

时间:2018-06-27 16:54:55

标签: excel-vba combobox vba excel

我似乎无法使此代码正常工作...我想基本上说的是,如果组合框发生更改且范围不为空,请询问用户是否确实要清除范围并进行更改。如果他们拒绝,则撤消组合框更改回原来的状态。我的“ Sub”选项不显示“ BeforeUpdate”,仅显示“ Change”和其他一些选项。

我试图预先捕获该值并将其设置回该值,但是它不起作用。

欢迎提出任何想法!

Sub ComboBox_UPC_C18_Change()

    Dim ComboBox_UPC_C18_Value As String

    Dim Location As Range
    Set Location = Range("Location_C18")

        If Application.WorksheetFunction.CountA(Location) = 0 Then

            Exit Sub

        ElseIf MsgBox("Are you sure you want change the UPC (clearing the row's data)?", vbYesNo, "User Confirmation") = vbYes Then

            Location.Value = ""

        ElseIf MsgBox("Are you sure you want change the UPC (clearing the row's data)?", vbYesNo, "User Confirmation") = vbNo Then

            ComboBox_UPC_C18.Value = ComboBox_UPC_C18_Value
            MsgBox (ComboBox_UPC_C18_Value)

            Exit Sub

        End If

    ComboBox_UPC_C18_Value = ComboBox_UPC_C18.Value
    MsgBox (ComboBox_UPC_C18_Value)

End Sub

1 个答案:

答案 0 :(得分:0)

我无法对此进行测试,但我认为这与您要查找的内容很接近。让我知道我是否完全不在这里:

Private Sub ComboBox_UPC_C18_Change()
    Dim RngLocation     As Range
    Dim PrevLocation    As String

    Application.EnableEvents = False

    PrevLocation = Range("Location_C18").Value

    If PrevLocation = "" Then
        Range("Location_C18").Value = ComboBox_UPC_C18.Value
    Else
        If MsgBox("Are you sure you want to change this?", vbYesNo) = vbYes Then
            Range("Location_C18").Value = ComboBox_UPC_C18.Value
        Else
            ComboBox_UPC_C18.Value = PrevLocation
        End If
    End If

    Application.EnableEvents = True
End Sub