NguyenthiH
Thành viên mới đăng ký
- Tham gia
- 11/12/16
- Bài viết
- 965
- Được thích
- 175
- Giới tính
- Nữ
Cho cái này nó thêm 1 chút là được.Chào các anh chị !!!
Em có file tính tồn cuối, nhưng khi chạy code lại lỗi, nhờ các anh chị xem sửa giúp ạ.
(Code chạy khi sheet!TonCuoi được Active ạ.)
ReDim Darr(1 To r + 100, 1 To 9)
Bạn thử mình chỉnh code 1 chút.Em cho cái Redim vào code trong sheet!TonDau thì chạy không lỗi, nhưng không lấy Số lượng tồn đầu anh @snow25 ơi, anh giúp em với.
Public Sub Ton()
Dim Dic As Object, sArr(), Darr(), i As Long, k As Long, r As Long, Rws As Long, tmp As String
Set Dic = CreateObject("Scripting.Dictionary")
'------------------------------------------------------TonDau'
With Sheets("TonDau")
sArr = .Range("B4", .Range("B4").End(xlDown)).Resize(, 5).Value
r = UBound(sArr)
ReDim Darr(1 To r + 100, 1 To 9)
End With
For i = 1 To r
tmp = sArr(i, 1) & "#" & sArr(i, 3)
If Not Dic.exists(tmp) Then
k = k + 1: Dic.Add tmp, k
Darr(k, 1) = k: Darr(k, 2) = sArr(i, 1)
Darr(k, 3) = sArr(i, 2): Darr(k, 4) = sArr(i, 3): Darr(k, 5) = sArr(i, 4): Darr(k, 6) = sArr(i, 5): Darr(k, 9) = sArr(i, 5)
Else
Rws = Dic.Item(tmp)
Darr(Rws, 6) = Darr(Rws, 6) + sArr(i, 5)
Darr(Rws, 9) = Darr(Rws, 6)
End If
Next i
'------------------------------------------------------Nhap'
With Sheets("Nhap")
sArr = .Range("B4", .Range("B4").End(xlDown)).Resize(, 5).Value
r = UBound(sArr)
End With
For i = 1 To r
tmp = sArr(i, 1) & "#" & sArr(i, 3)
If Not Dic.exists(tmp) Then
k = k + 1: Dic.Add tmp, k
Rws = Dic.Item(tmp)
Darr(Rws, 7) = Darr(Rws, 7) + sArr(i, 5)
Darr(Rws, 9) = Darr(Rws, 6) + Darr(Rws, 7)
End If
Next i
'------------------------------------------------------Xuat'
With Sheets("Xuat")
sArr = .Range("B4", .Range("B4").End(xlDown)).Resize(, 5).Value
r = UBound(sArr)
End With
For i = 1 To r
tmp = sArr(i, 1) & "#" & sArr(i, 3)
If Dic.exists(tmp) Then
Rws = Dic.Item(tmp)
Darr(Rws, 8) = Darr(Rws, 8) + sArr(i, 5)
Darr(Rws, 9) = Darr(Rws, 6) + Darr(Rws, 7) - Darr(Rws, 8)
End If
Next i
'-------------------------------------------------Ton'
For i = 1 To k
Darr(i, 9) = Darr(i, 6) + Darr(i, 7) - Darr(i, 8)
Next i
'--------------------------------------------------OK'
With Sheets("TonCuoi")
.Range("A4:I1000").ClearContents
.Range("A4:I4").Resize(k) = Darr
.Range("A4:I4").Resize(k).Borders.LineStyle = 1
.Range("F4:I4").Resize(k, 3).NumberFormat = " #,##0.00 ; [red](#,##0.00) ; - "
.Range("B4:I4").Resize(k).Sort [B4]
End With
Set Dic = Nothing
End Sub
Nhìn lướt quas thì có vẻ đoạn sau có vấn đềBạn thử mình chỉnh code 1 chút.
With Sheets("Nhap")
sArr = .Range("B4", .Range("B4").End(xlDown)).Resize(, 5).Value
r = UBound(sArr)
End With
For i = 1 To r
tmp = sArr(i, 1) & "#" & sArr(i, 3)
If Not Dic.exists(tmp) Then
k = k + 1: Dic.Add tmp, k
Rws = Dic.Item(tmp)
Darr(Rws, 7) = Darr(Rws, 7) + sArr(i, 5)
Darr(Rws, 9) = Darr(Rws, 6) + Darr(Rws, 7)
End If
Next i
Public Sub Ton()
Dim lastRow_ton As Long, lastRow_nhap As Long, lastRow_xuat As Long, k As Long, r As Long, c As Long, totalRow As Long, curr_row As Long
Dim Dic As Object, sArr(), result(), tmp As String
lastRow_ton = Worksheets("TonDau").Cells(Rows.Count, "B").End(xlUp).Row
If lastRow_ton > 3 Then totalRow = lastRow_ton - 3
lastRow_nhap = Worksheets("Nhap").Cells(Rows.Count, "B").End(xlUp).Row
If lastRow_nhap > 3 Then totalRow = totalRow + lastRow_nhap - 3
If totalRow = 0 Then Exit Sub
Set Dic = CreateObject("Scripting.Dictionary")
Dic.comparemode = vbTextCompare
ReDim result(1 To totalRow, 1 To 9)
' TonDau
If lastRow_ton > 3 Then
sArr = Worksheets("TonDau").Range("B4:F" & lastRow_ton).Value
For r = 1 To UBound(sArr)
tmp = sArr(r, 1) & "#" & sArr(r, 3)
If Not Dic.exists(tmp) Then
k = k + 1
Dic.Add tmp, k
result(k, 1) = k
For c = 1 To 5
result(k, c + 1) = sArr(r, c)
Next c
Else
curr_row = Dic.Item(tmp)
result(curr_row, 6) = result(curr_row, 6) + sArr(r, 5)
End If
Next r
End If
' Nhap
If lastRow_nhap > 3 Then
sArr = Worksheets("Nhap").Range("B4:F" & lastRow_nhap).Value
For r = 1 To UBound(sArr)
tmp = sArr(r, 1) & "#" & sArr(r, 3)
If Not Dic.exists(tmp) Then
k = k + 1
Dic.Add tmp, k
result(k, 1) = k
For c = 1 To 4
result(k, c + 1) = sArr(r, c)
Next c
result(k, 7) = sArr(r, 5)
Else
curr_row = Dic.Item(tmp)
result(curr_row, 7) = result(curr_row, 7) + sArr(r, 5)
End If
Next r
End If
' Xuat
lastRow_xuat = Worksheets("Xuat").Cells(Rows.Count, "B").End(xlUp).Row
If lastRow_xuat > 3 Then
sArr = Worksheets("Xuat").Range("B4:F" & lastRow_xuat).Value
For r = 1 To UBound(sArr)
tmp = sArr(r, 1) & "#" & sArr(r, 3)
If Not Dic.exists(tmp) Then
MsgBox "Da co xuat ma khong co ton dau va nhap. Loi tai dong " & r + 3 & " trong sheet Xuat"
Exit Sub
Else
curr_row = Dic.Item(tmp)
result(curr_row, 8) = result(curr_row, 8) + sArr(r, 5)
End If
Next r
End If
' TonCuoi
For r = 1 To k
result(r, 9) = result(r, 6) + result(r, 7) - result(r, 8)
Next r
' Nhap ket qua xuong sheet
With Worksheets("TonCuoi")
r = .Cells(Rows.Count, "B").End(xlUp).Row
If r > 3 Then .Range("A4:I" & r).Clear
.Range("A4:I4").Resize(k) = result
.Range("A4:I4").Resize(k).Borders.LineStyle = xlContinuous
.Range("F4:I4").Resize(k).NumberFormat = " #,##0.00 ; [red](#,##0.00) ; - "
.Range("B4:I4").Resize(k).Sort .Range("B4")
End With
Set Dic = Nothing
End Sub
Vâng em biết nhưng chiều nay bận em chưa sửa à.Nhìn lướt quas thì có vẻ đoạn sau có vấn đề
Đoạn trên không tính Nhập những lô (Mã vải + Mầu vải) đã tồn tại trong Tồn đầu hoặc Nhập trong những ngày trước đó. Tức bỏ qua những lô đã tồn tại ở thời điểm xét dòng nhập hiện hành. Chả nhẽ không cộng dồn vào Nhập những lô này? Nếu có rất nhiều dữ liệu thực thì sẽ thấy bị mật nhập.Mã:With Sheets("Nhap") sArr = .Range("B4", .Range("B4").End(xlDown)).Resize(, 5).Value r = UBound(sArr) End With For i = 1 To r tmp = sArr(i, 1) & "#" & sArr(i, 3) If Not Dic.exists(tmp) Then k = k + 1: Dic.Add tmp, k Rws = Dic.Item(tmp) Darr(Rws, 7) = Darr(Rws, 7) + sArr(i, 5) Darr(Rws, 9) = Darr(Rws, 6) + Darr(Rws, 7) End If Next i
Ngoài ra nếu là lô chưa tồn tại thì phải nhập Mã Vải, Loại Vải, Màu Vải và Đvt vào mảng chứ? Code ở trên không nhập chúng vào mản Darr - dòng Rws hiện hành, cũng chính là k, không có Mã Vải, Loại Vải, Màu Vải và Đvt.
-----------
Bạn thử code tôi sửa như sau. Nếu có Xuất lô mà không có trong Tồn đầu và trong Nhập (lỗi sổ sách) thì code sẽ thông báo và kết thúc.
Mã:Public Sub Ton() Dim lastRow_ton As Long, lastRow_nhap As Long, lastRow_xuat As Long, k As Long, r As Long, c As Long, totalRow As Long, curr_row As Long Dim Dic As Object, sArr(), result(), tmp As String lastRow_ton = Worksheets("TonDau").Cells(Rows.Count, "B").End(xlUp).Row If lastRow_ton > 3 Then totalRow = lastRow_ton - 3 lastRow_nhap = Worksheets("Nhap").Cells(Rows.Count, "B").End(xlUp).Row If lastRow_nhap > 3 Then totalRow = totalRow + lastRow_nhap - 3 If totalRow = 0 Then Exit Sub Set Dic = CreateObject("Scripting.Dictionary") Dic.comparemode = vbTextCompare ReDim result(1 To totalRow, 1 To 9) ' TonDau If lastRow_ton > 3 Then sArr = Worksheets("TonDau").Range("B4:F" & lastRow_ton).Value For r = 1 To UBound(sArr) tmp = sArr(r, 1) & "#" & sArr(r, 3) If Not Dic.exists(tmp) Then k = k + 1 Dic.Add tmp, k result(k, 1) = k For c = 1 To 5 result(k, c + 1) = sArr(r, c) Next c Else curr_row = Dic.Item(tmp) result(curr_row, 6) = result(curr_row, 6) + sArr(r, 5) End If Next r End If ' Nhap If lastRow_nhap > 3 Then sArr = Worksheets("Nhap").Range("B4:F" & lastRow_nhap).Value For r = 1 To UBound(sArr) tmp = sArr(r, 1) & "#" & sArr(r, 3) If Not Dic.exists(tmp) Then k = k + 1 Dic.Add tmp, k result(k, 1) = k For c = 1 To 4 result(k, c + 1) = sArr(r, c) Next c result(k, 7) = sArr(r, 5) Else curr_row = Dic.Item(tmp) result(curr_row, 7) = result(curr_row, 7) + sArr(r, 5) End If Next r End If ' Xuat lastRow_xuat = Worksheets("Xuat").Cells(Rows.Count, "B").End(xlUp).Row If lastRow_xuat > 3 Then sArr = Worksheets("Xuat").Range("B4:F" & lastRow_xuat).Value For r = 1 To UBound(sArr) tmp = sArr(r, 1) & "#" & sArr(r, 3) If Not Dic.exists(tmp) Then MsgBox "Da co xuat ma khong co ton dau va nhap. Loi tai dong " & r + 3 & " trong sheet Xuat" Exit Sub Else curr_row = Dic.Item(tmp) result(curr_row, 8) = result(curr_row, 8) + sArr(r, 5) End If Next r End If ' TonCuoi For r = 1 To k result(r, 9) = result(r, 6) + result(r, 7) - result(r, 8) Next r ' Nhap ket qua xuong sheet With Worksheets("TonCuoi") r = .Cells(Rows.Count, "B").End(xlUp).Row If r > 3 Then .Range("A4:I" & r).Clear .Range("A4:I4").Resize(k) = result .Range("A4:I4").Resize(k).Borders.LineStyle = xlContinuous .Range("F4:I4").Resize(k).NumberFormat = " #,##0.00 ; [red](#,##0.00) ; - " .Range("B4:I4").Resize(k).Sort .Range("B4") End With Set Dic = Nothing End Sub
Code ở dưới.Thầy batman1 ơi, file của em giờ thay đổi cấu trúc của sheet!TonDau (vì sheet này là của khách hàng đưa), em không biết chỉnh code lại làm sao, mong Thầy giúp em với. (Sheet!TonDau: Mã Vải = Cột B, Loại Vải = cột D, Màu Vải = cột G, Số lượng tồn đầu = cột I, Đvt = cột J)
Else ' thuc ra chi la de phong khi Ma Vai#Mau Vai duoc chia lam 2, 3 dong. Toi de phong do ban khong mo ta du lieu
curr_row = Dic.Item(tmp) ' doc ra STT cua muc Ma Vai#Mau hien hanh
result(curr_row, 6) = result(curr_row, 6) + sArr(r, 8) ' cong don TON
End If
Public Sub TonCuoi() '-------batman1
Dim lastRow_ton As Long, lastRow_nhap As Long, lastRow_xuat As Long, k As Long, r As Long, c As Long, totalRow As Long, curr_row As Long
Dim Dic As Object, sArr(), result(), tmp As String
lastRow_ton = Worksheets("TonDau").Cells(Rows.count, "B").End(xlUp).Row ' dong cuoi trong Ton Dau
If lastRow_ton > 3 Then totalRow = lastRow_ton - 3
lastRow_nhap = Worksheets("Nhap").Cells(Rows.count, "E").End(xlUp).Row ' dong cuoi trong Nhap
If lastRow_nhap > 3 Then totalRow = totalRow + lastRow_nhap - 3
If totalRow = 0 Then Exit Sub ' khong co Ton Dau va Nhap thi nghi choi
Set Dic = CreateObject("Scripting.Dictionary") ' tu dien de them Ma Vai#Mau Vai voi tu cach la KEY duy nhat
Dic.comparemode = vbTextCompare ' de phong du lieu luc co chu hoa luc co chu thuong
ReDim result(1 To totalRow, 1 To 9)
' TonDau
If lastRow_ton > 3 Then
' neu co du lieu Ton Dau thi nhap vao mang sArr
sArr = Worksheets("TonDau").Range("B4:J" & lastRow_ton).Value
For r = 1 To UBound(sArr)
tmp = sArr(r, 1) & "#" & sArr(r, 6) ' Ma Vai#Mau Vai
If Not Dic.exists(tmp) Then ' chua co Ma Vai#Mau Vai nen them vao tu dien
k = k + 1 ' STT trong mang result
Dic.Add tmp, k ' them muc duy nhat Ma Vai#Mau Vai voi tu cach la KEY va STT cua no voi tu cach la ITEM
result(k, 1) = k ' STT
result(k, 2) = sArr(r, 1) ' Ma Vai
result(k, 3) = sArr(r, 3) ' Loai Vai
result(k, 4) = sArr(r, 6) ' Mau Vai
result(k, 5) = sArr(r, 9) ' Dvt
result(k, 6) = sArr(r, 8) ' SL ton
Else ' thuc ra chi la de phong khi Ma Vai#Mau Vai duoc chia lam 2, 3 dong. Toi de phong do ban khong mo ta du lieu
curr_row = Dic.Item(tmp) ' doc ra STT cua muc Ma Vai#Mau hien hanh
result(curr_row, 6) = result(curr_row, 6) + sArr(r, 8) ' cong don TON
End If
Next r
End If
' Nhap
If lastRow_nhap > 3 Then
' neu co du lieu Nhap thi nhap vao mang sArr
sArr = Worksheets("Nhap").Range("E4:I" & lastRow_nhap).Value
For r = 1 To UBound(sArr)
tmp = sArr(r, 1) & "#" & sArr(r, 3) ' Ma Vai#Mau Vai
If Not Dic.exists(tmp) Then ' chua co Ma Vai#Mau Vai (nhap moi) nen them vao tu dien
k = k + 1 ' STT trong mang result
Dic.Add tmp, k ' them muc duy nhat Ma Vai#Mau Vai voi tu cach la KEY va STT cua no voi tu cach la ITEM
result(k, 1) = k ' STT
For c = 1 To 4
result(k, c + 1) = sArr(r, c) ' lan luot la Ma Vai, Loai Vai, Mau Vai, Dvt
Next c
result(k, 7) = sArr(r, 5) ' SL Nhap
Else ' da ton tai Ma Vai#Mau Vai nen cong don Nhap
curr_row = Dic.Item(tmp)
result(curr_row, 7) = result(curr_row, 7) + sArr(r, 5)
End If
Next r
End If
' Xuat
lastRow_xuat = Worksheets("Xuat").Cells(Rows.count, "F").End(xlUp).Row
If lastRow_xuat > 3 Then ' neu co Xuat thi moi thuc hien
' neu co du lieu Xuat thi nhap vao mang sArr
sArr = Worksheets("Xuat").Range("F4:J" & lastRow_xuat).Value
For r = 1 To UBound(sArr)
tmp = sArr(r, 1) & "#" & sArr(r, 3) ' Ma Vai#Mau Vai
If Not Dic.exists(tmp) Then ' loi Xuat hang khong co trong Ton Dau va Nhap
MsgBox "Da co xuat ma khong co ton dau va nhap. Loi tai dong " & r + 3 & " trong sheet Xuat"
Exit Sub
Else ' da co Ma Vai#Mau Vai
curr_row = Dic.Item(tmp) ' STT cua Ma Vai#Mau Vai
result(curr_row, 8) = result(curr_row, 8) + sArr(r, 5) ' cong don Xuat cho hang co Ma Vai#Mau Vai
End If
Next r
End If
' TonCuoi
For r = 1 To k
result(r, 9) = result(r, 6) + result(r, 7) - result(r, 8)
Next r
' Nhap ket qua xuong sheet
With Worksheets("TonCuoi")
r = .Cells(Rows.count, "B").End(xlUp).Row
If r > 3 Then .Range("A4:I" & r).Clear
.Range("A4:I4").Resize(k) = result
.Range("A4:I4").Resize(k).Borders.LineStyle = xlContinuous
.Range("F4:I4").Resize(k).NumberFormat = " #,##0.00 ; [red](#,##0.00) ; - "
.Range("B4:I4").Resize(k).Sort .Range("B4")
End With
Set Dic = Nothing
End Sub
Else ' thuc ra chi la de phong khi Ma Vai#Mau Vai duoc chia lam 2, 3 dong. Toi de phong do ban khong mo ta du lieu
curr_row = Dic.Item(tmp) ' doc ra STT cua muc Ma Vai#Mau hien hanh
result(curr_row, 6) = result(curr_row, 6) + sArr(r, 8) ' cong don TON