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
 

Some videos you may like

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

yky

Well-known Member
Joined
Jun 7, 2011
Messages
1,833
Office Version
  1. 2010
Platform
  1. Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,127,009
Messages
5,622,145
Members
415,881
Latest member
tasic89

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
Top