Excel 2016 VBA女士如何使用.top和.left

时间:2018-10-01 19:45:21

标签: excel vba

我有EXCEL 2016英文 我正在使用以下代码:

Sub PastePicture(Direccion)
Dim Altura As Long
Dim Width As Long
    ActiveSheet.Range(Direccion).Select

    Height = RangeHeight(Direccion) - 3
    Width = RangeWidth(Direccion) - 4

    ActiveSheet.Paste
    Selection.Top = rCell.Top + 3   ' this is not working
    Selection.Left = rCell.Left + 2 ' this is not working
    Selection.ShapeRange.LockAspectRatio = msoFalse
    Selection.Width = Width
    Selection.Height = Height
End Sub

Direccion是一个单元格范围。 RangeheightRangeWidth是两个计算范围大小的函数。 我想将图片粘贴到该范围,但定义.Top.Left。 因此,定位不完全位于范围的角落,而是稍稍位于内部。 我希望我的问题很清楚。

1 个答案:

答案 0 :(得分:0)

我编写了这些功能,如果在代码中使用ShapePosition,它应该可以完成工作。

Option Explicit

Sub Test()

    Dim cShp As Shape
    Dim cRng As Range

    Set cRng = Range("B3:F12")
    Set cShp = Selection.ShapeRange.Item(1) 'Takes the selected picture

    Call ShapePosition(cShp, cRng, 50, 50) 'The picture is getting centered

End Sub


Sub ShapePosition(xShp As Shape, xRng As Range, xOffsetTop As Double, xOffsetLeft As Double)

' If xOffseTop is 0 - the picture is fully to the top of the range, when 50 - it is centered, 100 - fully to the bottom
' If xOffseLeft is 0 - the picture is fully to the left of the range, when 50 - it is centered, 100 - fully to the right

    If xShp.Height > xRng.Height Or xShp.Width > xRng.Width Then
        MsgBox "The picture is too big!"
        Exit Sub
    End If

    xShp.Top = xRng.Top + ((xRng.Height - xShp.Height) * xOffsetTop / 100)
    xShp.Left = xRng.Left + ((xRng.Width - xShp.Width) * xOffsetLeft / 100)

End Sub