【问题标题】:Moving images between cells in VBA在 VBA 中的单元格之间移动图像
【发布时间】:2009-12-15 03:52:24
【问题描述】:

我在单元格 (3,1) 中有一个图像,并希望将图像移动到单元格 (1,1) 中。

我有这个代码:

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

但是,包含图像的单元格的单元格值似乎是空的,因此不会移动图像并且不会删除单元格 (3,1) 中的图像。当我运行那段特定的代码时,什么也没发生。

非常感谢任何帮助。

谢谢。

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    您的代码的部分问题在于您将图像视为单元格的。然而,虽然图像可能看起来“在”单元格中,但它实际上并不是单元格的值。

    要移动图像,您可以相对(使用Shape.IncrementLeftShape.IncrementRight)或绝对(通过设置@ 987654323@ 和 Shape.Top)。

    在下面的示例中,我演示了如何将形状移动到新的绝对位置,无论是否保留原始单元格的原始缩进(如果您不保留原始缩进,这就像设置ShapeTopLeft 值等于目标 Range 的值。

    此过程接受一个形状名称(您可以通过多种方式找到形状名称;我这样做的方法是录制一个宏,然后单击形状并移动它以查看它生成的代码),目标地址(例如"A1",和(可选)一个布尔值,指示是否要保留原始缩进偏移量。

    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
    

    注意:我以非常实用的方式编写代码。如果您想“清理”此代码,最好将功能放在对象中。希望它可以帮助读者了解形状在 Excel 中的工作方式。

    【讨论】:

    • 很好,如果我使用 false,这会处理奇怪/随机大小的缩进
    【解决方案2】:

    一种快速而肮脏的方式:

    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
    

    【讨论】:

    • 非常简单,但不幸的是,在使用它时,我会从目标单元格的左上角得到随机大小的缩进。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多