回答編集履歴
1
サンプルコードを追加しました。
answer
CHANGED
@@ -12,4 +12,124 @@
|
|
12
12
|

|
13
13
|
|
14
14
|
どうしましょう?
|
15
|
-
まっさらから(作り直し)でも良ければ、私から情報を出す事ができそうですが
|
15
|
+
まっさらから(作り直し)でも良ければ、私から情報を出す事ができそうですが
|
16
|
+
|
17
|
+
---
|
18
|
+
|
19
|
+
追記 2015/11/24
|
20
|
+
単純に目的の機能を満足するだろうサンプルを書いてみました。
|
21
|
+
以下コードの「SetImageMain関数」へ引数として
|
22
|
+
・1つ以上の配置先となるセル範囲(オペレーション的には多分Application.Selection)
|
23
|
+
・イメージファイルの一覧(フルパス文字列)
|
24
|
+
を渡してあげれば良いです。
|
25
|
+
```VBA
|
26
|
+
Option Explicit
|
27
|
+
|
28
|
+
'<summary>
|
29
|
+
' 指定されたセルへイメージを配置
|
30
|
+
'</summary>
|
31
|
+
'<param name="targetRng">処理対象全体の範囲</param>
|
32
|
+
'<param name="imageFiles">イメージファイル(配列)</param>
|
33
|
+
Sub SetImageMain(targetRng As Range, imageFiles() As String)
|
34
|
+
On Error Resume Next
|
35
|
+
Err.Clear
|
36
|
+
|
37
|
+
Dim FileSysObj As Object 'Scripting.FileSystemObject
|
38
|
+
Dim targetSht As Worksheet '対象のワークシート
|
39
|
+
Dim targetCell As Range '処理対象の範囲(セル)
|
40
|
+
Dim strMergeAreaOld As String '前回の処理範囲(結合セル)を記憶
|
41
|
+
Dim imgNameRng As Range 'イメージ名を設定するセル
|
42
|
+
Dim fileIdx As Integer 'ファイルのインデックス
|
43
|
+
Dim retVal As Boolean 'SetImage関数の戻り値受け取り用
|
44
|
+
|
45
|
+
'ファイル名から名前部分を取得するのに利用
|
46
|
+
Set FileSysObj = CreateObject("Scripting.FileSystemObject")
|
47
|
+
|
48
|
+
strMergeAreaOld = ""
|
49
|
+
fileIdx = LBound(imageFiles) 'ファイル一覧(配列)の最初の位置を取得
|
50
|
+
|
51
|
+
'対象のワークシートを取得
|
52
|
+
Set targetSht = targetRng.Parent
|
53
|
+
|
54
|
+
For Each targetCell In targetRng
|
55
|
+
|
56
|
+
With targetSht.Range(targetCell.Address)
|
57
|
+
|
58
|
+
'前回の範囲と一致する場合は次へ
|
59
|
+
If strMergeAreaOld = .MergeArea.Address Then
|
60
|
+
GoTo CONTINUE_FOR
|
61
|
+
End If
|
62
|
+
|
63
|
+
'イメージ配置の関数を呼び出し
|
64
|
+
retVal = SetImage(.MergeArea, imageFiles(fileIdx))
|
65
|
+
|
66
|
+
'隣接するセルへファイル名の名前部分を記入
|
67
|
+
Set imgNameRng = targetSht.Cells(targetCell.Row + .MergeArea.Rows.Count, targetCell.Column)
|
68
|
+
imgNameRng = "'" + FileSysObj.GetBaseName(imageFiles(fileIdx))
|
69
|
+
|
70
|
+
fileIdx = fileIdx + 1 '次のイメージへ
|
71
|
+
|
72
|
+
If fileIdx > UBound(imageFiles) Then 'イメージリストの終端に到達
|
73
|
+
Exit For
|
74
|
+
End If
|
75
|
+
|
76
|
+
strMergeAreaOld = .MergeArea.Address '今回の範囲を記憶
|
77
|
+
|
78
|
+
End With
|
79
|
+
CONTINUE_FOR:
|
80
|
+
Next targetCell
|
81
|
+
|
82
|
+
Set FileSysObj = Nothing
|
83
|
+
End Sub
|
84
|
+
|
85
|
+
|
86
|
+
'<summary>
|
87
|
+
' イメージを配置する
|
88
|
+
'</summary>
|
89
|
+
'<param name="mergeCell">配置対象のセル(結合)</param>
|
90
|
+
'<param name="imageFile">イメージのファイル名</param>
|
91
|
+
Function SetImage(ByRef mergeCell As Range, imageFile As String) As Boolean
|
92
|
+
On Error Resume Next
|
93
|
+
Err.Clear
|
94
|
+
|
95
|
+
Dim WSht As Worksheet 'イメージ配置の対称Worksheet
|
96
|
+
Dim Img As Shape '配置したイメージ
|
97
|
+
Dim rWidth As Double 'セルの幅とイメージ幅の比
|
98
|
+
Dim rHeight As Double 'セルの高さとイメージ高さの比
|
99
|
+
Dim ScaleVal As Double 'サイズ調整用のスケール値
|
100
|
+
|
101
|
+
SetImage = False
|
102
|
+
|
103
|
+
'対象のワークシートを取得
|
104
|
+
Set WSht = mergeCell.Parent
|
105
|
+
|
106
|
+
'イメージを配置
|
107
|
+
Set Img = WSht.Shapes.AddPicture(fileName:=imageFile, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=mergeCell.Left, Top:=mergeCell.Top, width:=0, height:=0)
|
108
|
+
|
109
|
+
Img.ScaleWidth 1, msoTrue '幅を元のサイズに
|
110
|
+
Img.ScaleHeight 1, msoTrue '高さを元のサイズに
|
111
|
+
Img.LockAspectRatio = msoTrue '縦横比を固定
|
112
|
+
|
113
|
+
|
114
|
+
rWidth = mergeCell.width / Img.width
|
115
|
+
rHeight = mergeCell.height / Img.height
|
116
|
+
|
117
|
+
If rWidth < rHeight Then
|
118
|
+
ScaleVal = rWidth
|
119
|
+
Else
|
120
|
+
ScaleVal = rHeight
|
121
|
+
End If
|
122
|
+
|
123
|
+
'ScaleValが1未満(セルに収まらない)場合に縮小
|
124
|
+
If ScaleVal < 1# Then
|
125
|
+
Img.width = Img.width * ScaleVal '縦横比固定なのでWidthかHeightいずれかを指定すれば良い
|
126
|
+
End If
|
127
|
+
|
128
|
+
'中央に配置
|
129
|
+
Img.Top = mergeCell.Top + (mergeCell.height - Img.height) / 2
|
130
|
+
Img.Left = mergeCell.Left + (mergeCell.width - Img.width) / 2
|
131
|
+
|
132
|
+
If Err.Number = 0 Then SetImage = True 'エラーがなければTrueを返す
|
133
|
+
End Function
|
134
|
+
```
|
135
|
+
こちらのコードで機能的には大丈夫だろうと思います。
|