2016-10-10 3 views
1

나는 잠시 동안이 문제를 해결하려고 노력해 왔습니다. 다음 코드는 선택에서 내 Excel 문서로 그림을 삽입합니다. 셀 B10에 그림을 배치하고 병합 된 셀 중 하나의 높이로 크기를 조정합니다. 이제 문제는 내가 센터를 얻을 수 없다는 것입니다. 나는 위의 라인 병합 된 셀의 VBA 센터 그림

.Left = 35# 

수동으로 하나 개의 화면을 가운데 수 있습니다,하지만 난 다른 폭의와 다른 모든 사진이 aswell centerd되고 싶어요. 누구든지이 문제를 도와 줄 수 있습니까? 아래 코드는 제가 사용해온 것입니다. 미리 감사드립니다!

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

관련 문제