首页 > 解决方案 > 将图像移动到工作表上的特定位置

问题描述

我有 12 张图片直接从 windows 文件夹拖放到工作表中。它们被命名为“1 1.bmp”、“1 2.bmp”、“1 3.bmp”等等。我想移动它们,但如何移动?

这是我正在尝试的代码:

Worksheets("R").Shapes("1 1").Top = Worksheets("R").Rows(24).Top

我不知道如何引用它们。它们与 .xlsm 文件位于同一文件夹中。我试过了

Worksheets("R").Shapes("1 1.bmp").Top = Worksheets("R").Rows(24).Top

也。

来自另一个问题的两个示例都来自堆栈溢出。

什么是正确的语法?

/詹斯

标签: excelvbaimage

解决方案


试试这个代码:

Option Explicit

Sub ArrangePictures()
    Dim sh As Shape, anchor_cell As Range, v_shift As Long
    With Worksheets("R")
        Set anchor_cell = .Range("B24")             'left top corner for pictures
        v_shift = 0                                 'vertical shift for next picture
        For Each sh In .Shapes                      'loop over all the shapes in the sheet
            If sh.Type = msoPicture Then            'check if the shape is a picture
                sh.Top = anchor_cell.Top + v_shift  'move picture (vertical)
                sh.Left = anchor_cell.Left          'move picture (horizontal)
                v_shift = v_shift + sh.Height       'add vertical shift for next picture
            End If
        Next
    End With
End Sub

推荐阅读