Excel VBA Worksheet.Change无法正常工作

时间:2017-11-02 19:47:21

标签: excel vba excel-vba validation combobox

我有以下代码,它基本上将一个组合框放在数据验证框上(如果单元格包含数据验证框),这样用户仍然可以拥有自动完成功能,但数据验证仍保留在单元格中。

'=================================================================================================
'From: http://http://www.contextures.com/xlDataVal14.html
'Code places combobox over data validation boxes to gain the autocomplete feature

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.ScreenUpdating = False

Dim Cancel As Boolean
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Set ws = ActiveSheet
On Error GoTo errHandler

If Target.Count > 1 Then GoTo exitHandler

Set cboTemp = ws.OLEObjects("MachineListComboBox")
  On Error Resume Next
If cboTemp.Visible = True Then
  With cboTemp
    .Top = 10
    .Left = 10
    .ListFillRange = ""
    .LinkedCell = ""
    .Visible = False
    .Value = ""
  End With
End If

  On Error GoTo errHandler
  If Target.Validation.Type = 3 Then
    'if the cell contains a data validation list
    Cancel = True
    Application.EnableEvents = False
    'get the data validation formula
    str = Target.Validation.Formula1
    str = Right(str, Len(str) - 1)
    With cboTemp
      'show the combobox with the list
      .Visible = True
      .Left = Target.Left
      .Top = Target.Top
      .Width = Target.Width + 15
      .Height = Target.Height + 5
      .ListFillRange = str
      .LinkedCell = Target.Address
      .Object.Style = 0
        'Object.Style will create a dropdown that will only allow the user to:
        '= 0 ... the user can type in any answer they want, while also getting the dropdown options
        '= 2 ... the user can type but will only get autocomplete options from the dropdown
      End With
    cboTemp.Activate
    'open the drop down list automatically
    Me.MachineListComboBox.DropDown
  End If

exitHandler:
  Application.ScreenUpdating = True
  Application.EnableEvents = True
  Exit Sub
errHandler:
  Resume exitHandler

End Sub

'=================================================================================================
'https://msdn.microsoft.com/en-us/library/aa243025%28v=vs.60%29.aspx
'Optional code to move to next cell if Tab or Enter are pressed
'from code by Ted Lanham
'***NOTE: if KeyDown causes problems, change to KeyUp
'Table with numbers for other keys such as Right Arrow (39)


Private Sub MachineListComboBox_KeyDown(ByVal _
        KeyCode As MSForms.ReturnInteger, _
        ByVal Shift As Integer)
    Select Case KeyCode
        Case 9 'Tab
        Application.ScreenUpdating = False
            ActiveCell.Offset(0, 10).Activate
            ActiveCell.Offset(0, -9).Activate
        Application.ScreenUpdating = True
        Case 13 'Enter
        Application.ScreenUpdating = False
            ActiveCell.Offset(1, 10).Activate
            ActiveCell.Offset(0, -10).Activate
        Application.ScreenUpdating = True
        Case Else
            'do nothing
    End Select
End Sub

然而,当我去使用Worksheet_Change时,它不起作用。我认为它与覆盖数据验证的组合框有关,因为如果我在普通数据验证单元格上执行此操作,它确实有效...这篇文章基本上说,如果他们更改了数据验证框,那么模块需要被调用,将根据第一个用户选择更新其他框。

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Address(True, True) = "$C$4" Then

        Call TEST2.Data_Validation_AreaSpecificMachines

    End If

End Sub

任何帮助表示赞赏!提前谢谢!

1 个答案:

答案 0 :(得分:2)

由于组合框更改事件未触发工作表更改事件,因此您的test2.data_validation_areaspecificmachines宏不会调用。你应该从组合框,_Change,_Click或_DropButtonClick事件中调用你的宏。