I have a file where i successfully locate pictures in the cell within a table. however, when i sort the table, the picture does not move with the row as the table is sorted.
here is my code:
Sub cmdAddPic_Click()
Dim r As Range
Dim ws As Worksheet
Dim tbl As ListObject
Dim lRow As Long
Dim img As Picture
Dim fNameAndPath As Variant
Set ws = Worksheets(strDataWs)
Set tbl = ws.ListObjects(strDataTbl)
lRow = tbl.DataBodyRange.Rows.Count + 1
lCol = tbl.DataBodyRange.Columns.Count - 1
'Sets destination for image to last row and next to last column
Set r = tbl.DataBodyRange.Cells(lRow, lCol)
ws.Unprotect Password:="Molykote"
'select file
fNameAndPath = Application.GetOpenFilename(Title:="Select Picture To Be Imported")
If fNameAndPath = False Then Exit Sub
'Sets image to file namd and path
Set img = ws.Pictures.Insert(fNameAndPath)
'sets size of image
With img
.ShapeRange.LockAspectRatio = msoTrue
.Top = r.Top
.Left = r.Left
.Width = r.Width
.Height = r.Height
img.Placement = xlFreeFloating
.PrintObject = True
End With
' Load picture to user form
With Me
.GraphImage.Picture = LoadPicture(fNameAndPath)
.txt_GraphImageRefFile.Value = (fNameAndPath)
End With
End Sub
here is my code:
Sub cmdAddPic_Click()
Dim r As Range
Dim ws As Worksheet
Dim tbl As ListObject
Dim lRow As Long
Dim img As Picture
Dim fNameAndPath As Variant
Set ws = Worksheets(strDataWs)
Set tbl = ws.ListObjects(strDataTbl)
lRow = tbl.DataBodyRange.Rows.Count + 1
lCol = tbl.DataBodyRange.Columns.Count - 1
'Sets destination for image to last row and next to last column
Set r = tbl.DataBodyRange.Cells(lRow, lCol)
ws.Unprotect Password:="Molykote"
'select file
fNameAndPath = Application.GetOpenFilename(Title:="Select Picture To Be Imported")
If fNameAndPath = False Then Exit Sub
'Sets image to file namd and path
Set img = ws.Pictures.Insert(fNameAndPath)
'sets size of image
With img
.ShapeRange.LockAspectRatio = msoTrue
.Top = r.Top
.Left = r.Left
.Width = r.Width
.Height = r.Height
img.Placement = xlFreeFloating
.PrintObject = True
End With
' Load picture to user form
With Me
.GraphImage.Picture = LoadPicture(fNameAndPath)
.txt_GraphImageRefFile.Value = (fNameAndPath)
End With
End Sub