Brandon212
New Member
- Joined
- May 9, 2018
- Messages
- 3
New member here!
I have just recently started playing with macros and I have setup a work doc that runs a macro when a button is pushed. This macro allows me to browse for an image and then inserts it into a range of cells, resizes and centers it. The problem I am having is the image is linked to the original file. Is there a way to break the link or try another approach that doesn't keep this link?
Things I really need:
1. Browse to select a file.
2. Resize and center in cells.
3. No link to original file.
This is what I am using right now:
Sub INSERT_PICTURE_HORIZONTAL()
Dim picToOpen As String
picToOpen = Application _
.GetOpenFilename("")
If picToOpen <> "" Then _
InsertPictureInRange picToOpen, Range(Cells(5, 2), Cells(29, 13))
End Sub
Sub InsertPictureInRange(PictureFileName As String, TargetCells As Range)
' inserts a picture and resizes it to fit the TargetCells range
Dim p As Object, t As Double, l As Double, w As Double, h As Double, r As Integer
If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
If Dir(PictureFileName) = "" Then Exit Sub
' import picture
Set p = ActiveSheet.Pictures.Insert(PictureFileName)
' determine positions
With TargetCells
t = .Top
l = .Left
w = .Width
h = .Height
End With
' position picture
With p
.Top = t
.Left = l
.Width = w
.Height = h
End With
' position picture
With p
If .Width > w Then
.Width = w
.Height = h
End If
.Top = t + (h - .Height) / 2
.Left = l + (w - .Width) / 2
End With
Set p = Nothing
End Sub
I have just recently started playing with macros and I have setup a work doc that runs a macro when a button is pushed. This macro allows me to browse for an image and then inserts it into a range of cells, resizes and centers it. The problem I am having is the image is linked to the original file. Is there a way to break the link or try another approach that doesn't keep this link?
Things I really need:
1. Browse to select a file.
2. Resize and center in cells.
3. No link to original file.
This is what I am using right now:
Sub INSERT_PICTURE_HORIZONTAL()
Dim picToOpen As String
picToOpen = Application _
.GetOpenFilename("")
If picToOpen <> "" Then _
InsertPictureInRange picToOpen, Range(Cells(5, 2), Cells(29, 13))
End Sub
Sub InsertPictureInRange(PictureFileName As String, TargetCells As Range)
' inserts a picture and resizes it to fit the TargetCells range
Dim p As Object, t As Double, l As Double, w As Double, h As Double, r As Integer
If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
If Dir(PictureFileName) = "" Then Exit Sub
' import picture
Set p = ActiveSheet.Pictures.Insert(PictureFileName)
' determine positions
With TargetCells
t = .Top
l = .Left
w = .Width
h = .Height
End With
' position picture
With p
.Top = t
.Left = l
.Width = w
.Height = h
End With
' position picture
With p
If .Width > w Then
.Width = w
.Height = h
End If
.Top = t + (h - .Height) / 2
.Left = l + (w - .Width) / 2
End With
Set p = Nothing
End Sub