ID非公開さん
2022/5/19 20:08
2回答
VBA超初心者です。やりたいことは以下の通りです。 UserFormのコンボボックスで、表1行目の任意の文字(あ、か、さ…or A、B、C…)を入れる
VBA超初心者です。やりたいことは以下の通りです。 UserFormのコンボボックスで、表1行目の任意の文字(あ、か、さ…or A、B、C…)を入れる →UserFormの「名前」に文字を入れる →コマンドボタンを押す →コンボボックスに入れた文字が「あ、か、さ…」なら「ristあいう」の特定の列の最終行に、コンボボックスに入れた文字が「A、B、C…」なら「ristABC」の特定の列の最終行に入力する このような作業をvbaで行うことが可能であれば、ご教示いただけますと嬉しいです。宜しくお願い致します。
Visual Basic | Excel・104閲覧・250
ベストアンサー
再回答です。 ■フォームモジュール Option Explicit Dim mysorce(61) As Variant Dim sh1 As Worksheet, sh2 As Worksheet '登録 Private Sub CommandButton1_Click() Dim c As Integer, i As Integer, r As Integer, index As Integer Dim mysel As String Set sh1 = Worksheets("ristあいう") Set sh2 = Worksheets("ristABC") index = UserForm1.ComboBox1.ListIndex If index <> -1 Then mysel = UserForm1.ComboBox1.List(index) End If If mysel Like "[A-Z]" = True Then sh2.Select With sh2 For i = 0 To UBound(mysorce) If mysel = mysorce(i) Then c = (i - 10) * 2 + 1 r = .Cells(Rows.Count, c).End(xlUp).Row + 1 .Cells(r, c) = TextBox1.Value End If Next i End With Else sh1.Select With sh1 For i = 0 To UBound(mysorce) If mysel = mysorce(i) Then c = i * 2 + 1 r = .Cells(Rows.Count, c).End(xlUp).Row + 1 .Cells(r, c) = TextBox1.Value End If Next i End With End If End Sub '終了 Private Sub CommandButton2_Click() Unload UserForm1 End Sub '初期設定 Private Sub UserForm_Initialize() Dim mydata As Variant Dim j As Long, n As Long Set sh1 = Worksheets("ristあいう") Set sh2 = Worksheets("ristABC") With sh1 For j = 1 To 19 Step 2 mysorce(n) = .Cells(1, j) n = n + 1 Next j End With With sh2 For j = 1 To 51 Step 2 mysorce(n) = .Cells(1, j) n = n + 1 Next j End With For Each mydata In mysorce ComboBox1.AddItem mydata Next ComboBox1.ListIndex = 0 End Sub ■標準モジュール Sub test() UserForm1.Show vbModeless End Sub
1人がナイス!しています
ID非公開さん
質問者2022/5/19 22:26
望み通りの作業が出来ました。ありがとうございました。
質問者からのお礼コメント
この度はありがとうございました。また宜しくお願いいたします。
お礼日時:5/19 22:27