dimitrilouwet
New Member
- Joined
- Mar 14, 2024
- Messages
- 5
- Office Version
- 365
- Platform
- Windows
We work with content exports that need validation. A picture helps, so we created a tool for this using ActiveSheet.Shapes.AddPicture
(ActiveSheet.Pictures.Insert is no good, because this only works on own computer)
On the first lines all seems ok. But after a given amount of rows, we see that the images are not inline with the cells anymore. See image enclosed.
Also note I use a fixed height of 40 here. Eventually the idea is to make this a variable input between 20 and 100.
Hope someone can point me in the right direction. Thanks in advance!
I'm sure this code can be much more elegant. Please note I'm just a novice vba user.
(ActiveSheet.Pictures.Insert is no good, because this only works on own computer)
On the first lines all seems ok. But after a given amount of rows, we see that the images are not inline with the cells anymore. See image enclosed.
Also note I use a fixed height of 40 here. Eventually the idea is to make this a variable input between 20 and 100.
Hope someone can point me in the right direction. Thanks in advance!
VBA Code:
Private Sub InsertPictures()
Dim objFile As String
Dim strPath As String
Dim lRow As Long
Dim c As Long
Dim rng As Range
Dim sh As Shape
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = -1 Then
strPath = .SelectedItems(1)
End If
End With
If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
lRow = Cells(Rows.Count, Range("B3").Column).End(xlUp).Row + 1
Application.ScreenUpdating = False
c = 3
Do Until c = lRow
If Cells(c, 2) = Empty Then
GoTo 10
Else
'Where will picture go?
Set rng = Cells(c, 3)
objFile = strPath & Cells(c, 2).Value
'Clear variable
Set sh = Nothing
On Error GoTo 10
'Attempt to load picture. Note -1 means to use default
Set sh = ActiveSheet.Shapes.AddPicture( _
Filename:=objFile, _
LinkToFile:=msoFalse, _
SaveWithDocument:=msoTrue, _
Left:=rng.Left + 2, _
Top:=rng.Top + 2, _
Width:=-1, _
Height:=-1)
On Error GoTo 0
If Not sh Is Nothing Then
'Lock ratios and resize height to fit cell
sh.LockAspectRatio = msoCTrue
sh.Height = 40 - 4
End If
End If
10:
c = c + 1
Loop
Application.ScreenUpdating = True
Range("A2").Select
End Sub
I'm sure this code can be much more elegant. Please note I'm just a novice vba user.
Attachments
Last edited by a moderator: