[ 初めての方へ | 一覧(最新更新順) | 全文検索 | 過去ログ ]
『テキストボックスの値を元のセルに返す』(まる)
過去の回答を参考にユーザーフォームを作って住所録からテキストボックスに 名字を入力して、検索→対象者のリストをリストボックスに表示→選択したリストの 氏名、郵便番号、住所、を各テキストボックスに表示させるところまでやってみました。 (多分ところどころおかしいかと思いますが・・・) テキストボックスの内容を変更してコマンドボタンを押すと住所録のデータが修正 されるようにしたいのですが、ここのコードがわかりません。 Private Sub cmdEntry_Click()の中のコードを修正すべきだと思うのですが、 自分でできませんでした。現在のコードだB2:E2に修正データが入力されてしまいます。 どなたかご教授下さい。
住所録はシート名:社員にあり、A4:E150の範囲にデータがあります。
A B C D
3行目 社員コード 氏名 郵便番号 住所 4行目
150行目
frmEntry:ユーザーフォーム名 cmdClose:コマンドボタン(閉じる)ユーザーフォームを閉じる cmdEntry:コマンドボタン(入力)修正データを元セルの入力させる cmdSearch:コマンドボタン(検索)TextBox1に名字を入力したら検索ボタンを押す TextBox1:テキストボックス(名字検索用) txtName1.Value:テキストボックス(氏名) txtName2.Value:テキストボックス(郵便番号) txtName3.Value:テキストボックス(住所) txtName4.Value:テキストボックス(喪中チェック)
Private Sub cmdClose_Click()
Unload frmEntry
End Sub
Private Sub cmdEntry_Click()
Dim sht As Worksheet Set sht = Worksheets("社員") With Me.ListBox1 If .ListIndex >= 0 Then sht.Cells(Me.ListBox1.ListIndex, 2).Value = txtName1.Text sht.Cells(Me.ListBox1.ListIndex, 3).Value = CVar(txtName2.Text) sht.Cells(Me.ListBox1.ListIndex, 4).Value = txtName3.Text sht.Cells(Me.ListBox1.ListIndex, 5).Value = txtName4.Text
.List(.ListIndex, 1) = txtName1.Text .List(.ListIndex, 2) = CVar(txtName2.Text) .List(.ListIndex, 3) = txtName3.Text .List(.ListIndex, 4) = txtName4.Text
End If End With End Sub
Private Sub cmdSearch_Click() Dim c As Long, i As Long, j As Long, tbl As Variant Me.ListBox1.Clear With Worksheets("社員").Range("A4").CurrentRegion c = Application.WorksheetFunction.CountIf(.Columns("B"), "*" & Me.TextBox1.Value & "*") If c = 0 Then Exit Sub tbl = .Value End With Me.ListBox1.ColumnCount = 5 For i = 1 To UBound(tbl) If tbl(i, 2) Like "*" & Me.TextBox1.Value & "*" Then With Me.ListBox1 .AddItem For j = 1 To 5 .List(.ListCount - 1, j - 1) = tbl(i, j) Next j End With End If Next i End Sub
Private Sub ListBox1_Click()
With ListBox1 txtName1.Value = .List(.ListIndex, 1) txtName2.Value = .List(.ListIndex, 2) txtName3.Value = .List(.ListIndex, 3) txtName4.Value = .List(.ListIndex, 4) End With
End Sub
Private Sub UserForm_Initialize()
Me.ListBox1.List = Sheets("社員").Range("A4:E150").Value
End Sub
WindowsXP 2000
Listbox1の ListIndexと社員というシートのデータ開始行の関係は、
データ開始行=Listbox1.Listindex+4
ですから・・・、 sht.Cells(Me.ListBox1.ListIndex+4, 2).Value = txtName1.Text sht.Cells(Me.ListBox1.ListIndex+4, 3).Value = CVar(txtName2.Text) sht.Cells(Me.ListBox1.ListIndex+4, 4).Value = txtName3.Text sht.Cells(Me.ListBox1.ListIndex+4, 5).Value = txtName4.Text で試してみてください。
ichinose
ichinose様 試してみたところ、選択したセルに一致するデータではなく、5行目のデータが 修正されてしまいました。 他に修正点がありましたら教えて下さい。
(まる)
ListBoxのリストを絞り込んでしまったらListIndexと行との関係は崩れるので 検索しなおすのが簡単だと思います。
Dim myRow as Long myRow = WorksheetFunction.Match(.List(.ListIndex, 1), sht.Columns("B"), 0) sht.Cells(myRow, 2).Value = txtName1.Text sht.Cells(myRow, 3).Value = CVar(txtName2.Text) sht.Cells(myRow, 4).Value = txtName3.Text sht.Cells(myRow, 5).Value = txtName4.Text
(momo)
momo様 おかげさまでできました。 今回のユーザーフォームを作るのにmomo様とichinose様の過去の回答が 大変参考になりました。ありがとうございました!
まる
>選択したセルに一致するデータではなく、5行目のデータが >修正されてしまいました。
あっ、リストボックスには、絞込みデータが登録されている事に気がつきませんでした。 解決後ですが、momoさんとは 別の方法で・・・・。
絞り込んでリストボックスに設定時にデータの行番号も設定してしまう方法です。
ユーザーフォームのモジュールに
Private Sub cmdClose_Click()
Unload frmEntry
End Sub
Private Sub cmdEntry_Click()
Dim sht As Worksheet Set sht = Worksheets("社員") With Me.ListBox1 If .ListIndex >= 0 Then sht.Cells(.List(.ListIndex, 5), 2).Value = txtname1.Text sht.Cells(.List(.ListIndex, 5), 3).Value = CVar(txtname2.Text) sht.Cells(.List(.ListIndex, 5), 4).Value = txtname3.Text sht.Cells(.List(.ListIndex, 5), 5).Value = txtname4.Text
.List(.ListIndex, 1) = txtname1.Text .List(.ListIndex, 2) = CVar(txtname2.Text) .List(.ListIndex, 3) = txtname3.Text .List(.ListIndex, 4) = txtname4.Text
End If End With End Sub
Private Sub cmdSearch_Click() Dim rng As Range Dim c As Long, i As Long, j As Long, tbl As Variant Me.ListBox1.Clear With Worksheets("社員") Set rng = .Range("a4", .Cells(.Rows.Count, "a").End(xlUp)).Resize(, 5) If rng.Row >= 4 Then c = Application.WorksheetFunction.CountIf(rng.Columns(2), "*" & Me.TextBox1.Value & "*") End If End With If c > 0 Then tbl = rng.Value Me.ListBox1.ColumnCount = 5 For i = 1 To UBound(tbl) If tbl(i, 2) Like "*" & Me.TextBox1.Value & "*" Then With Me.ListBox1 .AddItem For j = 1 To 5 .List(.ListCount - 1, j - 1) = tbl(i, j) Next j .List(.ListCount - 1, j - 1) = i + 3 End With End If Next i End If Set rng = Nothing End Sub
Private Sub ListBox1_Click()
With ListBox1 txtname1.Value = .List(.ListIndex, 1) txtname2.Value = .List(.ListIndex, 2) txtname3.Value = .List(.ListIndex, 3) txtname4.Value = .List(.ListIndex, 4) End With
End Sub
因みに まるさんのコードだと セルB3の氏名も検索対象になってしまいますよ!!
ichinose
ichinose様
追加回答ありがとうございました。気づくのが遅くなりすみません。
こちらのコードも試してみたらうまくいきました。 Private Sub cmdSearch_Click()のコードのところがまだ理解しきれていなかった ので、これから勉強します。 おかげさまで大変参考になりました!少しずつ覚えていくのが楽しくなってきた ところなので、追加回答ありがたいです。
まる
[ 一覧(最新更新順) ]
YukiWiki 1.6.7 Copyright (C) 2000,2001 by Hiroshi Yuki.
Modified by kazu.