質問編集履歴
3
書式改善
title
CHANGED
File without changes
|
body
CHANGED
@@ -16,7 +16,7 @@
|
|
16
16
|
対策あれば教えていただきたいです。
|
17
17
|
よろしくお願いいたします。
|
18
18
|
|
19
|
-
※追記
|
19
|
+
### ※追記
|
20
20
|
2番目のソースコードを参考に今回のコードを書いたのですが、そちらではChangeイベントが発生しないまま進みます。
|
21
21
|
何の違いがあってこのようなことが起こるのか、教えていただきたいです。
|
22
22
|
よろしくお願いいたします。
|
2
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -15,6 +15,12 @@
|
|
15
15
|
|
16
16
|
対策あれば教えていただきたいです。
|
17
17
|
よろしくお願いいたします。
|
18
|
+
|
19
|
+
※追記
|
20
|
+
2番目のソースコードを参考に今回のコードを書いたのですが、そちらではChangeイベントが発生しないまま進みます。
|
21
|
+
何の違いがあってこのようなことが起こるのか、教えていただきたいです。
|
22
|
+
よろしくお願いいたします。
|
23
|
+
|
18
24
|
### 該当のソースコード
|
19
25
|
|
20
26
|
```Excel VBA
|
@@ -114,4 +120,133 @@
|
|
114
120
|
Private Sub btnClose_Click()
|
115
121
|
Unload Me
|
116
122
|
End Sub
|
123
|
+
```
|
124
|
+
|
125
|
+
### Changeイベントが発生せずに進むコード
|
126
|
+
```
|
127
|
+
Option Explicit
|
128
|
+
|
129
|
+
Private Sub btnClose_Click()
|
130
|
+
Unload Me
|
131
|
+
End Sub
|
132
|
+
|
133
|
+
Private Sub btnDelete_Click()
|
134
|
+
Dim strMsg As String
|
135
|
+
Dim strTitle As String
|
136
|
+
Dim res As Integer 'MsgBoxの戻り値を格納
|
137
|
+
|
138
|
+
strMsg = ListBox1.List(ListBox1.ListIndex, 1) 'リストボックスで選択された値の2列目(第2引数「1」)の値を変数に格納
|
139
|
+
strMsg = strMsg & " を削除します。よろしいですか?"
|
140
|
+
strTitle = "削除の確認"
|
141
|
+
res = MsgBox(strMsg, vbYesNo + vbExclamation, strTitle)
|
142
|
+
If res = vbNo Then Exit Sub
|
143
|
+
|
144
|
+
'削除の処理
|
145
|
+
Dim TargetRow As Integer
|
146
|
+
TargetRow = ListBox1.Value + 1 'Valueプロパティにはリストボックスの選択された行の1列目の値が格納されているので、ここでは商品IDが変数に格納される
|
147
|
+
Cells(TargetRow, 8).Value = 1
|
148
|
+
|
149
|
+
'リストボックスの行を削除/更新
|
150
|
+
ListBox1.RemoveItem ListBox1.ListIndex '選択されている行番号(ListBox1.ListIndex)を引数に指定して削除
|
151
|
+
End Sub
|
152
|
+
|
153
|
+
Private Sub btnUpdate_Click()
|
154
|
+
Dim Msg As String, Title As String
|
155
|
+
Msg = "修正します。よろしいですか?"
|
156
|
+
Title = "修正の確認"
|
157
|
+
|
158
|
+
Dim res As Integer
|
159
|
+
res = MsgBox(Msg, vbYesNo + vbInformation, Title)
|
160
|
+
If res = vbNo Then Exit Sub
|
161
|
+
|
162
|
+
'メイン処理(vbYes)
|
163
|
+
With ListBox1
|
164
|
+
Dim TargetIndex As Integer
|
165
|
+
TargetIndex = .ListIndex 'どの行が選択されているかを変数に格納
|
166
|
+
.List(TargetIndex, 1) = txtGoods.Text
|
167
|
+
.List(TargetIndex, 2) = cboCategory.Value
|
168
|
+
.List(TargetIndex, 3) = txtMaker.Text
|
169
|
+
.List(TargetIndex, 4) = FormatAddSpace(Format(txtPrice.Text, "#,##0"), 10)
|
170
|
+
.List(TargetIndex, 5) = txtUnit.Text
|
171
|
+
.List(TargetIndex, 6) = txtRemark.Text
|
172
|
+
End With
|
173
|
+
|
174
|
+
'シートのデータを更新
|
175
|
+
Dim TargetRow As Integer
|
176
|
+
TargetRow = CInt(txtID.Text) + 1
|
177
|
+
Cells(TargetRow, 2).Value = txtGoods.Text
|
178
|
+
Cells(TargetRow, 3).Value = cboCategory.Text
|
179
|
+
Cells(TargetRow, 4).Value = txtMaker.Text
|
180
|
+
Cells(TargetRow, 5).Value = txtPrice.Text
|
181
|
+
Cells(TargetRow, 6).Value = txtUnit.Text
|
182
|
+
Cells(TargetRow, 7).Value = txtRemark.Text
|
183
|
+
|
184
|
+
'各コントロール値のクリア
|
185
|
+
txtID.Text = ""
|
186
|
+
txtGoods.Text = ""
|
187
|
+
cboCategory.Text = ""
|
188
|
+
txtMaker.Text = ""
|
189
|
+
txtPrice.Text = ""
|
190
|
+
txtUnit.Text = ""
|
191
|
+
txtRemark.Text = ""
|
192
|
+
|
193
|
+
End Sub
|
194
|
+
|
195
|
+
Private Sub ListBox1_Change()
|
196
|
+
With ListBox1
|
197
|
+
Dim TargetRow As Integer
|
198
|
+
'現在選択されている項目を識別し、行番号を変数に格納
|
199
|
+
TargetRow = .ListIndex
|
200
|
+
|
201
|
+
'ListBox.Textには選択された行の値が格納されている(複数列の場合は1列目の値)
|
202
|
+
txtID.Text = .Text
|
203
|
+
txtGoods.Text = .List(TargetRow, 1)
|
204
|
+
cboCategory.Text = .List(TargetRow, 2)
|
205
|
+
txtMaker.Text = .List(TargetRow, 3)
|
206
|
+
txtPrice.Text = Trim(.List(TargetRow, 4)) 'スペース削除
|
207
|
+
txtUnit.Text = .List(TargetRow, 5)
|
208
|
+
txtRemark.Text = .List(TargetRow, 6)
|
209
|
+
|
210
|
+
'修正ボタンと削除ボタンを有効にする
|
211
|
+
btnUpdate.Enabled = True
|
212
|
+
btnDelete.Enabled = True
|
213
|
+
|
214
|
+
End With
|
215
|
+
End Sub
|
216
|
+
|
217
|
+
Private Sub UserForm_Initialize()
|
218
|
+
'「商品マスタ」をフォームが表示されたときに選択する
|
219
|
+
Worksheets("商品マスタ").Select
|
220
|
+
|
221
|
+
'リストボックスの設定
|
222
|
+
With ListBox1
|
223
|
+
.Font.Size = 10
|
224
|
+
.ColumnCount = 7
|
225
|
+
.ColumnWidths = "50;100;80;80;100;30;70"
|
226
|
+
.TextAlign = fmTextAlignLeft
|
227
|
+
.Font.Name = "MS ゴシック"
|
228
|
+
|
229
|
+
'リストボックスにデータを表示させる
|
230
|
+
Dim i As Integer, LastRow As Integer
|
231
|
+
LastRow = Range("A65536").End(xlUp).Row
|
232
|
+
For i = 2 To LastRow
|
233
|
+
If Cells(i, 8).Value <> 1 Then
|
234
|
+
.AddItem FormatAddSpace(Cells(i, 1).Value, 4)
|
235
|
+
.List(.ListCount - 1, 1) = Cells(i, 2).Value
|
236
|
+
.List(.ListCount - 1, 2) = Cells(i, 3).Value
|
237
|
+
.List(.ListCount - 1, 3) = Cells(i, 4).Value
|
238
|
+
.List(.ListCount - 1, 4) = FormatAddSpace(Format(Cells(i, 5).Value, "#,##0"), 10)
|
239
|
+
.List(.ListCount - 1, 5) = Cells(i, 6).Value
|
240
|
+
.List(.ListCount - 1, 6) = Cells(i, 7).Value
|
241
|
+
End If
|
242
|
+
Next
|
243
|
+
End With
|
244
|
+
|
245
|
+
'商品IDを変更不可能にする
|
246
|
+
txtID.Locked = True
|
247
|
+
|
248
|
+
'修正ボタンと削除ボタンを無効にしておく
|
249
|
+
btnUpdate.Enabled = False
|
250
|
+
btnDelete.Enabled = False
|
251
|
+
End Sub
|
117
252
|
```
|
1
title
CHANGED
File without changes
|
body
CHANGED
@@ -113,4 +113,5 @@
|
|
113
113
|
|
114
114
|
Private Sub btnClose_Click()
|
115
115
|
Unload Me
|
116
|
-
End Sub
|
116
|
+
End Sub
|
117
|
+
```
|