質問編集履歴
3
書式改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -34,7 +34,7 @@
|
|
34
34
|
|
35
35
|
|
36
36
|
|
37
|
-
※追記
|
37
|
+
### ※追記
|
38
38
|
|
39
39
|
2番目のソースコードを参考に今回のコードを書いたのですが、そちらではChangeイベントが発生しないまま進みます。
|
40
40
|
|
2
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -32,6 +32,18 @@
|
|
32
32
|
|
33
33
|
よろしくお願いいたします。
|
34
34
|
|
35
|
+
|
36
|
+
|
37
|
+
※追記
|
38
|
+
|
39
|
+
2番目のソースコードを参考に今回のコードを書いたのですが、そちらではChangeイベントが発生しないまま進みます。
|
40
|
+
|
41
|
+
何の違いがあってこのようなことが起こるのか、教えていただきたいです。
|
42
|
+
|
43
|
+
よろしくお願いいたします。
|
44
|
+
|
45
|
+
|
46
|
+
|
35
47
|
### 該当のソースコード
|
36
48
|
|
37
49
|
|
@@ -231,3 +243,261 @@
|
|
231
243
|
End Sub
|
232
244
|
|
233
245
|
```
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
### Changeイベントが発生せずに進むコード
|
250
|
+
|
251
|
+
```
|
252
|
+
|
253
|
+
Option Explicit
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
Private Sub btnClose_Click()
|
258
|
+
|
259
|
+
Unload Me
|
260
|
+
|
261
|
+
End Sub
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
Private Sub btnDelete_Click()
|
266
|
+
|
267
|
+
Dim strMsg As String
|
268
|
+
|
269
|
+
Dim strTitle As String
|
270
|
+
|
271
|
+
Dim res As Integer 'MsgBoxの戻り値を格納
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
strMsg = ListBox1.List(ListBox1.ListIndex, 1) 'リストボックスで選択された値の2列目(第2引数「1」)の値を変数に格納
|
276
|
+
|
277
|
+
strMsg = strMsg & " を削除します。よろしいですか?"
|
278
|
+
|
279
|
+
strTitle = "削除の確認"
|
280
|
+
|
281
|
+
res = MsgBox(strMsg, vbYesNo + vbExclamation, strTitle)
|
282
|
+
|
283
|
+
If res = vbNo Then Exit Sub
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
'削除の処理
|
288
|
+
|
289
|
+
Dim TargetRow As Integer
|
290
|
+
|
291
|
+
TargetRow = ListBox1.Value + 1 'Valueプロパティにはリストボックスの選択された行の1列目の値が格納されているので、ここでは商品IDが変数に格納される
|
292
|
+
|
293
|
+
Cells(TargetRow, 8).Value = 1
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
'リストボックスの行を削除/更新
|
298
|
+
|
299
|
+
ListBox1.RemoveItem ListBox1.ListIndex '選択されている行番号(ListBox1.ListIndex)を引数に指定して削除
|
300
|
+
|
301
|
+
End Sub
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
Private Sub btnUpdate_Click()
|
306
|
+
|
307
|
+
Dim Msg As String, Title As String
|
308
|
+
|
309
|
+
Msg = "修正します。よろしいですか?"
|
310
|
+
|
311
|
+
Title = "修正の確認"
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
Dim res As Integer
|
316
|
+
|
317
|
+
res = MsgBox(Msg, vbYesNo + vbInformation, Title)
|
318
|
+
|
319
|
+
If res = vbNo Then Exit Sub
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
'メイン処理(vbYes)
|
324
|
+
|
325
|
+
With ListBox1
|
326
|
+
|
327
|
+
Dim TargetIndex As Integer
|
328
|
+
|
329
|
+
TargetIndex = .ListIndex 'どの行が選択されているかを変数に格納
|
330
|
+
|
331
|
+
.List(TargetIndex, 1) = txtGoods.Text
|
332
|
+
|
333
|
+
.List(TargetIndex, 2) = cboCategory.Value
|
334
|
+
|
335
|
+
.List(TargetIndex, 3) = txtMaker.Text
|
336
|
+
|
337
|
+
.List(TargetIndex, 4) = FormatAddSpace(Format(txtPrice.Text, "#,##0"), 10)
|
338
|
+
|
339
|
+
.List(TargetIndex, 5) = txtUnit.Text
|
340
|
+
|
341
|
+
.List(TargetIndex, 6) = txtRemark.Text
|
342
|
+
|
343
|
+
End With
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
'シートのデータを更新
|
348
|
+
|
349
|
+
Dim TargetRow As Integer
|
350
|
+
|
351
|
+
TargetRow = CInt(txtID.Text) + 1
|
352
|
+
|
353
|
+
Cells(TargetRow, 2).Value = txtGoods.Text
|
354
|
+
|
355
|
+
Cells(TargetRow, 3).Value = cboCategory.Text
|
356
|
+
|
357
|
+
Cells(TargetRow, 4).Value = txtMaker.Text
|
358
|
+
|
359
|
+
Cells(TargetRow, 5).Value = txtPrice.Text
|
360
|
+
|
361
|
+
Cells(TargetRow, 6).Value = txtUnit.Text
|
362
|
+
|
363
|
+
Cells(TargetRow, 7).Value = txtRemark.Text
|
364
|
+
|
365
|
+
|
366
|
+
|
367
|
+
'各コントロール値のクリア
|
368
|
+
|
369
|
+
txtID.Text = ""
|
370
|
+
|
371
|
+
txtGoods.Text = ""
|
372
|
+
|
373
|
+
cboCategory.Text = ""
|
374
|
+
|
375
|
+
txtMaker.Text = ""
|
376
|
+
|
377
|
+
txtPrice.Text = ""
|
378
|
+
|
379
|
+
txtUnit.Text = ""
|
380
|
+
|
381
|
+
txtRemark.Text = ""
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
End Sub
|
386
|
+
|
387
|
+
|
388
|
+
|
389
|
+
Private Sub ListBox1_Change()
|
390
|
+
|
391
|
+
With ListBox1
|
392
|
+
|
393
|
+
Dim TargetRow As Integer
|
394
|
+
|
395
|
+
'現在選択されている項目を識別し、行番号を変数に格納
|
396
|
+
|
397
|
+
TargetRow = .ListIndex
|
398
|
+
|
399
|
+
|
400
|
+
|
401
|
+
'ListBox.Textには選択された行の値が格納されている(複数列の場合は1列目の値)
|
402
|
+
|
403
|
+
txtID.Text = .Text
|
404
|
+
|
405
|
+
txtGoods.Text = .List(TargetRow, 1)
|
406
|
+
|
407
|
+
cboCategory.Text = .List(TargetRow, 2)
|
408
|
+
|
409
|
+
txtMaker.Text = .List(TargetRow, 3)
|
410
|
+
|
411
|
+
txtPrice.Text = Trim(.List(TargetRow, 4)) 'スペース削除
|
412
|
+
|
413
|
+
txtUnit.Text = .List(TargetRow, 5)
|
414
|
+
|
415
|
+
txtRemark.Text = .List(TargetRow, 6)
|
416
|
+
|
417
|
+
|
418
|
+
|
419
|
+
'修正ボタンと削除ボタンを有効にする
|
420
|
+
|
421
|
+
btnUpdate.Enabled = True
|
422
|
+
|
423
|
+
btnDelete.Enabled = True
|
424
|
+
|
425
|
+
|
426
|
+
|
427
|
+
End With
|
428
|
+
|
429
|
+
End Sub
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
Private Sub UserForm_Initialize()
|
434
|
+
|
435
|
+
'「商品マスタ」をフォームが表示されたときに選択する
|
436
|
+
|
437
|
+
Worksheets("商品マスタ").Select
|
438
|
+
|
439
|
+
|
440
|
+
|
441
|
+
'リストボックスの設定
|
442
|
+
|
443
|
+
With ListBox1
|
444
|
+
|
445
|
+
.Font.Size = 10
|
446
|
+
|
447
|
+
.ColumnCount = 7
|
448
|
+
|
449
|
+
.ColumnWidths = "50;100;80;80;100;30;70"
|
450
|
+
|
451
|
+
.TextAlign = fmTextAlignLeft
|
452
|
+
|
453
|
+
.Font.Name = "MS ゴシック"
|
454
|
+
|
455
|
+
|
456
|
+
|
457
|
+
'リストボックスにデータを表示させる
|
458
|
+
|
459
|
+
Dim i As Integer, LastRow As Integer
|
460
|
+
|
461
|
+
LastRow = Range("A65536").End(xlUp).Row
|
462
|
+
|
463
|
+
For i = 2 To LastRow
|
464
|
+
|
465
|
+
If Cells(i, 8).Value <> 1 Then
|
466
|
+
|
467
|
+
.AddItem FormatAddSpace(Cells(i, 1).Value, 4)
|
468
|
+
|
469
|
+
.List(.ListCount - 1, 1) = Cells(i, 2).Value
|
470
|
+
|
471
|
+
.List(.ListCount - 1, 2) = Cells(i, 3).Value
|
472
|
+
|
473
|
+
.List(.ListCount - 1, 3) = Cells(i, 4).Value
|
474
|
+
|
475
|
+
.List(.ListCount - 1, 4) = FormatAddSpace(Format(Cells(i, 5).Value, "#,##0"), 10)
|
476
|
+
|
477
|
+
.List(.ListCount - 1, 5) = Cells(i, 6).Value
|
478
|
+
|
479
|
+
.List(.ListCount - 1, 6) = Cells(i, 7).Value
|
480
|
+
|
481
|
+
End If
|
482
|
+
|
483
|
+
Next
|
484
|
+
|
485
|
+
End With
|
486
|
+
|
487
|
+
|
488
|
+
|
489
|
+
'商品IDを変更不可能にする
|
490
|
+
|
491
|
+
txtID.Locked = True
|
492
|
+
|
493
|
+
|
494
|
+
|
495
|
+
'修正ボタンと削除ボタンを無効にしておく
|
496
|
+
|
497
|
+
btnUpdate.Enabled = False
|
498
|
+
|
499
|
+
btnDelete.Enabled = False
|
500
|
+
|
501
|
+
End Sub
|
502
|
+
|
503
|
+
```
|
1
test
CHANGED
File without changes
|
test
CHANGED
@@ -229,3 +229,5 @@
|
|
229
229
|
Unload Me
|
230
230
|
|
231
231
|
End Sub
|
232
|
+
|
233
|
+
```
|