sadsack5000
New Member
- Joined
- Jun 7, 2017
- Messages
- 3
Hello all,
I've scoured the internet and pieced together some code to help in my task of developing an inspection report template. I know nothing about VBA so the below is probably totally wrong.
As part of the report im trying to create a photos page where the user clicks on a cell, the insert picture dialogue appears and once the photo is selected it is imported into the same cell and resized (keeping aspect ratio) to the extents of the cell. The photos template i have created contains eight boxes for photos made up from merged cells.
I have partially got it working by scraping around the internet / trial and error.
First i made this macro as a module in the VBA side
Then i added this code into the "View code" section of the photos sheet which tells excel to run the above when any of my merged cells are double clicked:
However every time i double click one of my photo "frames" the picture is inserted into cell A1 which is incorrect.
Where am i going wrong?
Thanks in advance
I've scoured the internet and pieced together some code to help in my task of developing an inspection report template. I know nothing about VBA so the below is probably totally wrong.
As part of the report im trying to create a photos page where the user clicks on a cell, the insert picture dialogue appears and once the photo is selected it is imported into the same cell and resized (keeping aspect ratio) to the extents of the cell. The photos template i have created contains eight boxes for photos made up from merged cells.
I have partially got it working by scraping around the internet / trial and error.
First i made this macro as a module in the VBA side
Code:
Sub InsertPictures()
Dim Pict As Variant
Dim ImgFileFormat As String
Dim rngPict As Range
Dim lLoop As Long
'ActiveSheet.Protect False, False, False, False, False
'ImgFileFormat = "Image Files gif (*.gif),*.gif,(*.jpg), others, tif (*.tif),*.tif"
Pict = Application.GetOpenFilename(ImgFileFormat, MultiSelect:=True)
'Note you can load in any nearly file format
If Not IsArray(Pict) Then
Debug.Print "No files selected."
Exit Sub
End If
Application.ScreenUpdating = False
For lLoop = LBound(Pict) To UBound(Pict)
Set rngPict = Cells(lLoop, "A")
With ActiveSheet.Pictures.Insert(Pict(lLoop))
.Left = rngPict.Left
.Top = rngPict.Top
If .Width / .Height > rngPict.Width / rngPict.Height Then
.Height = .Height * rngPict.Width / .Width
.Top = .Top + (rngPict.Height - .Height) / 2
.Width = rngPict.Width
Else
.Width = .Width * rngPict.Height / .Height
.Left = .Left + (rngPict.Width - .Width) / 2
.Height = rngPict.Height
End If
End With
Next lLoop
Application.ScreenUpdating = True
End Sub
Then i added this code into the "View code" section of the photos sheet which tells excel to run the above when any of my merged cells are double clicked:
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Target.Cells(1).Address = "$B$3" Or Target.Cells(1).Address = "$S$3" Or Target.Cells(1).Address = "$B$15" Or Target.Cells(1).Address = "$S$15" Or Target.Cells(1).Address = "$B$27" Or Target.Cells(1).Address = "$S$27" Or Target.Cells(1).Address = "$B$39" Or Target.Cells(1).Address = "$S$39" Then
Cancel = True
Call InsertPictures
End If
End Sub
However every time i double click one of my photo "frames" the picture is inserted into cell A1 which is incorrect.
Where am i going wrong?
Thanks in advance