【发布时间】:2020-07-01 16:31:35
【问题描述】:
我正在尝试运行 VBA 代码,以便使用特定参考(.jpg 的名称和 Excel 上写的名称)自动插入图像。我正在使用 Mac 并不断收到错误消息:
运行时错误'1004'
如果有人可以提供帮助,我在下面包含了我正在使用的代码:
Sub Picture()
Dim pictname As String
Dim pastehere As Range
Dim pasterow As Long
Dim x As Long
Dim lastrow As Long
lastrow = Worksheets("sheet1").Range("B1").CurrentRegion.Rows.Count
x = 2
For x = 2 To lastrow
Set pastehere = Cells(x, 1)
pasterow = pastehere.Row
Cells(pasterow, 1).Select
pictname = Cells(x, 2) 'This is the picture name
ActiveSheet.Pictures.Insert("/Users/name/Desktop/macro" & pictname & ".JPG").Select
With Selection
.Left = Cells(pasterow, 1).Left
.Top = Cells(pasterow, 1).Top
.ShapeRange.LockAspectRatio = msoFalse
.ShapeRange.Height = 80#
.ShapeRange.Width = 80#
.ShapeRange.Rotation = 0#
End With
Next
End Sub
【问题讨论】:
-
您检查文件名是否正确?
/macro是存储文件的文件夹吗?在这种情况下,您在连接pictname之前缺少/ -
顺便说一句:告诉哪一行引发错误总是一个好主意。
-
嗨,对不起,我在原帖中不清楚。是的 /Macro 是文件夹。导致错误的行在下面突出显示:ActiveSheet.Pictures.Insert("/Users/name/Desktop/macro" & pictname & ".JPG").Select
标签: excel vba macos image automation