Skip to content

Instantly share code, notes, and snippets.

@kimsama
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save kimsama/e510e5fbfd8258eb7c44 to your computer and use it in GitHub Desktop.

Select an option

Save kimsama/e510e5fbfd8258eb7c44 to your computer and use it in GitHub Desktop.
A VBA script which loads image file into a selected cell then resize cell to fit to loaded size of the image and insert only image filename into the nexe cell of the cell inserted an image.
Sub InsertPicture()
Dim iRange As Range
Dim strMessage As String
Dim x As Variant
Dim filename As String
Set iRange = ActiveCell
Application.ScreenUpdating = False
strMessage = Application.GetOpenFilename(filefilter:="picture(*.JPG;*.GIF;*.BMP;*.PNG),*.JPG;*.GIF;*.BMP;*.PNG", _
Title:="Select an image to insert into the selected cell.")
If strMessage = "False" Then
MsgBox "None of an image is selected.", 64, "Error"
Exit Sub
End If
' insert an image into the selected cell
With ActiveSheet.Pictures.Insert(strMessage)
' resize the cell to fit the image size
iRange.RowHeight = .Height
iRange.ColumnWidth = .Width
' insert image filename to the nexe cell of the cell which inserted the loaded image.
x = Split(strMessage, Application.PathSeparator)
filename = Replace(x(UBound(x)), ".png", "")
iRange.Next.Value = filename
End With
Application.ScreenUpdating = True
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment