Excel VBA Worksheet.Change无法正常工作

我有下面的代码,基本上放置在数据validation框(如果单元格包含数据validation框)combobox,以便用户仍然可以有自动完成function,但数据validation停留在单元格中。

'================================================================================================= '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时,它不起作用。 我相信它必须与combobox覆盖数据validation,因为如果我在一个正常的数据validation单元上这样做,它确实工作…这篇文章基本上说,如果他们更改数据validation框,然后模块需要根据第一个用户的select,将被调用,这将更新其他框。

 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 

任何帮助表示赞赏! 先谢谢你!

由于combobox更改事件不会触发工作表更改事件,因此您的test2.data_validation_areaspecificmachinesmacros不会获得调用。 您应该从combobox,_Change,_Click或_DropButtonClick事件调用您的macros。