Moving pictures in table when table is sorted

Chadchi

New Member
Joined
Mar 2, 2021
Messages
2
Office Version
  1. 365
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
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
You set the img placement to xlFreeFloating. So, they are not moving with cells. Try this:

VBA Code:
img.Placement = xlMoveAndSize  'or xlMove only if you don't want to resize
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,536
Members
449,037
Latest member
tmmotairi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top