Em đang cần một đoạn code dùng để chèn hình ảnh theo một ô có điều kiện.Ở trên diễn đàn thì em cũng thấy có nhiều mục về việc này, nhưng em không thể tự sửa được theo ý của em. Mong các bác giúp đỡ ^^Chi tiết ở trong file excel ạ! Em cảm ơn nhiều nhiều
![]()
![]()
Vậy thì tạm làm theo code này thử:không bác ạ, cái này chỉ có khoảng vài hình cơ bản thôi ạ! cảm ơn về ý kiến của bác!
Private Sub Worksheet_Change(ByVal Target As Range)
Dim DataRng As Range, fRng As Range, Pic As Picture, pItem As Picture
On Error Resume Next
If Target.Column = 1 Then
If Target.Row >= 5 Then
If Target.Count = 1 Then
ActiveSheet.Shapes(Target.Address).Delete
On Error GoTo ExitSub
Set DataRng = Sheet1.Range("A2:B1000")
Set fRng = DataRng.Find(Target.Value, , , xlWhole)
If Not fRng Is Nothing Then
fRng.Offset(, 1).Copy
Target.Parent.Pictures.Paste
For Each pItem In Sheet2.Pictures
If pItem.Name Like "Picture*" Then
Set Pic = pItem: Exit For
End If
Next
With ActiveSheet.Shapes(Pic.Name)
.LockAspectRatio = False
.Top = Target.Top: .Left = Target.Offset(, 1).Left
.Height = Target.Height: .Width = Target.Offset(, 1).Width
.Name = Target.Address
End With
ExitSub:
Application.CutCopyMode = 0
End If
End If
End If
End If
End Sub
Chào thầy,Vậy thì tạm làm theo code này thử:
Chưa biết có cách nào hay hơnPHP:Private Sub Worksheet_Change(ByVal Target As Range) Dim DataRng As Range, fRng As Range, Pic As Picture, pItem As Picture On Error Resume Next If Target.Column = 1 Then If Target.Row >= 5 Then If Target.Count = 1 Then ActiveSheet.Shapes(Target.Address).Delete On Error GoTo ExitSub Set DataRng = Sheet1.Range("A2:B1000") Set fRng = DataRng.Find(Target.Value, , , xlWhole) If Not fRng Is Nothing Then fRng.Offset(, 1).Copy Target.Parent.Pictures.Paste For Each pItem In Sheet2.Pictures If pItem.Name Like "Picture*" Then Set Pic = pItem: Exit For End If Next With ActiveSheet.Shapes(Pic.Name) .LockAspectRatio = False .Top = Target.Top: .Left = Target.Offset(, 1).Left .Height = Target.Height: .Width = Target.Offset(, 1).Width .Name = Target.Address End With ExitSub: Application.CutCopyMode = 0 End If End If End If End If End Sub
Xem file