Xin phép Mod vì em hỏi bằng cách reply trong Topic không có ai trả lời nên em tạo Topic mới ạ. Vấn đề em đã trình bày chi tiết trong file đính kèm. Xin các cao nhân giúp đỡ ạ
Mãi không ai giúp em với ạ .
Sub loc()
Set dic = CreateObject("Scripting.Dictionary")
nguon = Sheets(1).Range("A2:I16")
ReDim kq(1 To 100, 1 To 8)
Sheets(2).[A2:H1000].ClearContents
k = 0
For i = 1 To UBound(nguon)
If nguon(i, UBound(nguon, 2)) = Sheets(2).[A1] Then
k = k + 1
If nguon(i, 1) = "" Then
temp = Sheets(1).Range("A" & Sheets(1).Cells(i, 1).End(3).Row)
If Not dic.exists(temp) Then
kq(k, 1) = temp
dic.Add temp, ""
End If
Else
kq(k, 1) = nguon(i, 1)
dic.Add nguon(i, 1), ""
End If
For j = 2 To UBound(nguon, 2) - 1
kq(k, j) = nguon(i, j)
Next j
End If
Next i
Sheets(2).[A2].Resize(100, 8) = kq
End Sub
Gõ vào ô A1 nha.....................................
Sub GPE()
Dim Darr, Arr(), Nban As String, i As Long, j As Integer, k As Integer, tmp As String
With Sheets(1)
Darr = .Range("A2:I" & .Range("B2").End(xlDown).Row + 1)
End With
ReDim Arr(1 To UBound(Darr), 1 To 8)
With Sheets(2)
Nban = .Cells(1, 1)
For i = 1 To UBound(Darr) - 1
If Darr(i + 1, 1) = "" Then Darr(i + 1, 1) = Darr(i, 1)
If Darr(i, 9) = Nban Then
k = k + 1
For j = 2 To 8
Arr(k, j) = Darr(i, j)
Next j
If tmp <> Darr(i, 1) Then
Arr(k, 1) = Darr(i, 1)
tmp = Darr(i, 1)
End If
End If
Next i
.Range("A2:H20000").Clear
.Range("A2").Resize(k, 8) = Arr
End With
End Sub
Bạn kiểm tra xem Code này được chưa nha:
Mã:Sub loc() Set dic = CreateObject("Scripting.Dictionary") nguon = Sheets(1).Range("A2:I16") ReDim kq(1 To 100, 1 To 8) Sheets(2).[A2:H1000].ClearContents k = 0 For i = 1 To UBound(nguon) If nguon(i, UBound(nguon, 2)) = Sheets(2).[A1] Then k = k + 1 If nguon(i, 1) = "" Then temp = Sheets(1).Range("A" & Sheets(1).Cells(i, 1).End(3).Row) If Not dic.exists(temp) Then kq(k, 1) = temp dic.Add temp, "" End If Else kq(k, 1) = nguon(i, 1) dic.Add nguon(i, 1), "" End If For j = 2 To UBound(nguon, 2) - 1 kq(k, j) = nguon(i, j) Next j End If Next i Sheets(2).[A2].Resize(100, 8) = kq End Sub
Bên trên đã có bài của anh LetGâuGâu giúp rồi còn gì nhỉ ..
Gõ vào ô A1 nha.....................................
Gõ vào ô A1 nha.....................................
sửa code@Hiếu CD : Code bác em thấy chạy ngon nhất ạ nhưng có điều khi in ra dữ liệu thì nó làm xóa đi các style em đã định dạng cho bảng như: Màu background, Fontsize chữ và màu chữ như ảnh em đính kèm đó ạ. Bác có fix được cái này ko bác
Sub GPE()
Dim Darr, Arr(), Nban As String, i As Long, j As Integer, k As Integer, tmp As String
With Sheets(1)
Darr = .Range("A2:I" & .Range("B2").End(xlDown).Row + 1)
End With
ReDim Arr(1 To UBound(Darr), 1 To 8)
With Sheets(2)
[COLOR=#0000ff]Nban = .Range("[/COLOR][COLOR=#ff0000]A1[/COLOR][COLOR=#0000ff]")[/COLOR]
For i = 1 To UBound(Darr) - 1
If Darr(i + 1, 1) = "" Then Darr(i + 1, 1) = Darr(i, 1)
If Darr(i, 9) = Nban Then
k = k + 1
For j = 2 To 8
Arr(k, j) = Darr(i, j)
Next j
If tmp <> Darr(i, 1) Then
Arr(k, 1) = Darr(i, 1)
tmp = Darr(i, 1)
End If
End If
Next i
.Range("A2:H20000").[COLOR=#ff0000]ClearContents[/COLOR]
.Range("A2").Resize(k, 8) = Arr
End With
End Sub
file bạn gởi bị lổi@HieuCd: Bác giúp em thêm tý nữa được không ạ. Khi in ra sản phẩm bác loại trừ những sản phẩm có Xuất sứ là XS1 hoặc XS9 ở cột Xuất sứ ( Mà em đã đánh dấu đỏ như ở File đính kèm ) được không ạ.
Sub GPE()
Dim Darr, Arr(), Nban As String, i As Long, j As Integer, k As Integer, tmp As String
With Sheets(1)
Darr = .Range("A2:I" & .Range("B2").End(xlDown).Row + 1)
End With
ReDim Arr(1 To UBound(Darr), 1 To 8)
With Sheets(2)
Nban = .Range("A1")
For i = 1 To UBound(Darr) - 1
If Darr(i + 1, 1) = "" Then Darr(i + 1, 1) = Darr(i, 1)
[COLOR=#ff0000] If Darr(i, 9) = Nban And Darr(i, 7) <> "XS1" And Darr(i, 7) <> "XS9" Then[/COLOR]
k = k + 1
For j = 2 To 8
Arr(k, j) = Darr(i, j)
Next j
If tmp <> Darr(i, 1) Then
Arr(k, 1) = Darr(i, 1)
tmp = Darr(i, 1)
End If
End If
Next i
.Range("A2:H20000").ClearContents
.Range("A2").Resize(k, 8) = Arr
End With
End Sub
Bạn thay code nầy@HieuCD: Cảm ơn bác ạ . Bác giúp em nốt trường hợp này với ạ. Chả là khi Sản phẩm ở cột Xuất sứ chỉ có XS1 & XS9 thì khi em chạy Macro nó báo lỗi như ảnh vs file em đính kèm. Bác thay cái báo lỗi này thành 1 cái thông báo là " Không có dữ liệu " giùm em với được không ạ.
Sub GPE()
Dim Darr, Arr(), Nban As String, i As Long, j As Integer, k As Integer, tmp As String
With Sheets(1)
Darr = .Range("A2:I" & .Range("B65500").End(xlUp).Row + 1)
End With
ReDim Arr(1 To UBound(Darr), 1 To 8)
With Sheets(2)
Nban = .Range("A1")
For i = 1 To UBound(Darr) - 1
If Darr(i + 1, 1) = "" Then Darr(i + 1, 1) = Darr(i, 1)
If Darr(i, 9) = Nban And Darr(i, 7) <> "XS1" And Darr(i, 7) <> "XS9" Then
k = k + 1
For j = 2 To 8
Arr(k, j) = Darr(i, j)
Next j
If tmp <> Darr(i, 1) Then
Arr(k, 1) = Darr(i, 1)
tmp = Darr(i, 1)
End If
End If
Next i
If k = 0 Then
MsgBox "Khong co du lieu nguoi ban " & Nban & " co xuat su XS1 va XS9"
Exit Sub
End If
.Range("A2:H20000").ClearContents
.Range("A2").Resize(k, 8) = Arr
End With
End Sub
[TABLE="width: 72"]
[TR]
[TD="colspan: 2"]mã acci[/TD]
[/TR]
[TR]
[TD]á[/TD]
[TD="align: right"]225[/TD]
[/TR]
[TR]
[TD]à[/TD]
[TD="align: right"]224[/TD]
[/TR]
[TR]
[TD]ả[/TD]
[TD="align: right"]7843[/TD]
[/TR]
[TR]
[TD]ã[/TD]
[TD="align: right"]227[/TD]
[/TR]
[TR]
[TD]ạ[/TD]
[TD="align: right"]7841[/TD]
[/TR]
[TR]
[TD]ắ[/TD]
[TD="align: right"]7855[/TD]
[/TR]
[TR]
[TD]ằ[/TD]
[TD="align: right"]7857[/TD]
[/TR]
[TR]
[TD]ẳ[/TD]
[TD="align: right"]7859[/TD]
[/TR]
[TR]
[TD]ẵ[/TD]
[TD="align: right"]7861[/TD]
[/TR]
[TR]
[TD]ặ[/TD]
[TD="align: right"]7863[/TD]
[/TR]
[TR]
[TD]ấ[/TD]
[TD="align: right"]7845[/TD]
[/TR]
[TR]
[TD]ầ[/TD]
[TD="align: right"]7847[/TD]
[/TR]
[TR]
[TD]ẩ[/TD]
[TD="align: right"]7849[/TD]
[/TR]
[TR]
[TD]ẫ[/TD]
[TD="align: right"]7851[/TD]
[/TR]
[TR]
[TD]ậ[/TD]
[TD="align: right"]7853[/TD]
[/TR]
[TR]
[TD]ó[/TD]
[TD="align: right"]243[/TD]
[/TR]
[TR]
[TD]ò[/TD]
[TD="align: right"]242[/TD]
[/TR]
[TR]
[TD]ỏ[/TD]
[TD="align: right"]7887[/TD]
[/TR]
[TR]
[TD]õ[/TD]
[TD="align: right"]245[/TD]
[/TR]
[TR]
[TD]ọ[/TD]
[TD="align: right"]7885[/TD]
[/TR]
[TR]
[TD]ố[/TD]
[TD="align: right"]7889[/TD]
[/TR]
[TR]
[TD]ồ[/TD]
[TD="align: right"]7891[/TD]
[/TR]
[TR]
[TD]ổ[/TD]
[TD="align: right"]7893[/TD]
[/TR]
[TR]
[TD]ỗ[/TD]
[TD="align: right"]7895[/TD]
[/TR]
[TR]
[TD]ộ[/TD]
[TD="align: right"]7897[/TD]
[/TR]
[TR]
[TD]ớ[/TD]
[TD="align: right"]7899[/TD]
[/TR]
[TR]
[TD]ờ[/TD]
[TD="align: right"]7901[/TD]
[/TR]
[TR]
[TD]ở[/TD]
[TD="align: right"]7903[/TD]
[/TR]
[TR]
[TD]ỡ[/TD]
[TD="align: right"]7905[/TD]
[/TR]
[TR]
[TD]ợ[/TD]
[TD="align: right"]7907[/TD]
[/TR]
[TR]
[TD]ú[/TD]
[TD="align: right"]250[/TD]
[/TR]
[TR]
[TD]ù[/TD]
[TD="align: right"]249[/TD]
[/TR]
[TR]
[TD]ủ[/TD]
[TD="align: right"]7911[/TD]
[/TR]
[TR]
[TD]ũ[/TD]
[TD="align: right"]361[/TD]
[/TR]
[TR]
[TD]ụ[/TD]
[TD="align: right"]7909[/TD]
[/TR]
[TR]
[TD]ứ[/TD]
[TD="align: right"]7913[/TD]
[/TR]
[TR]
[TD]ừ[/TD]
[TD="align: right"]7915[/TD]
[/TR]
[TR]
[TD]ử[/TD]
[TD="align: right"]7917[/TD]
[/TR]
[TR]
[TD]ữ[/TD]
[TD="align: right"]7919[/TD]
[/TR]
[TR]
[TD]ự[/TD]
[TD="align: right"]7921[/TD]
[/TR]
[TR]
[TD]é[/TD]
[TD="align: right"]233[/TD]
[/TR]
[TR]
[TD]è[/TD]
[TD="align: right"]232[/TD]
[/TR]
[TR]
[TD]ẻ[/TD]
[TD="align: right"]7867[/TD]
[/TR]
[TR]
[TD]ẽ[/TD]
[TD="align: right"]7869[/TD]
[/TR]
[TR]
[TD]ẹ[/TD]
[TD="align: right"]7865[/TD]
[/TR]
[TR]
[TD]ế[/TD]
[TD="align: right"]7871[/TD]
[/TR]
[TR]
[TD]ề[/TD]
[TD="align: right"]7873[/TD]
[/TR]
[TR]
[TD]ể[/TD]
[TD="align: right"]7875[/TD]
[/TR]
[TR]
[TD]ễ[/TD]
[TD="align: right"]7877[/TD]
[/TR]
[TR]
[TD]ệ[/TD]
[TD="align: right"]7879[/TD]
[/TR]
[TR]
[TD]ý[/TD]
[TD="align: right"]253[/TD]
[/TR]
[TR]
[TD]ỳ[/TD]
[TD="align: right"]7923[/TD]
[/TR]
[TR]
[TD]ỷ[/TD]
[TD="align: right"]7927[/TD]
[/TR]
[TR]
[TD]ỹ[/TD]
[TD="align: right"]7929[/TD]
[/TR]
[TR]
[TD]ỵ[/TD]
[TD="align: right"]7925[/TD]
[/TR]
[TR]
[TD]Á[/TD]
[TD="align: right"]193[/TD]
[/TR]
[TR]
[TD]À[/TD]
[TD="align: right"]192[/TD]
[/TR]
[TR]
[TD]Ả[/TD]
[TD="align: right"]7842[/TD]
[/TR]
[TR]
[TD]Ã[/TD]
[TD="align: right"]195[/TD]
[/TR]
[TR]
[TD]Ạ[/TD]
[TD="align: right"]7840[/TD]
[/TR]
[TR]
[TD]Ắ[/TD]
[TD="align: right"]7854[/TD]
[/TR]
[TR]
[TD]Ằ[/TD]
[TD="align: right"]7856[/TD]
[/TR]
[TR]
[TD]Ẳ[/TD]
[TD="align: right"]7858[/TD]
[/TR]
[TR]
[TD]Ẵ[/TD]
[TD="align: right"]7860[/TD]
[/TR]
[TR]
[TD]Ặ[/TD]
[TD="align: right"]7862[/TD]
[/TR]
[TR]
[TD]Ấ[/TD]
[TD="align: right"]7844[/TD]
[/TR]
[TR]
[TD]Ầ[/TD]
[TD="align: right"]7846[/TD]
[/TR]
[TR]
[TD]Ẩ[/TD]
[TD="align: right"]7848[/TD]
[/TR]
[TR]
[TD]Ẫ[/TD]
[TD="align: right"]7850[/TD]
[/TR]
[TR]
[TD]Ậ[/TD]
[TD="align: right"]7852[/TD]
[/TR]
[TR]
[TD]É[/TD]
[TD="align: right"]201[/TD]
[/TR]
[TR]
[TD]È[/TD]
[TD="align: right"]200[/TD]
[/TR]
[TR]
[TD]Ẻ[/TD]
[TD="align: right"]7866[/TD]
[/TR]
[TR]
[TD]Ẽ[/TD]
[TD="align: right"]7868[/TD]
[/TR]
[TR]
[TD]Ẹ[/TD]
[TD="align: right"]7864[/TD]
[/TR]
[TR]
[TD]Ế[/TD]
[TD="align: right"]7870[/TD]
[/TR]
[TR]
[TD]Ề[/TD]
[TD="align: right"]7872[/TD]
[/TR]
[TR]
[TD]Ể[/TD]
[TD="align: right"]7874[/TD]
[/TR]
[TR]
[TD]Ễ[/TD]
[TD="align: right"]7876[/TD]
[/TR]
[TR]
[TD]Ệ[/TD]
[TD="align: right"]7878[/TD]
[/TR]
[TR]
[TD]Ó[/TD]
[TD="align: right"]211[/TD]
[/TR]
[TR]
[TD]Ò[/TD]
[TD="align: right"]210[/TD]
[/TR]
[TR]
[TD]Ỏ[/TD]
[TD="align: right"]7886[/TD]
[/TR]
[TR]
[TD]Õ[/TD]
[TD="align: right"]213[/TD]
[/TR]
[TR]
[TD]Ọ[/TD]
[TD="align: right"]7884[/TD]
[/TR]
[TR]
[TD]Ố[/TD]
[TD="align: right"]7888[/TD]
[/TR]
[TR]
[TD]Ồ[/TD]
[TD="align: right"]7890[/TD]
[/TR]
[TR]
[TD]Ổ[/TD]
[TD="align: right"]7892[/TD]
[/TR]
[TR]
[TD]Ỗ[/TD]
[TD="align: right"]7894[/TD]
[/TR]
[TR]
[TD]Ộ[/TD]
[TD="align: right"]7896[/TD]
[/TR]
[TR]
[TD]Ớ[/TD]
[TD="align: right"]7898[/TD]
[/TR]
[TR]
[TD]Ờ[/TD]
[TD="align: right"]7900[/TD]
[/TR]
[TR]
[TD]Ở[/TD]
[TD="align: right"]7902[/TD]
[/TR]
[TR]
[TD]Ỡ[/TD]
[TD="align: right"]7904[/TD]
[/TR]
[TR]
[TD]Ợ[/TD]
[TD="align: right"]7906[/TD]
[/TR]
[TR]
[TD]Ú[/TD]
[TD="align: right"]218[/TD]
[/TR]
[TR]
[TD]Ù[/TD]
[TD="align: right"]217[/TD]
[/TR]
[TR]
[TD]Ủ[/TD]
[TD="align: right"]7910[/TD]
[/TR]
[TR]
[TD]Ũ[/TD]
[TD="align: right"]360[/TD]
[/TR]
[TR]
[TD]Ụ[/TD]
[TD="align: right"]7908[/TD]
[/TR]
[TR]
[TD]Ứ[/TD]
[TD="align: right"]7912[/TD]
[/TR]
[TR]
[TD]Ừ[/TD]
[TD="align: right"]7914[/TD]
[/TR]
[TR]
[TD]Ử[/TD]
[TD="align: right"]7916[/TD]
[/TR]
[TR]
[TD]Ữ[/TD]
[TD="align: right"]7918[/TD]
[/TR]
[TR]
[TD]Ự[/TD]
[TD="align: right"]7920[/TD]
[/TR]
[TR]
[TD]Ý[/TD]
[TD="align: right"]221[/TD]
[/TR]
[TR]
[TD]Ỳ[/TD]
[TD="align: right"]7922[/TD]
[/TR]
[TR]
[TD]Ỷ[/TD]
[TD="align: right"]7926[/TD]
[/TR]
[TR]
[TD]Ỹ[/TD]
[TD="align: right"]7928[/TD]
[/TR]
[TR]
[TD]Ỵ[/TD]
[TD="align: right"]7924[/TD]
[/TR]
[TR]
[TD]đ[/TD]
[TD="align: right"]273[/TD]
[/TR]
[TR]
[TD]Ð[/TD]
[TD="align: right"]208[/TD]
[/TR]
[TR]
[TD]ă[/TD]
[TD="align: right"]259[/TD]
[/TR]
[TR]
[TD]Ă[/TD]
[TD="align: right"]258[/TD]
[/TR]
[TR]
[TD]â[/TD]
[TD="align: right"]226[/TD]
[/TR]
[TR]
[TD]Â[/TD]
[TD="align: right"]194[/TD]
[/TR]
[TR]
[TD]ê[/TD]
[TD="align: right"]234[/TD]
[/TR]
[TR]
[TD]Ê[/TD]
[TD="align: right"]202[/TD]
[/TR]
[TR]
[TD]ơ[/TD]
[TD="align: right"]417[/TD]
[/TR]
[TR]
[TD]Ơ[/TD]
[TD="align: right"]416[/TD]
[/TR]
[TR]
[TD]ô[/TD]
[TD="align: right"]244[/TD]
[/TR]
[TR]
[TD]Ô[/TD]
[TD="align: right"]212[/TD]
[/TR]
[TR]
[TD]ư[/TD]
[TD="align: right"]432[/TD]
[/TR]
[TR]
[TD]Ư[/TD]
[TD="align: right"]431[/TD]
[/TR]
[TR]
[TD]Í[/TD]
[TD="align: right"]205[/TD]
[/TR]
[TR]
[TD]Ì[/TD]
[TD="align: right"]204[/TD]
[/TR]
[TR]
[TD]Ỉ[/TD]
[TD="align: right"]7880[/TD]
[/TR]
[TR]
[TD]Ĩ[/TD]
[TD="align: right"]296[/TD]
[/TR]
[TR]
[TD]Ị[/TD]
[TD="align: right"]7882[/TD]
[/TR]
[TR]
[TD]í[/TD]
[TD="align: right"]237[/TD]
[/TR]
[TR]
[TD]ì[/TD]
[TD="align: right"]236[/TD]
[/TR]
[TR]
[TD]ỉ[/TD]
[TD="align: right"]7881[/TD]
[/TR]
[TR]
[TD]ĩ[/TD]
[TD="align: right"]297[/TD]
[/TR]
[TR]
[TD]ị[/TD]
[TD="align: right"]7883