tranphuson
Thành viên thường trực
- Tham gia
- 14/8/09
- Bài viết
- 269
- Được thích
- 10
- Giới tính
- Nam
Bạn chạy thử code dưới xem.Cho mình hỏi có cách nào tạo Comment cho toàn bộ Sheet "Giao hàng" dựa theo chi tiết ở Sheet "ORDER"
Ví dụ: Ở Cột D4 -> DAT HOA có ngày order là "Thứ 5" thì comment sẽ dựa vào Sheet "Order" theo Thứ
Xin cảm ơn
Sub chen()
Dim arr, i As Long, j As Long, dk As String, dic As Object, lr As Long, cm As String
Set dic = CreateObject("scripting.dictionary")
With Sheets("order")
lr = .Range("C" & Rows.Count).End(xlUp).Row
If lr < 11 Then Exit Sub
arr = .Range("B10:m" & lr).Value
dk = .Range("g8").Value
For i = 2 To UBound(arr, 1)
cm = Empty
For j = 7 To 12
If dk = arr(i, j) Then
cm = dk & ": " & arr(1, j)
Exit For
End If
Next j
dic.Item(arr(i, 1)) = cm
Next i
End With
With Sheets("Giao hang")
lr = .Range("B" & Rows.Count).End(xlUp).Row
If lr < 4 Then Exit Sub
.Range("B4:N" & lr).ClearComments
For j = 3 To 13 Step 2
For i = 4 To lr
If .Cells(i, j).Value <> Empty Then
.Cells(i, j + 1).AddComment
.Cells(i, j + 1).Comment.Text Text:=dic.Item(.Cells(i, j).Value)
End If
Next i
Next j
End With
End Sub
Bạn chạy thử code dưới xem.
Mã:Sub chen() Dim arr, i As Long, j As Long, dk As String, dic As Object, lr As Long, cm As String Set dic = CreateObject("scripting.dictionary") With Sheets("order") lr = .Range("C" & Rows.Count).End(xlUp).Row If lr < 11 Then Exit Sub arr = .Range("B10:m" & lr).Value dk = .Range("g8").Value For i = 2 To UBound(arr, 1) cm = Empty For j = 7 To 12 If dk = arr(i, j) Then cm = dk & ": " & arr(1, j) Exit For End If Next j dic.Item(arr(i, 1)) = cm Next i End With With Sheets("Giao hang") lr = .Range("B" & Rows.Count).End(xlUp).Row If lr < 4 Then Exit Sub .Range("B4:N" & lr).ClearComments For j = 3 To 13 Step 2 For i = 4 To lr If .Cells(i, j).Value <> Empty Then .Cells(i, j + 1).AddComment .Cells(i, j + 1).Comment.Text Text:=dic.Item(.Cells(i, j).Value) End If Next i Next j End With End Sub
Bạn thay code này nhé.Nhân tiện cho mình hỏi thêm là nếu dòng 10 bị "Merge" như file đính kèm thì bị báo lỗi
Cảm ơn
Sub chen()
Dim arr, i As Long, j As Long, dk As String, dic As Object, lr As Long, cm As String
Set dic = CreateObject("scripting.dictionary")
With Sheets("order")
lr = .Range("C" & Rows.Count).End(xlUp).Row
If lr < 11 Then Exit Sub
arr = .Range("B10:m" & lr).Value
dk = .Range("g8").Value
For i = 2 To UBound(arr, 1)
cm = Empty
For j = 7 To 12
If dk = arr(i, j) Then
cm = dk & ": " & arr(1, j)
Exit For
End If
Next j
dic.Item(arr(i, 1)) = cm
Next i
End With
With Sheets("Giao hang")
lr = .Range("B" & Rows.Count).End(xlUp).Row
If lr < 4 Then Exit Sub
.Range("B4:N" & lr).ClearComments
For j = 3 To 13 Step 2
For i = 4 To lr
If .Cells(i, j).Value <> Empty Then
If dic.exists(.Cells(i, j).Value) Then
.Cells(i, j + 1).AddComment
.Cells(i, j + 1).Comment.Text Text:=dic.Item(.Cells(i, j).Value)
End If
End If
Next i
Next j
End With
End Sub
Bạn thay code này nhé.
Mã:Sub chen() Dim arr, i As Long, j As Long, dk As String, dic As Object, lr As Long, cm As String Set dic = CreateObject("scripting.dictionary") With Sheets("order") lr = .Range("C" & Rows.Count).End(xlUp).Row If lr < 11 Then Exit Sub arr = .Range("B10:m" & lr).Value dk = .Range("g8").Value For i = 2 To UBound(arr, 1) cm = Empty For j = 7 To 12 If dk = arr(i, j) Then cm = dk & ": " & arr(1, j) Exit For End If Next j dic.Item(arr(i, 1)) = cm Next i End With With Sheets("Giao hang") lr = .Range("B" & Rows.Count).End(xlUp).Row If lr < 4 Then Exit Sub .Range("B4:N" & lr).ClearComments For j = 3 To 13 Step 2 For i = 4 To lr If .Cells(i, j).Value <> Empty Then If dic.exists(.Cells(i, j).Value) Then .Cells(i, j + 1).AddComment .Cells(i, j + 1).Comment.Text Text:=dic.Item(.Cells(i, j).Value) End If End If Next i Next j End With End Sub