質問するログイン新規登録

回答編集履歴

4

コード修正

2018/05/02 07:53

投稿

hatena19
hatena19

スコア34377

answer CHANGED
@@ -12,7 +12,7 @@
12
12
  MsgBox "データのあるセルを選択してください。"
13
13
  Exit Sub
14
14
  ElseIf EndR.Row = ActiveCell.Row Then
15
- Set r = ActiveCell
15
+ Set r = Cells(ActiveCell.Row, 1)
16
16
  Else
17
17
  Set r = Range(Cells(ActiveCell.Row, 1), EndR).SpecialCells(xlCellTypeVisible)
18
18
  End If
@@ -73,7 +73,7 @@
73
73
  MsgBox "データのあるセルを選択してください。"
74
74
  Exit Sub
75
75
  ElseIf EndR.Row = ActiveCell.Row Then
76
- Set r = ActiveCell
76
+ Set r = Cells(ActiveCell.Row, 1)
77
77
  Else
78
78
  Set r = Range(Cells(ActiveCell.Row, 1), EndR).SpecialCells(xlCellTypeVisible)
79
79
  End If

3

コード修正

2018/05/02 07:53

投稿

hatena19
hatena19

スコア34377

answer CHANGED
@@ -36,9 +36,11 @@
36
36
  この仕様にも対応するようにしてみました。
37
37
 
38
38
  ```vba
39
+ Option Explicit
40
+
39
41
  Private Sub CB_Change()
40
42
  With Me.ActiveControl
41
- Cells(.Tag, 4).Value = .Value
43
+ If .Tag <> "" Then Cells(.Tag, 4).Value = .Value
42
44
  End With
43
45
  End Sub
44
46
 
@@ -62,7 +64,6 @@
62
64
  CB_Change
63
65
  End Sub
64
66
 
65
-
66
67
  Private Sub CommandButton1_Click()
67
68
  Dim r As Range, c As Range, n As Long, EndR As Range
68
69
 
@@ -85,11 +86,11 @@
85
86
  Next
86
87
 
87
88
  For n = n + 1 To 5
88
- Me("TextBox" & n) = ""
89
+ Me("TextBox" & n).Value = ""
90
+ Me("ComboBox" & n).Tag = ""
89
91
  Next
90
92
  End Sub
91
93
 
92
-
93
94
  Private Sub UserForm_Initialize()
94
95
  Dim i As Long
95
96
 

2

コードの追記

2018/05/02 05:48

投稿

hatena19
hatena19

スコア34377

answer CHANGED
@@ -27,4 +27,77 @@
27
27
  Me("TextBox" & n) = ""
28
28
  Next
29
29
  End Sub
30
+ ```
31
+
32
+ 追記
33
+ ---
34
+ > 最終目的は、テキストBOXの横にあるドロップダウンリストから○×を選ぶと、対応する行のD列にその値が入るようにすることです。
35
+
36
+ この仕様にも対応するようにしてみました。
37
+
38
+ ```vba
39
+ Private Sub CB_Change()
40
+ With Me.ActiveControl
41
+ Cells(.Tag, 4).Value = .Value
42
+ End With
43
+ End Sub
44
+
45
+ Private Sub ComboBox1_Change()
46
+ CB_Change
47
+ End Sub
48
+
49
+ Private Sub ComboBox2_Change()
50
+ CB_Change
51
+ End Sub
52
+
53
+ Private Sub ComboBox3_Change()
54
+ CB_Change
55
+ End Sub
56
+
57
+ Private Sub ComboBox4_Change()
58
+ CB_Change
59
+ End Sub
60
+
61
+ Private Sub ComboBox5_Change()
62
+ CB_Change
63
+ End Sub
64
+
65
+
66
+ Private Sub CommandButton1_Click()
67
+ Dim r As Range, c As Range, n As Long, EndR As Range
68
+
69
+ Set EndR = Cells(Rows.Count, 1).End(xlUp)
70
+
71
+ If EndR.Row < ActiveCell.Row Then
72
+ MsgBox "データのあるセルを選択してください。"
73
+ Exit Sub
74
+ ElseIf EndR.Row = ActiveCell.Row Then
75
+ Set r = ActiveCell
76
+ Else
77
+ Set r = Range(Cells(ActiveCell.Row, 1), EndR).SpecialCells(xlCellTypeVisible)
78
+ End If
79
+
80
+ For Each c In r
81
+ n = n + 1
82
+ Me("TextBox" & n).Value = c.Value & c.Offset(, 1).Value
83
+ Me("ComboBox" & n).Tag = c.Row 'コンボボックスのTagに行インデックスを格納
84
+ If n = 5 Then Exit For
85
+ Next
86
+
87
+ For n = n + 1 To 5
88
+ Me("TextBox" & n) = ""
89
+ Next
90
+ End Sub
91
+
92
+
93
+ Private Sub UserForm_Initialize()
94
+ Dim i As Long
95
+
96
+ For i = 1 To 5
97
+ With Me("ComboBox" & i)
98
+ .AddItem ("○")
99
+ .AddItem ("×")
100
+ End With
101
+ Next
102
+ End Sub
30
103
  ```

1

コードの修正

2018/05/02 05:10

投稿

hatena19
hatena19

スコア34377

answer CHANGED
@@ -4,18 +4,25 @@
4
4
 
5
5
  ```vba
6
6
  Private Sub CommandButton1_Click()
7
- Dim r As Range, c As Range, n As Long
7
+ Dim r As Range, c As Range, n As Long, EndR As Range
8
8
 
9
- Set r = Range(Cells(ActiveCell.Row, 1), Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
9
+ Set EndR = Cells(Rows.Count, 1).End(xlUp)
10
10
 
11
+ If EndR.Row < ActiveCell.Row Then
12
+ MsgBox "データのあるセルを選択してください。"
13
+ Exit Sub
14
+ ElseIf EndR.Row = ActiveCell.Row Then
15
+ Set r = ActiveCell
16
+ Else
11
- Debug.Print Me("TextBox1") = ""; IsNull(Me("TextBox1"))
17
+ Set r = Range(Cells(ActiveCell.Row, 1), EndR).SpecialCells(xlCellTypeVisible)
18
+ End If
12
19
 
13
20
  For Each c In r
14
21
  n = n + 1
15
22
  Me("TextBox" & n) = c.Value & c.Offset(, 1).Value
16
23
  If n = 5 Then Exit For
17
24
  Next
18
-
25
+
19
26
  For n = n + 1 To 5
20
27
  Me("TextBox" & n) = ""
21
28
  Next