2017-11-02 6 views
1

基本的にデータ検証ボックス(セルにデータ検証ボックスが含まれている場合)の上にコンボボックスを配置して、ユーザーがオートコンプリート機能を引き続き使用できるようにするデータがありますが、検証はセル内にとどまります。Excel VBA Worksheet.Change not working

'================================================================================================= 
'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を使用すると、それは動作しません。私はそれがデータ検証をオーバーレイするコンボボックスと関係していると信じています。なぜなら、通常のデータ検証セルでこれを行うと動作します...この部分は基本的に、データ検証ボックスを変更すると、第1のユーザの選択に基づいて、他のボックスを更新する、呼び出されるべきである。

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

*「ただし、Worksheet_Changeを使用すると、動作しません」*「動作しません」と定義してください。イベントはトリガーされませんか?エラーがスローされますか?それは何をし、それはあなたの期待とは具体的にどう違うのですか? – MoondogsMaDawg

+0

私は謝罪しました - 私は説明すべきでした:それはエラーをスローしませんが、基本的にそれは変更が起こるのを見ていないかのように、モジュールを実行しません。実際の変更は重複しているコンボボックスで起こり、データ検証ボックス自体に起こる変化ではなく、データ検証ボックスに反映されるためだと思います。 – mitchmitch24

+0

私はそれは同じことを行い 'Target.Address(真、真)= "$ C $ 4" 次に コールTEST2.Data_Validation_AreaSpecificMachines Worksheet_SelectionChange内 エンドIf'場合に置きます。 – mitchmitch24

答えて

2

コンボボックス変更イベントはワークシート変更イベントをトリガしないため、test2.data_validation_areaspecificmachinesマクロはコールを取得しません。あなたは、コンボボックス、_Change、_Clickまたは_DropButtonClickイベントからマクロを呼び出す必要があります。