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

回答編集履歴

2

エラー処理追加

2016/09/08 04:23

投稿

ttyp03
ttyp03

スコア17002

answer CHANGED
@@ -18,4 +18,42 @@
18
18
  r = r + 1
19
19
  Wend
20
20
  End Sub
21
+ ```
22
+
23
+ ---
24
+ エラー処理追加1(エラーを無視する)
25
+ ```VBA
26
+ Sub Macro()
27
+ On Error Resume Next ' <===これを追加
28
+ Dim r As Long
29
+ r = 2
30
+ While Cells(r, 7) <> ""
31
+ Set Picture = ActiveSheet.Shapes.AddPicture( _
32
+ Filename:=Cells(r, 7), _
33
+ LinkToFile:=False, SaveWithDocument:=True, _
34
+ Left:=Cells(r, 1).Left, Top:=Cells(r, 1).Top, _
35
+ Width:=50, Height:=50)
36
+ r = r + 1
37
+ Wend
38
+ End Sub
39
+ ```
40
+ エラー処理追加2(エラー情報を取得する)
41
+ ```VBA
42
+ Sub Macro()
43
+ On Error Goto ErrHandle ' <===これを追加
44
+ Dim r As Long
45
+ r = 2
46
+ While Cells(r, 7) <> ""
47
+ Set Picture = ActiveSheet.Shapes.AddPicture( _
48
+ Filename:=Cells(r, 7), _
49
+ LinkToFile:=False, SaveWithDocument:=True, _
50
+ Left:=Cells(r, 1).Left, Top:=Cells(r, 1).Top, _
51
+ Width:=50, Height:=50)
52
+ r = r + 1
53
+ Wend
54
+ Exit Sub ' <==== ここから先を追加
55
+ ErrHandle:
56
+ MsgBox Err.Description
57
+ ' Resume Next ' 処理を継続する場合はこの行を有効化
58
+ End Sub
21
59
  ```

1

追加

2016/09/08 04:23

投稿

ttyp03
ttyp03

スコア17002

answer CHANGED
@@ -1,2 +1,21 @@
1
1
  こちらの質問が参考になるかも。
2
- [https://teratail.com/questions/40321](https://teratail.com/questions/40321)
2
+ [https://teratail.com/questions/40321](https://teratail.com/questions/40321)
3
+
4
+ ---
5
+ 上記質問に書いた自分のコードの修正版です。
6
+ お試しください。
7
+ 但し、Macでは未検証です。
8
+ ```VBA
9
+ Sub Macro()
10
+ Dim r As Long
11
+ r = 2
12
+ While Cells(r, 7) <> ""
13
+ Set Picture = ActiveSheet.Shapes.AddPicture( _
14
+ Filename:=Cells(r, 7), _
15
+ LinkToFile:=False, SaveWithDocument:=True, _
16
+ Left:=Cells(r, 1).Left, Top:=Cells(r, 1).Top, _
17
+ Width:=50, Height:=50)
18
+ r = r + 1
19
+ Wend
20
+ End Sub
21
+ ```