ComboBoxで検索エラーを回避するには?

解決


NMmarge  2006-10-24 22:00:44  No: 96987

Excelです。
ComboBoxでRowSource内にない言葉を書き込んだ場合、エラーが出るんです。

RowSource内にない言葉を書き込んだ時に「もう一度選択し直すか」もしくは、その言葉をRowSourceに加えるにはどうすればいいですか?

現在:    xyz = ComboBox1
          Range("ABC").Find(xyz, LookAt:=xlWhole).Activate


VB梅  2006-10-24 22:53:51  No: 96988

NMmatgeさん  こんにちは
昔に作ったもので参考になればうれしいです。
ユーザーフォームにボタンとコンボボックスを作って
下のプログラムを貼り付ければ動くと思います。

Private Sub CommandButton1_Click()
 Dim count As Integer
 Dim gyou As Integer
 Dim i As Integer   
 For i = 0 To 9
  count = count - (Cells(1 + 256 * i) = ComboBox1.Value)
  gyou = gyou - (Cells(1 + 256 * i) <> "")
 Next i
 If count = 0 Then Cells(1 + 256 * gyou) = ComboBox1.Value
 UserForm_Activate
End Sub
Private Sub UserForm_Activate()
 Dim gyou As Integer
 Dim i As Integer
 For i = 0 To 9
  gyou = gyou - (Cells(1 + 256 * i) <> "")
 Next i
 If gyou = 0 Then gyou = 1
 ComboBox1.RowSource = "A1:A" & gyou
End Sub

サンプルで作成したので不都合な部分もあります
ご自分で改良してください。


NMmarge  2006-10-25 01:51:00  No: 96989

もしよろしければ
今後の役に立つと思うので簡単に説明していただきたいのですが…


VB梅  2006-10-25 02:29:40  No: 96990

説明?このソースのどこに説明を入れろと?
順次デバックすればわかりますよ。

取り合えず
count = count - (Cells(1 + 256 * i) = ComboBox1.Value)
と言うのは

If Cells(1 + i, 1) = ComboBox1.Value Then
    count = count + 1
endif
と同じです。

ちなみにComboBoxのRowSourceは
ComboBox1.RowSource = "ABC"でセットさていると解釈しましたが、
考え方は↓の様になります。
Range("A1:A" & gyou)


NMmarge  2006-10-25 04:41:10  No: 96991

ありがとうございました。


※返信する前に利用規約をご確認ください。

※Google reCAPTCHA認証からCloudflare Turnstile認証へ変更しました。






  このエントリーをはてなブックマークに追加