回答編集履歴

1

サンプルコードを追加しました。

2015/11/23 21:55

投稿

sgr-2
sgr-2

スコア294

test CHANGED
@@ -27,3 +27,243 @@
27
27
  どうしましょう?
28
28
 
29
29
  まっさらから(作り直し)でも良ければ、私から情報を出す事ができそうですが
30
+
31
+
32
+
33
+ ---
34
+
35
+
36
+
37
+ 追記 2015/11/24
38
+
39
+ 単純に目的の機能を満足するだろうサンプルを書いてみました。
40
+
41
+ 以下コードの「SetImageMain関数」へ引数として
42
+
43
+ ・1つ以上の配置先となるセル範囲(オペレーション的には多分Application.Selection)
44
+
45
+ ・イメージファイルの一覧(フルパス文字列)
46
+
47
+ を渡してあげれば良いです。
48
+
49
+ ```VBA
50
+
51
+ Option Explicit
52
+
53
+
54
+
55
+ '<summary>
56
+
57
+ ' 指定されたセルへイメージを配置
58
+
59
+ '</summary>
60
+
61
+ '<param name="targetRng">処理対象全体の範囲</param>
62
+
63
+ '<param name="imageFiles">イメージファイル(配列)</param>
64
+
65
+ Sub SetImageMain(targetRng As Range, imageFiles() As String)
66
+
67
+ On Error Resume Next
68
+
69
+ Err.Clear
70
+
71
+
72
+
73
+ Dim FileSysObj As Object 'Scripting.FileSystemObject
74
+
75
+ Dim targetSht As Worksheet '対象のワークシート
76
+
77
+ Dim targetCell As Range '処理対象の範囲(セル)
78
+
79
+ Dim strMergeAreaOld As String '前回の処理範囲(結合セル)を記憶
80
+
81
+ Dim imgNameRng As Range 'イメージ名を設定するセル
82
+
83
+ Dim fileIdx As Integer 'ファイルのインデックス
84
+
85
+ Dim retVal As Boolean 'SetImage関数の戻り値受け取り用
86
+
87
+
88
+
89
+ 'ファイル名から名前部分を取得するのに利用
90
+
91
+ Set FileSysObj = CreateObject("Scripting.FileSystemObject")
92
+
93
+
94
+
95
+ strMergeAreaOld = ""
96
+
97
+ fileIdx = LBound(imageFiles) 'ファイル一覧(配列)の最初の位置を取得
98
+
99
+
100
+
101
+ '対象のワークシートを取得
102
+
103
+ Set targetSht = targetRng.Parent
104
+
105
+
106
+
107
+ For Each targetCell In targetRng
108
+
109
+
110
+
111
+ With targetSht.Range(targetCell.Address)
112
+
113
+
114
+
115
+ '前回の範囲と一致する場合は次へ
116
+
117
+ If strMergeAreaOld = .MergeArea.Address Then
118
+
119
+ GoTo CONTINUE_FOR
120
+
121
+ End If
122
+
123
+
124
+
125
+ 'イメージ配置の関数を呼び出し
126
+
127
+ retVal = SetImage(.MergeArea, imageFiles(fileIdx))
128
+
129
+
130
+
131
+ '隣接するセルへファイル名の名前部分を記入
132
+
133
+ Set imgNameRng = targetSht.Cells(targetCell.Row + .MergeArea.Rows.Count, targetCell.Column)
134
+
135
+ imgNameRng = "'" + FileSysObj.GetBaseName(imageFiles(fileIdx))
136
+
137
+
138
+
139
+ fileIdx = fileIdx + 1 '次のイメージへ
140
+
141
+
142
+
143
+ If fileIdx > UBound(imageFiles) Then 'イメージリストの終端に到達
144
+
145
+ Exit For
146
+
147
+ End If
148
+
149
+
150
+
151
+ strMergeAreaOld = .MergeArea.Address '今回の範囲を記憶
152
+
153
+
154
+
155
+ End With
156
+
157
+ CONTINUE_FOR:
158
+
159
+ Next targetCell
160
+
161
+
162
+
163
+ Set FileSysObj = Nothing
164
+
165
+ End Sub
166
+
167
+
168
+
169
+
170
+
171
+ '<summary>
172
+
173
+ ' イメージを配置する
174
+
175
+ '</summary>
176
+
177
+ '<param name="mergeCell">配置対象のセル(結合)</param>
178
+
179
+ '<param name="imageFile">イメージのファイル名</param>
180
+
181
+ Function SetImage(ByRef mergeCell As Range, imageFile As String) As Boolean
182
+
183
+ On Error Resume Next
184
+
185
+ Err.Clear
186
+
187
+
188
+
189
+ Dim WSht As Worksheet 'イメージ配置の対称Worksheet
190
+
191
+ Dim Img As Shape '配置したイメージ
192
+
193
+ Dim rWidth As Double 'セルの幅とイメージ幅の比
194
+
195
+ Dim rHeight As Double 'セルの高さとイメージ高さの比
196
+
197
+ Dim ScaleVal As Double 'サイズ調整用のスケール値
198
+
199
+
200
+
201
+ SetImage = False
202
+
203
+
204
+
205
+ '対象のワークシートを取得
206
+
207
+ Set WSht = mergeCell.Parent
208
+
209
+
210
+
211
+ 'イメージを配置
212
+
213
+ Set Img = WSht.Shapes.AddPicture(fileName:=imageFile, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=mergeCell.Left, Top:=mergeCell.Top, width:=0, height:=0)
214
+
215
+
216
+
217
+ Img.ScaleWidth 1, msoTrue '幅を元のサイズに
218
+
219
+ Img.ScaleHeight 1, msoTrue '高さを元のサイズに
220
+
221
+ Img.LockAspectRatio = msoTrue '縦横比を固定
222
+
223
+
224
+
225
+
226
+
227
+ rWidth = mergeCell.width / Img.width
228
+
229
+ rHeight = mergeCell.height / Img.height
230
+
231
+
232
+
233
+ If rWidth < rHeight Then
234
+
235
+ ScaleVal = rWidth
236
+
237
+ Else
238
+
239
+ ScaleVal = rHeight
240
+
241
+ End If
242
+
243
+
244
+
245
+ 'ScaleValが1未満(セルに収まらない)場合に縮小
246
+
247
+ If ScaleVal < 1# Then
248
+
249
+ Img.width = Img.width * ScaleVal '縦横比固定なのでWidthかHeightいずれかを指定すれば良い
250
+
251
+ End If
252
+
253
+
254
+
255
+ '中央に配置
256
+
257
+ Img.Top = mergeCell.Top + (mergeCell.height - Img.height) / 2
258
+
259
+ Img.Left = mergeCell.Left + (mergeCell.width - Img.width) / 2
260
+
261
+
262
+
263
+ If Err.Number = 0 Then SetImage = True 'エラーがなければTrueを返す
264
+
265
+ End Function
266
+
267
+ ```
268
+
269
+ こちらのコードで機能的には大丈夫だろうと思います。