Excel VBA通过复制/粘贴触发工作表更改

Excel VBA通过复制/粘贴触发工作表更改,excel,vba,Excel,Vba,我正在尝试使用VBA根据图纸同一行上B列的值,使用图像文件填充电子表格G列。如果我手动将值输入B列,一切都很好,但是我有一个很长的列表,希望将多个值复制/粘贴到B列。粘贴时,似乎没有触发工作表更改,H列也没有填充图像。我正在使用的代码如下,任何帮助都将不胜感激。谢谢 Private Sub Worksheet_Change(ByVal Target As Range) Dim shp As Shape If Intersect(Target, [B:B]) Is Nothing Then Exi

我正在尝试使用VBA根据图纸同一行上B列的值,使用图像文件填充电子表格G列。如果我手动将值输入B列,一切都很好,但是我有一个很长的列表,希望将多个值复制/粘贴到B列。粘贴时,似乎没有触发工作表更改,H列也没有填充图像。我正在使用的代码如下,任何帮助都将不胜感激。谢谢

Private Sub Worksheet_Change(ByVal Target As Range)
Dim shp As Shape
If Intersect(Target, [B:B]) Is Nothing Then Exit Sub
If Target.Row Mod 20 = 0 Then Exit Sub
On Error GoTo son

For Each shp In ActiveSheet.Shapes
If shp.Type = msoPicture And shp.TopLeftCell.Address = Target.Offset(0, 4).Address Then shp.Delete
Next

If Target.Value <> "" And Dir(ThisWorkbook.Path & "\" & Target.Value & ".jpg") = "" Then
        'picture not there!
        MsgBox Target.Value & " Doesn't exist!"
End If

ActiveSheet.Pictures.Insert(ThisWorkbook.Path & "\" & Target.Value & ".jpg").Select
Selection.Top = Target.Offset(0, 5).Top
Selection.Left = Target.Offset(0, 5).Left

With Selection.ShapeRange
.LockAspectRatio = msoFalse
.Height = Target.Offset(0, 5).Height
.Width = Target.Offset(0, 5).Width
End With
Target.Offset(1, 0).Select
son:

End Sub
Private子工作表\u更改(ByVal目标作为范围)
将shp变暗为形状
如果Intersect(目标[B:B])为空,则退出Sub
如果Target.Row Mod 20=0,则退出Sub
关于错误GoTo-son
对于ActiveSheet.Shapes中的每个shp
如果shp.Type=msoPicture,shp.TopLeftCell.Address=Target.Offset(0,4).Address,则shp.Delete
下一个
如果Target.Value“”和Dir(ThisWorkbook.Path&“\”&Target.Value&“.jpg”)=则
”“不在那儿!
MsgBox Target.Value&“不存在!”
如果结束
ActiveSheet.Pictures.Insert(ThisWorkbook.Path&“\”&Target.Value&“.jpg”)。选择
Selection.Top=Target.Offset(0,5).Top
Selection.Left=Target.Offset(0,5)。Left
带Selection.shaperage
.LockAspectRatio=msoFalse
.高度=目标.偏移量(0,5).高度
.Width=目标.Offset(0,5).Width
以
目标。偏移量(1,0)。选择
儿子:
端接头

粘贴多个值时,
目标
参数将成为粘贴范围的数组。 如果只粘贴一行,它也是一个由1个成员组成的数组

所以,使用
For..Next
循环完成所有要粘贴的行。并将所有
Target
更改为
Target(i)
,并将部分代码更改如下

For i = 1 To Target.Rows.Count
    If Target(i).Value <> "" And Dir(ThisWorkbook.Path & "\" & Target(i).Value & ".jpg") = "" Then
        'picture not there!
        MsgBox Target(i).Value & " Doesn't exist!"
    Else
        ActiveSheet.Pictures.Insert(ThisWorkbook.Path & "\" & Target(i).Value & ".jpg").Select
        Selection.Top = Target(i).Offset(0, 5).Top
        Selection.Left = Target(i).Offset(0, 5).Left

        With Selection.ShapeRange
        .LockAspectRatio = msoFalse
        .Height = Target(i).Offset(0, 5).Height
        .Width = Target(i).Offset(0, 5).Width
        End With
    End If
Next
i=1到Target.Rows.Count的

如果Target(i).Value“”和Dir(ThisWorkbook.Path&“\”&Target(i).Value&“.jpg”)=则
”“不在那儿!
MsgBox目标(i).Value&“不存在!”
其他的
ActiveSheet.Pictures.Insert(ThisWorkbook.Path&“\”和Target(i.Value&“.jpg”)。选择
Selection.Top=目标(i).偏移量(0,5).Top
选择。左=目标(i)。偏移量(0,5)。左
带Selection.shaperage
.LockAspectRatio=msoFalse
.高度=目标(i).偏移量(0,5).高度
.宽度=目标(i).偏移量(0,5).宽度
以
如果结束
下一个

粘贴多个值时,
目标
参数将成为粘贴范围的数组。 如果只粘贴一行,它也是一个由1个成员组成的数组

所以,使用
For..Next
循环完成所有要粘贴的行。并将所有
Target
更改为
Target(i)
,并将部分代码更改如下

For i = 1 To Target.Rows.Count
    If Target(i).Value <> "" And Dir(ThisWorkbook.Path & "\" & Target(i).Value & ".jpg") = "" Then
        'picture not there!
        MsgBox Target(i).Value & " Doesn't exist!"
    Else
        ActiveSheet.Pictures.Insert(ThisWorkbook.Path & "\" & Target(i).Value & ".jpg").Select
        Selection.Top = Target(i).Offset(0, 5).Top
        Selection.Left = Target(i).Offset(0, 5).Left

        With Selection.ShapeRange
        .LockAspectRatio = msoFalse
        .Height = Target(i).Offset(0, 5).Height
        .Width = Target(i).Offset(0, 5).Width
        End With
    End If
Next
i=1到Target.Rows.Count的

如果Target(i).Value“”和Dir(ThisWorkbook.Path&“\”&Target(i).Value&“.jpg”)=则
”“不在那儿!
MsgBox目标(i).Value&“不存在!”
其他的
ActiveSheet.Pictures.Insert(ThisWorkbook.Path&“\”和Target(i.Value&“.jpg”)。选择
Selection.Top=目标(i).偏移量(0,5).Top
选择。左=目标(i)。偏移量(0,5)。左
带Selection.shaperage
.LockAspectRatio=msoFalse
.高度=目标(i).偏移量(0,5).高度
.宽度=目标(i).偏移量(0,5).宽度
以
如果结束
下一个