Option Explicit
Sub chen_anh()
Dim lastRow As Long, curr_row As Long, k As Long, r As Long, start As Long, msg As String, chiso(), shp As Shape, rng As Range, max_row As Long, t
' sheet HINH
With ThisWorkbook.Worksheets("HINH")
If .Shapes.Count = 0 Then Exit Sub ' neu HINH khong co Anh thi ket thuc
For Each shp In .Shapes
If LCase(shp.Name) Like "picture*" Then
shp.Top = shp.Top + 3
If max_row < shp.TopLeftCell.Row Then max_row = shp.TopLeftCell.Row
shp.Top = shp.Top - 3
End If
Next shp
k = max_row
ThisWorkbook.Worksheets("BC").Columns("B:B").ColumnWidth = 21 ' Column With cua cot B = 21
' mang chiso co 3 cot: cot 1 la noi dung cua cot D tren sheet, cot 2 la chi so dong cua Anh va noi dung, cot 3 la Ten Anh
ReDim chiso(1 To k, 1 To 3)
For Each shp In .Shapes
If LCase(shp.Name) Like "picture*" Then
shp.Top = shp.Top + 3 ' nhieu anh cho´m len cell o dong truoc nen dung shp.TopLeftCell.Row se sai. De khac phuc truoc tien ta dich Picture xuong duoi mot chut - 3 point
r = shp.TopLeftCell.Row
chiso(k + 1 - r, 1) = Application.Trim(Application.Clean(.Cells(r, "D").Value))
chiso(k + 1 - r, 2) = k + 1 - r ' chi so dong
chiso(k + 1 - r, 3) = shp.Name ' ten Anh
shp.Top = shp.Top - 3 ' dich len phia tren 3 point - tra lai vi tri cu cua Picture
End If
Next shp
End With
Application.ScreenUpdating = False
start = 2
With ThisWorkbook.Worksheets("BC")
.Activate
lastRow = .Cells(Rows.Count, "D").End(xlUp).Row + 1
For r = 2 To lastRow
If Len(.Range("A" & r).Value) > 30 Or r = lastRow Then ' dong hien hanh la dong chua NGAY
For k = r - 1 To start Step -1 ' duyet tung dong di tu dong cuoi cung len phia tren
If .Range("A" & k).Value = "" And .Range("D" & k).Value <> "" Then ' dong can chen Anh
Set rng = .Range("B" & k) ' o ma o do se dan Anh
' xoa anh cu neu dang ton tai
On Error Resume Next
.Shapes(rng.Address).Delete
curr_row = 0
' tim noi dung hien hanh co o cot D cua sheet BC trong cot HINH!D
curr_row = Application.VLookup(Application.Trim(Application.Clean(.Range("D" & k).Value)), chiso, 2, 0)
On Error GoTo 0
If curr_row Then ' neu tim thay noi dung hien hanh co o cot D cua sheet BC ...
' trong mang chiso thay gia tri vua tim duoc bang "x" de trong cac buoc sau khong tim o dong do nua
chiso(curr_row, 1) = "x"
' sao chep anh tu HINH vao bo nho
ThisWorkbook.Worksheets("HINH").Shapes(chiso(curr_row, 3)).Copy
rng.EntireRow.RowHeight = 108 ' Row Height cua dong hien hanh bang 108
rng.Select
' neu co loi "Paste method of Worksheet class failed" thi thu thay 0.4 bang gia tri lon hon, vd. 0.6
t = Timer
Do While Timer - t < 0.6
DoEvents
Loop
' dan anh tu bo nho vao o trong cot B cua sheet ngay hien hanh
.Paste
' dieu chinh kich thuoc anh va dat ten la dia chi cua o tai cot B
With Selection
.ShapeRange.LockAspectRatio = msoFalse
.Width = rng.Width
.Height = rng.Height
.Name = rng.Address ' dat ten cho Anh
End With
Else
msg = msg & "Gia tri tai D" & k & " trong sheet BC khong co trong sheet HINH, hoac Anh ung voi no khong co trong sheet HINH" & vbCrLf
End If
End If
Next k
start = r + 1
End If
Next r
End With
Application.ScreenUpdating = True
If Len(msg) Then
MsgBox msg
Else
MsgBox "Da chen xong anh"
End If
End Sub