2016-10-10 7 views
1

私はこの問題をしばらく解決しようとしています。次のコードは、選択した画像をExcel文書に挿入します。セルB10に画像を配置し、マージしたセルの高さにサイズを変更します。今問題は、私はそれがcenterdになることができないということです。VBA中央の画像が結合されたセル

上記の行では、手動で1つの画像を中央に置くことができますが、他のすべての画像を中心にすることもできます。誰もこの問題で私を助けることができますか?以下のコードは私が使ってきたものです。前もって感謝します!

Sub Insert_Pic_Section_One() 

Dim fileName1 As Variant 

fileName1 = Application.GetOpenFilename(filefilter:="Tiff Files(*.tif;*.tiff),*.tif;*.tiff,JPEG Files (*.jpg;*.jpeg;*.jfif;*.jpe),*.jpg;*.jpeg;*.jfif;*.jpe,Bitmap Files(*.bmp),*.bmp", FilterIndex:=2, Title:="Choose picture", MultiSelect:=False) 

If fileName1 = False Then 
Exit Sub 
Else 
ActiveWorkbook.ActiveSheet.Select 
Range("B10").Select 
Dim picture1 As Object 
Set picture1 = ActiveWorkbook.ActiveSheet.Pictures.Insert(fileName1) 

    With picture1 
    .Top = .Top 
    .Left = 35# 
    .Width = .Width 
    .Height = 233# 
    End With 

End If 

End Sub 
+1

何を中心に?どうやって35歳になったのですか?それは、それぞれの写真のためのいくつかの算術のように聞こえるはずです。 – arcadeprecinct

答えて

0

何も選択する必要はありません。マージされたセルを使用するため、.MergeAreaを使用する必要があります。そうしないと、マージされていない行と列の高さと幅しか得られません。

Dim ws As Worksheet 
Dim targetCell As Range 
Dim picture1 As Picture 

Set ws = ActiveSheet 'replace with actual worksheet if possible 
Set targetCell = ws.Range("B10") 
Set picture1 = ws.Pictures.Insert(fileName1) 

With picture1 
    .Height = targetCell.MergeArea.Height 'set height first because width will change 
    .Top = targetCell.Top 
    .Left = targetCell.Left + (targetCell.MergeArea.Width - .Width)/2 
End With 
+0

ありがとう、それは働いた! –

+0

@SjoerdEemanあなたは答えを受け入れることができます;) – arcadeprecinct