Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving images between cells in VBA

Tags:

excel

vba

I have an image in cell (3,1) and would like to move the image into cell (1,1).

I have this code:

ActiveSheet.Cells(1, 1).Value = ActiveSheet.Cells(3, 1).Value
ActiveSheet.Cells(3, 1).Value = ""

However, it seems that the cell value is empty for cells containing images, so therefore the image is not moved and the image in cell (3,1) is not deleted. Nothing happened when I run that particular bit of the code.

Any help is greatly appreciated.

Thanks.

like image 905
Tim Avatar asked Dec 15 '09 03:12

Tim


2 Answers

Part of the problem with your code is that you are thinking of the image as the value of the cell. However, although the image might appear to be "in" the cell, it is not actually the value of the cell.

To move the image, you can do so relatively (using Shape.IncrementLeft or Shape.IncrementRight) or you can do it absolutely (by setting the values of Shape.Left and Shape.Top).

In the example below, I demonstrate how you can move the shape to a new absolute position with or without keeping the original indentation off of the original cell (if you are not keeping the original indentation, this is as simple as setting the Top and Left values of the Shape to be equal to those of the target Range).

This procedure takes in a shape name (you can find the shape name in a number of ways; the way I did it was to record a macro and then click on the shape and move it to see the code it generated), the target address (such as "A1", and (optionally) a boolean value indicating if you want to retain the original indentation offset.

Sub ShapeMove(strShapeName As String, _
    strTargetAddress As String, _
    Optional blnIndent As Boolean = True)
Dim ws As Worksheet
Dim shp As Shape
Dim dblCurrentPosLeft As Double
Dim dblCurrentPosTop As Double
Dim rngCurrentCell As Range
Dim dblCurrentCellTop As Double
Dim dblCurrentCellLeft As Double
Dim dblIndentLeft As Double
Dim dblIndentTop As Double
Dim rngTargetCell As Range
Dim dblTargetCellTop As Double
Dim dblTargetCellLeft As Double
Dim dblNewPosTop As Double
Dim dblNewPosLeft As Double

'Set ws to be the ActiveSheet, though this can really be any sheet      '
Set ws = ActiveSheet

'Set the shp variable as the shape with the specified shape name  '
Set shp = ws.Shapes(strShapeName)

'Get the current position of the image on the worksheet                 '
dblCurrentPosLeft = shp.Left
dblCurrentPosTop = shp.Top

'Get the current cell range of the image                                '
Set rngCurrentCell = ws.Range(shp.TopLeftCell.Address)

'Get the absolute position of the current cell                          '
dblCurrentCellLeft = rngCurrentCell.Left
dblCurrentCellTop = rngCurrentCell.Top

'Establish the current offset of the image in relation to the top left cell'
dblIndentLeft = dblCurrentPosLeft - dblCurrentCellLeft
dblIndentTop = dblCurrentPosTop - dblCurrentCellTop

'Set the rngTargetCell object to be the address specified in the paramater '
Set rngTargetCell = ws.Range(strTargetAddress)

'Get the absolute position of the target cell       '
dblTargetCellLeft = rngTargetCell.Left
dblTargetCellTop = rngTargetCell.Top

'Establish the coordinates of the new position. Only indent if the boolean '
' parameter passed in is true. '
' NB: The indent can get off if your indentation is greater than the length '
' or width of the cell '
If blnIndent Then
    dblNewPosLeft = dblTargetCellLeft + dblIndentLeft
    dblNewPosTop = dblTargetCellTop + dblIndentTop
Else
    dblNewPosLeft = dblTargetCellLeft
    dblNewPosTop = dblTargetCellTop
End If

'Move the shape to its new position '
shp.Top = dblNewPosTop
shp.Left = dblNewPosLeft

End Sub

NOTE: I wrote the code in very much a functional manner. If you wanted to "clean up" this code, it would be best to put the functionality within an object. Hopefully it helps the reader understand how shapes work in Excel either way.

like image 190
Ben McCormack Avatar answered Sep 17 '22 15:09

Ben McCormack


A quick and dirty way:

Public Sub Example()
    MoveShape ActiveSheet.Shapes("Picture 1"), Range("A1")
End Sub

Private Sub MoveShape(ByVal shp As Excel.Shape, ByVal target As Excel.Range)
    shp.IncrementLeft -(shp.TopLeftCell.Left - target.Left)
    shp.IncrementTop -(shp.TopLeftCell.Top - target.Top)
End Sub
like image 41
Oorang Avatar answered Sep 20 '22 15:09

Oorang