在 Excel 中自动生成图像名称

在 Excel 中自动生成图像名称

正如所描述的这里,Excel 引入了加载到电子表格中的图像的通用文件名。我想知道是否可以创建一个 VBA 脚本或其他程序,将这些名称保存在电子表格中的某个位置?

例如,我加载一个空电子表格foo.jpg,在某个单元格中,我得到image1.jpg。然后我加载bar.jpg得到image2.jpg等等。

答案1

您需要编写如下所示的 VBA 代码来插入图片并跟踪其名称。

Sub InsertPicture()
    Dim sFileName As String


    sFileName = Application.GetOpenFilename("Pictures JPG (*.jpg)|*.jpg, Pictures PNG (*.png)|*.png", 0, "Select the Picture", , False)

    Sheets("Sheet1").Range("A1048576").End(xlUp).Offset(1, 0).Value = sFileName

    ActiveSheet.Pictures.Insert(sFileName).Select
End Sub

相关内容