2016-11-30 3 views
0

Excel VBA UserForm에 스크립트를 쓰고 있습니다. 양식에는 사용자가 채울 수있는 세 개의 다른 필드 (ComboBox)가 있습니다. 에 무엇을 입력했는지에 따라 ComboBox 3의 콘텐츠를 만들고 싶습니다. 다음과 같이ComboBox Excel VBA를 고정하는 방법

ComboBox2이 채워집니다

With ComboBox2 
     .AddItem "Legal Information" 
     .AddItem "Media" 
     .AddItem "Official Disclosures" 
     .AddItem "Patents and Trademarks" 
     .AddItem "Private Corporate Information" 
     .AddItem "Private Individual Information" 
     .AddItem "Property Information" 
     .AddItem "Public Company Information" 
     .AddItem "Public Tenders" 
     .AddItem "Ships, Vessels and Aircraft Information" 
     .AddItem "Watchlists/Blacklists" 
    End With 

ComboBox2에서 사용자의 입력이, ComboBox3이 다른 옵션에 의해 채워지는 내용에 따라. 다음과 같은 내가 뭐하는 거지 : 내가 ComboBox3에 대한 싶습니다
Private Sub ComboBox2_Change() 

Dim index As Integer 
index = ComboBox2.ListIndex 

ComboBox3.Clear 

Select Case index 
    Case Is = 0 
     With ComboBox3 
      .AddItem "Administrative" 
      .AddItem "Civil" 
      .AddItem "Criminal" 
     End With 
    Case Is = 1 
     With ComboBox3 
      .AddItem "Arts and Culture" 
      .AddItem "Blog/Social Media" 
      .AddItem "Business and Economics" 
      .AddItem "General News" 
      .AddItem "Intelligence and Security" 
      .AddItem "Official News Agency/Official Press" 
      .AddItem "Energy" 
      .AddItem "Pharmaceutical and Medical News" 
      .AddItem "Politics" 
      .AddItem "Religion" 
      .AddItem "Society, Lifestyle and Opinion" 
      .AddItem "Sport" 
     End With 

End Sub 

즉, 냉동 할 불가능 사용자가 입력하는, ComboBox2의 옵션을 다른이 선택된 경우 - 이벤트 Case is = 2, 3, 4, 5, 6, 7, 8, 9, 10에 . 어떻게해야합니까? 고맙습니다.

+4

'ComboBox3.Enabled = False'? – CMArg

+2

.locked를 사용하여 잠 그거나 사용 가능하게합니다. 사용 가능 –

+2

FWIW'Case Is = 0'은'Case 0'으로보다 간단하게 쓸 수 있습니다. –

답변

0

설명에 명시된대로 ComboBox3ComboBox3.Enabled = False으로 "고정"됩니다. 그러나 사용자가 ComboBox을 "고정"한 다음 ComboBox2에서 다른 옵션을 선택하지 않으려면 각 인덱스 앞에 ComboBox3.Enabled = True을 삽입해야합니다.

수정 된 코드는 다음과 같습니다.

Private Sub ComboBox2_Change() 

Dim index As Integer 
index = ComboBox2.ListIndex 

ComboBox3.Clear 

Select Case index 
    Case Is = 0 
     ComboBox3.Enabled = True 
     With ComboBox3 
      .AddItem "Administrative" 
      .AddItem "Civil" 
      .AddItem "Criminal" 
     End With 
    Case Is = 1 
     ComboBox3.Enabled = True 
     With ComboBox3 
      .AddItem "Arts and Culture" 
      .AddItem "Blog/Social Media" 
      .AddItem "Business and Economics" 
      .AddItem "General News" 
      .AddItem "Intelligence and Security" 
      .AddItem "Official News Agency/Official Press" 
      .AddItem "Energy" 
      .AddItem "Pharmaceutical and Medical News" 
      .AddItem "Politics" 
      .AddItem "Religion" 
      .AddItem "Society, Lifestyle and Opinion" 
      .AddItem "Sport" 
     End With 
    Case Is = 2 
     ComboBox3.Enabled = False 
    Case Is = 3 
     ComboBox3.Enabled = False 
    Case Is = 4 
     ComboBox3.Enabled = False 
    Case Is = 5 
     ComboBox3.Enabled = False 
    Case Is = 6 
     ComboBox3.Enabled = False 
    Case Is = 7 
     ComboBox3.Enabled = False 
    Case Is = 8 
     ComboBox3.Enabled = False 
    Case Is = 9 
     ComboBox3.Enabled = False 
    Case Is = 10 
     ComboBox3.Enabled = False 
End Select 

End Sub 
+0

'ComboBox3.Clear' 바로 다음에'ComboBox3.Enabled = False'를 추가하여 해당 cmb를 비활성화 할 수 있습니다. 그리고 다른 모든 사례 (2, 3, 4 등)를 나열하는 대신 'Case Else' – CMArg

1

콤보 상자의 "MatchRequired"를 사용하고 TRUE로 설정하면 콤보 상자에있는 항목 만 선택하거나 입력 할 수 있습니다. 그것을 얼릴 필요가 없다.

관련 문제