重要なお知らせ

「教えて! goo」は2025年9月17日(水)をもちまして、サービスを終了いたします。詳細はこちら>

電子書籍の厳選無料作品が豊富!

質問させて下さい。

エクセルで会社で写真台帳を作成しています。

このマクロは以前、会社にいた人が作ったらしいのですが、その後退職してしまい、だれもマクロの内容が分からず困っています。

よろしくお願いします。


【資料】を添しましたので、どの場所をどのように変更すればよいのか、教えて下さい。

【順番】・・・縦3枚横2枚のA3の写真台紙で下記の順番になるように画像を貼付たい

  A   B   C   D   E   F   G   H
1    画像1    コメント欄   コント欄    画像4
2
3
4    画像2    コメント欄   コメント欄    画像5
5
6
7    画像3    コメント欄   コメント欄    画像6
8
9
10    画像7    コメント欄   コメント欄    画像9


・      ・・・      ・・・      ・・・      ・・・
・ 
100


【資料】


Sub 画像挿入()

Dim strFilter As String
Dim Filenames As Variant
Dim Pic As Picture

' 「ファイルを開く」ダイアログでファイル名を取得
strFilter = "画像ファイル(*.jpg;*.jpeg;*.gif;*.bmp;*.png),*.jpg;*.jpeg;*.gif;*.bmp;*.png"
Filenames = Application.GetOpenFilename( _
FileFilter:=strFilter, _
Title:="図の挿入(複数選択可)", _
MultiSelect:=True)
If Not IsArray(Filenames) Then Exit Sub

' ファイル名をソート
Call BubbleSort_Str(Filenames, True, vbTextCompare)

' 貼り付け開始セルを選択


' マクロ実行中の画面描写を停止
Application.ScreenUpdating = False
' 順番に画像を挿入

j = -1
For i = LBound(Filenames) To UBound(Filenames)
Set Pic = ActiveSheet.Pictures.Insert(Filenames(i))
j = j + 1

'-------------------------------------------------------------
' 画像の各種プロパティ変更
'-------------------------------------------------------------
With Pic
.Top = ActiveCell.Top ' 位置:アクティブセルの上側に重ねる
.Left = ActiveCell.Left ' 位置:アクティブセルの左側に重ねる
.Placement = xlMove ' 移動するがサイズ変更しない
.PrintObject = True ' 印刷する
End With
With Pic.ShapeRange
.LockAspectRatio = msoFalse ' 縦横比維持
' 画像の幅をアクティブセルにあわせる
' 結合セルの場合でも対応
.Height = ActiveCell.MergeArea.Height 'Height:高さに合わせる場合
.Width = ActiveCell.MergeArea.Width 'Width:幅に合わせる場合
End With

' 次の貼り付け先を選択(アクティブセルにする)[例:5個下のセル]
If j Mod 2 = 0 Then

ActiveCell.Offset(15, 0).Select


Else
ActiveCell.Offset(0, 13).Select

End If


Set Pic = Nothing
Next i

' 終了
Application.ScreenUpdating = True
MsgBox i - 1 & "枚の画像を挿入しました", vbInformation

End Sub


バブルソート(文字列)
Private Sub BubbleSort_Str( _
ByRef Source As Variant, _
Optional ByVal SortAsc As Boolean = True, _
Optional ByVal Compare As VbCompareMethod = vbTextCompare)

If Not IsArray(Source) Then Exit Sub

Dim i As Long, j As Long
Dim vntTmp As Variant
For i = LBound(Source) To UBound(Source) - 1
For j = LBound(Source) To LBound(Source) + UBound(Source) - i - 1
If StrComp(Source(IIf(SortAsc, j, j + 1)), _
Source(IIf(SortAsc, j + 1, j)), Compare) = 1 Then
vntTmp = Source(j)
Source(j) = Source(j + 1)
Source(j + 1) = vntTmp
End If
Next j
Next i

End Sub

A 回答 (1件)

' 次の貼り付け先を選択(アクティブセルにする)[例:5個下のセル]


If j Mod 2 = 0 Then
   ActiveCell.Offset(15, 0).Select
 Else
   ActiveCell.Offset(0, 13).Select
 End If



※ 上記の部分で、次の貼り付け位置を決めています。

  この例では、2箇所なので、 j Mod 2 で条件を選んでいます。

  お尋ねの事例では、6箇所に増えるので


If  j  Mod  6  =  0  Then
   ActiveCell.Offset(15, 0).Select
 Elseif  j  Mod  6  =  1 Then
   ActiveCell.Offset(15, 0).Select
 Elseif  j  Mod  6  =  2 Then
   ActiveCell.Offset(-30, 13).Select
 Elseif  j  Mod  6  =  3 Then
   ActiveCell.Offset(15, 0).Select
 Elseif  j  Mod  6  =  4 Then
   ActiveCell.Offset(15, 0).Select
 Elseif  j  Mod  6  =  5 Then
   ActiveCell.Offset(15, -13).Select
End If



こんな感じでしょうか?
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!