KlausW
Active Member
- Joined
- Sep 9, 2020
- Messages
- 458
- Office Version
- 2016
- Platform
- Windows
Hi
I have a challenge, I use this VBA code to insert images with. It works really well. It works with a userform, where I enter which cell I want to start inserting the images and whether it is in a column or in a row.
Now I would like to know if someone can help to avoid using the useform, so that I can insert the images by running the VBA code, starting in A1 and downwards.
Best regards
Klaus W
I have a challenge, I use this VBA code to insert images with. It works really well. It works with a userform, where I enter which cell I want to start inserting the images and whether it is in a column or in a row.
Now I would like to know if someone can help to avoid using the useform, so that I can insert the images by running the VBA code, starting in A1 and downwards.
Best regards
Klaus W
VBA Code:
Option Explicit
Private Sub cmdCancel_Click()
Unload Me
End Sub
Private Sub cmdOK_Click()
Const cBorder = 2
Dim vArray As Variant, vPicture As Variant, pic As Shape, rng As Range
If Me.RefEdit1 = "" Then
Me.RefEdit1.SetFocus
MsgBox "Please select a cell, then try again.", vbExclamation
Exit Sub
End If
vArray = Application.GetOpenFilename(FileFilter:="Pictures (*.png; *.jpg; *.jpeg; *.tif), *.png; *.jpg; *.jpeg; *.tif", _
Title:="Select Picture to Import", MultiSelect:=True)
If Not IsArray(vArray) Then
MsgBox "You didn't select any pictures. Please try again.", vbExclamation
Exit Sub
End If
Set rng = Range(Me.RefEdit1).MergeArea
For Each vPicture In vArray
Set pic = ActiveSheet.Shapes.AddPicture(Filename:=vPicture, LinkToFile:=False, SaveWithDocument:=True, _
Left:=rng.Left + cBorder, Top:=rng.Top + cBorder, Width:=-1, Height:=-1)
With pic
.LockAspectRatio = False
.Width = rng.Width - 2 * cBorder
.Height = rng.Height - 2 * cBorder
.Placement = xlMoveAndSize
End With
If Me.OptionButton1 Then
Set rng = rng.Offset(1, 0)
Else
Set rng = rng.Offset(0, 1)
End If
Next vPicture
Set pic = Nothing
Unload Me
End Sub