昨日回答し、疑問点をきいたところ
-------------------------------
質問者2025/11/26 2:00
tx1は1行目のものになります。
誠に申し訳ございませんが、tx2からtx10くらいまで果物名があることを想定して作成していただけないでしょうか
宜しくお願いします
-------------------------------
との返信がありました。
それを見る前にtx1は1行目として、コードを作成してしまっていましたので
tx6までのコードです。
増やすのは簡単な変更で可能ですので、ご自身でお願いします
スピンボタンはわかると言われていましたが結構面倒ですよ。
ユーザーフォーム全部のコードです。
Option Explicit
Dim ws As Worksheet
Dim rng As Range
Dim ck As Variant
Private Sub UserForm_Initialize()
Dim lastcol As Long
Dim i As Long
Set ws = Worksheets(\u0026quot;Sheet1\u0026quot;)
With ws
lastcol = .Cells(1, Columns.Count).End(xlToLeft).Column
If lastcol = 1 Then
tx1.Value = 1
Else
tx1.Value = .Cells(1, lastcol).Value + 1
End If
End With
SpinButton1.Value = tx1.Value
End Sub
Private Sub SpinButton1_Change()
Dim i As Long
tx1.Value = SpinButton1.Value
With ws
Set rng = .Range(.Cells(1, 1), .Cells(1, .Cells(1, Columns.Count).End(xlToLeft).Column))
ck = Application.Match(tx1.Value * 1, rng, 0)
For i = 2 To 6
If IsError(ck) = False Then
Controls(\u0026quot;tx\u0026quot; \u0026amp; i).Value = .Cells(i, ck).Value
Else
Controls(\u0026quot;tx\u0026quot; \u0026amp; i).Value = \u0026quot;\u0026quot;
End If
Next i
End With
End Sub
Private Sub CommandButton1_Click()
Dim lastcol As Long
Dim i As Long
With ws
Set rng = .Range(.Cells(1, 1), .Cells(1, .Cells(1, Columns.Count).End(xlToLeft).Column))
ck = Application.Match(tx1.Value * 1, rng, 0)
If IsError(ck) Then
ck = .Cells(1, Columns.Count).End(xlToLeft).Column + 1
End If
For i = 1 To 6
.Cells(i, ck).Value = Controls(\u0026quot;tx\u0026quot; \u0026amp; i).Value
Next i
tx1.Value = tx1.Value + 1
SpinButton1.Value = tx1.Value
For i = 2 To 6
Controls(\u0026quot;tx\u0026quot; \u0026amp; i).Value = \u0026quot;\u0026quot;
Next i
End With
End Sub