Giúp em Code VBA chạy tập hợp tổng số lượng mã hàng theo từng loại hình?

Liên hệ QC

danhculao

Thành viên hoạt động
Tham gia
26/9/16
Bài viết
147
Được thích
16
Chào các anh, chị
Anh, chị có thể giúp em công thức cho kết quả như ở sheet Tong các ô tô màu cụ thể như sau ạ:
- Dữ liệu em đưa vào là các sheet: Tab1, Tab2, Tab3,....gồm 2 bảng: bảng trên là PON, bảng dưới là PAYTV và theo từng loại hình: Triển khai mới, chuyển địa điểm, khác
- Sheet Tong sẽ link lấy tổng của các mã hàng theo tất cả các Tab có dữ liệu
Nhờ anh giúp em công thức. Em cám ơn!
 

File đính kèm

  • GOC.xlsx
    49.3 KB · Đọc: 9
Lần chỉnh sửa cuối:
Dear các Anh, chị,
Nhờ các anh, chị chỉ Giúp em Code VBA chạy tập hợp tổng số lượng mã hàng theo từng loại hình, dữ liệu dựa vào các sheet: Tab1, Tab2, Tab3,...Số lượng Sheet khoảng vài chục sheet nếu em chạy công thức file sẽ không chạy nỗi. Nên nhờ các anh, chị chỉ giúp ạ.
Em cám ơn
Cho cái ví dụ kết quả khi chạy code nào bạn.
 
Mà cho mình hỏi tại sao cần 2 bảng vậy.sao không để 1 bảng cho nó gọn.
- Các sheet Tab1, tab2, tab3,.... có rất nhiều sheet ạ, anh chỉ giúp code sau này em insert thêm sheet code vẫn chạy được ạ
- 2 bảng là vì: sheet ở trên PON: là internet, còn sheet bên dưới là: truyền hình ạ
(Em gửi lại file ạ)
 
- Các sheet Tab1, tab2, tab3,.... có rất nhiều sheet ạ, anh chỉ giúp code sau này em insert thêm sheet code vẫn chạy được ạ
- 2 bảng là vì: sheet ở trên PON: là internet, còn sheet bên dưới là: truyền hình ạ
(Em gửi lại file ạ)
Góp ý cho bạn:
1/ Cách theo dõi của bạn chưa hợp lý, lý do: Mới có 3 sheet mà đã rối, nếu vài chục sheet thì sẽ như thế nào?
2/ Tốt nhất là chỉ nên theo dõi chung trong 1 sheet thì việc tổng hợp sẽ dễ dàng hơn.
3/ Nếu muốn mỗi Tab 1 sheet thì dựa vào cột B để tách nó ra.
 

File đính kèm

  • GPE_NXT.xlsx
    20.9 KB · Đọc: 3
- Các sheet Tab1, tab2, tab3,.... có rất nhiều sheet ạ, anh chỉ giúp code sau này em insert thêm sheet code vẫn chạy được ạ
- 2 bảng là vì: sheet ở trên PON: là internet, còn sheet bên dưới là: truyền hình ạ
(Em gửi lại file ạ)
Bạn thử code này xem nhé.
 

File đính kèm

  • GPE_NXT (2).xlsm
    28.6 KB · Đọc: 10
Bạn thử code này xem nhé.
Anh ơi, sheet "Tong" giờ mình thêm nút Run chẳng hạn, khi mình bấm vào thì dữ liệu sẽ được xô lên có được không ạ. Anh chỉ thêm giúp em với ạ.
Em cám ơn!
Bài đã được tự động gộp:

Góp ý cho bạn:
1/ Cách theo dõi của bạn chưa hợp lý, lý do: Mới có 3 sheet mà đã rối, nếu vài chục sheet thì sẽ như thế nào?
2/ Tốt nhất là chỉ nên theo dõi chung trong 1 sheet thì việc tổng hợp sẽ dễ dàng hơn.
3/ Nếu muốn mỗi Tab 1 sheet thì dựa vào cột B để tách nó ra.
Dạ, vì dữ liệu nhập tay sẽ đưa vào các sheet: Tab1, Tab2, Tab3, Tab4,... ạ, nên theo dõi chung trong 1 sheet không được ạ
 
File này dữ liệu khác với dữ liệu bạn gửi lúc đầu nên không chạy được vì trước nó có 3 quận giờ còn có 2.Nên nó bị lỗi.
Dạ, dữ liệu em đưa vào là các tab1, tab2,...sẽ không cố định 3 quận anh ạ, nên nhờ anh chỉ giúp ạ, kết quả là tổng số lượng của hàng theo từng loại hình. Theo như kết quả ở sheet Tổng. Nhờ anh chỉ giúp ạ. Em cám ơn
 
Dạ, dữ liệu em đưa vào là các tab1, tab2,...sẽ không cố định 3 quận anh ạ, nên nhờ anh chỉ giúp ạ, kết quả là tổng số lượng của hàng theo từng loại hình. Theo như kết quả ở sheet Tổng. Nhờ anh chỉ giúp ạ. Em cám ơn
Bạn đưa dữ liệu thực tế lên mình xem nào.
 
Anh ơi, nhờ anh sửa giúp em code với ạ, em chạy báo lỗi ạ
Em cám ơn
Dùng tạm code
Mã:
Option Explicit

Sub TongHop()
  Dim sArr(), td(), GoiCuoc, Res(), Res2(), sh As Worksheet, Dic As Object, iKey As String
  Dim eCol As Long, sCol As Long, eRow As Long, i As Long, j As Long
  Dim k As Long, ik As Long, jk As Long, n As Long, q As Long
    
  With Sheets("Tong")
    td = .Range("A1:E1").Value
    eRow = .Range("E" & Rows.Count).End(xlUp).Row
    eCol = .UsedRange.Columns.Count
    If eRow > 1 Then .Range("A2:A" & eRow).Resize(, eCol).Clear
  End With
  Application.ScreenUpdating = False
  Set Dic = CreateObject("scripting.dictionary")
  GoiCuoc = Array("PON", "PAYTV")
  For n = 0 To UBound(GoiCuoc)
    sCol = 5: k = 1
    ReDim Res(1 To 1000, 1 To sCol)
    For Each sh In ThisWorkbook.Worksheets
      If Not sh.Name Like "T?ng" Then
        eRow = sh.Range("F" & Rows.Count).End(xlUp).Row + 1
        For i = 2 To eRow - 1
          If UCase(sh.Range("A" & i).Value) = GoiCuoc(n) Then
            eCol = sh.Cells(i - 1, Columns.Count).End(1).Column
            sArr = sh.Range(sh.Cells(i - 1, "A"), sh.Cells(eRow, eCol)).Value
            Exit For
          End If
        Next i
        For j = 6 To eCol
          iKey = sArr(1, j)
          If Dic.exists(iKey) = False Then
            sCol = sCol + 1
            ReDim Preserve Res(1 To 1000, 1 To sCol)
            Res(1, sCol) = iKey
            Dic.Add iKey, sCol
          End If
        Next j
        Res(2, 1) = GoiCuoc(n)
        For i = 2 To UBound(sArr)
          If Len(sArr(i, 3)) = 0 Then Exit For
          iKey = sArr(i, 3) & "#" & sArr(i, 4)
          If Dic.exists(iKey) = False Then
            k = k + 1
            Dic.Add iKey, k
            Res(k, 2) = k - 1
            Res(k, 3) = sArr(i, 3)
            Res(k, 4) = sArr(i, 4)
          End If
          ik = Dic.Item(iKey)
          Res(ik, 5) = Res(ik, 5) + sArr(i, 5)
          For j = 6 To eCol
            jk = Dic.Item(sArr(1, j))
            If sArr(i, j) > 0 Then
              Res(ik, jk) = Res(ik, jk) + sArr(i, j)
            End If
          Next j
        Next i
      End If
    Next
    ik = 1
    If k > 1 Then
      Dic.RemoveAll
      ReDim Res2(1 To k + 2, 1 To sCol)
      Res2(k + 2, 5) = "Tong Cong"
      For j = 6 To sCol
        Res2(1, j) = Res(1, j)
      Next j
      For i = 2 To k
        iKey = Res(i, 3)
        If Dic.exists(iKey) = False Then
          Dic.Add iKey, Empty
          For q = i To k
            If iKey = Res(q, 3) Then
              ik = ik + 1
              For j = 1 To sCol
                Res2(ik, j) = Res(q, j)
                If j > 5 Then Res2(k + 2, j) = Res2(k + 2, j) + Res(ik, j)
              Next j
            End If
          Next q
        End If
      Next i
      With Sheets("Tong")
        eRow = .Range("E" & Rows.Count).End(xlUp).Row
        If eRow > 2 Then
          eRow = eRow + 2
          .Range("A" & eRow).Resize(, 5).Value = td
        End If
        .Range("A" & eRow + 1).Resize(k + 2, sCol).Value = Res2
        .Range("A" & eRow).Resize(k + 3, sCol).Borders.LineStyle = 1
      End With
    End If
    Dic.RemoveAll
  Next n
  Application.ScreenUpdating = True
End Sub
 

File đính kèm

  • GPE_DATA.xlsm
    36.1 KB · Đọc: 11
Dùng tạm code
Mã:
Option Explicit

Sub TongHop()
  Dim sArr(), td(), GoiCuoc, Res(), Res2(), sh As Worksheet, Dic As Object, iKey As String
  Dim eCol As Long, sCol As Long, eRow As Long, i As Long, j As Long
  Dim k As Long, ik As Long, jk As Long, n As Long, q As Long
   
  With Sheets("Tong")
    td = .Range("A1:E1").Value
    eRow = .Range("E" & Rows.Count).End(xlUp).Row
    eCol = .UsedRange.Columns.Count
    If eRow > 1 Then .Range("A2:A" & eRow).Resize(, eCol).Clear
  End With
  Application.ScreenUpdating = False
  Set Dic = CreateObject("scripting.dictionary")
  GoiCuoc = Array("PON", "PAYTV")
  For n = 0 To UBound(GoiCuoc)
    sCol = 5: k = 1
    ReDim Res(1 To 1000, 1 To sCol)
    For Each sh In ThisWorkbook.Worksheets
      If Not sh.Name Like "T?ng" Then
        eRow = sh.Range("F" & Rows.Count).End(xlUp).Row + 1
        For i = 2 To eRow - 1
          If UCase(sh.Range("A" & i).Value) = GoiCuoc(n) Then
            eCol = sh.Cells(i - 1, Columns.Count).End(1).Column
            sArr = sh.Range(sh.Cells(i - 1, "A"), sh.Cells(eRow, eCol)).Value
            Exit For
          End If
        Next i
        For j = 6 To eCol
          iKey = sArr(1, j)
          If Dic.exists(iKey) = False Then
            sCol = sCol + 1
            ReDim Preserve Res(1 To 1000, 1 To sCol)
            Res(1, sCol) = iKey
            Dic.Add iKey, sCol
          End If
        Next j
        Res(2, 1) = GoiCuoc(n)
        For i = 2 To UBound(sArr)
          If Len(sArr(i, 3)) = 0 Then Exit For
          iKey = sArr(i, 3) & "#" & sArr(i, 4)
          If Dic.exists(iKey) = False Then
            k = k + 1
            Dic.Add iKey, k
            Res(k, 2) = k - 1
            Res(k, 3) = sArr(i, 3)
            Res(k, 4) = sArr(i, 4)
          End If
          ik = Dic.Item(iKey)
          Res(ik, 5) = Res(ik, 5) + sArr(i, 5)
          For j = 6 To eCol
            jk = Dic.Item(sArr(1, j))
            If sArr(i, j) > 0 Then
              Res(ik, jk) = Res(ik, jk) + sArr(i, j)
            End If
          Next j
        Next i
      End If
    Next
    ik = 1
    If k > 1 Then
      Dic.RemoveAll
      ReDim Res2(1 To k + 2, 1 To sCol)
      Res2(k + 2, 5) = "Tong Cong"
      For j = 6 To sCol
        Res2(1, j) = Res(1, j)
      Next j
      For i = 2 To k
        iKey = Res(i, 3)
        If Dic.exists(iKey) = False Then
          Dic.Add iKey, Empty
          For q = i To k
            If iKey = Res(q, 3) Then
              ik = ik + 1
              For j = 1 To sCol
                Res2(ik, j) = Res(q, j)
                If j > 5 Then Res2(k + 2, j) = Res2(k + 2, j) + Res(ik, j)
              Next j
            End If
          Next q
        End If
      Next i
      With Sheets("Tong")
        eRow = .Range("E" & Rows.Count).End(xlUp).Row
        If eRow > 2 Then
          eRow = eRow + 2
          .Range("A" & eRow).Resize(, 5).Value = td
        End If
        .Range("A" & eRow + 1).Resize(k + 2, sCol).Value = Res2
        .Range("A" & eRow).Resize(k + 3, sCol).Borders.LineStyle = 1
      End With
    End If
    Dic.RemoveAll
  Next n
  Application.ScreenUpdating = True
End Sub
Anh ơi, em thêm sheet MaHang để lấy thêm mã vật tư vào bên sheet Tong
và cột D Loại hình bên sheet Tong gom chung lại 3 loại: Triển khai mới, chuyển địa điểm, khác thôi ạ
Nhờ anh chỉnh lại code giúp em với ạ
Em cám ơn!
 

File đính kèm

  • GPE_DATA.xlsm
    50 KB · Đọc: 5
Anh ơi, em thêm sheet MaHang để lấy thêm mã vật tư vào bên sheet Tong
và cột D Loại hình bên sheet Tong gom chung lại 3 loại: Triển khai mới, chuyển địa điểm, khác thôi ạ
Nhờ anh chỉnh lại code giúp em với ạ
Em cám ơn!
May Khong co tieng Viet !!!
Kiem tra lai code
Mã:
Option Explicit

Sub TongHop2()
  Dim sArr(), td(), GoiCuoc, Res(), Res2(), Ma(), sh As Worksheet, Dic As Object, Dic2 As Object, iKey As String
  Dim eCol As Long, sCol As Long, eRow As Long, i As Long, j As Long
  Dim k As Long, ik As Long, jk As Long, n As Long, q As Long
 
  Set Dic = CreateObject("scripting.dictionary")
  Set Dic2 = CreateObject("scripting.dictionary")
  With Sheets("MaHang")
    eRow = .Range("B" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("B2:C" & eRow).Value
      For i = 1 To UBound(sArr)
        Dic2.Item(sArr(i, 2)) = sArr(i, 1)
      Next i
    End If
  End With
  With Sheets("Tong")
    td = .Range("A1:E1").Value
    eRow = .Range("E" & Rows.Count).End(xlUp).Row
    eCol = .UsedRange.Columns.Count
    If eRow > 1 Then .Range("A2:A" & eRow).Resize(, eCol).Clear
  End With
  Application.ScreenUpdating = False
  GoiCuoc = Array("PON", "PAYTV")
  For n = 0 To UBound(GoiCuoc)
    sCol = 5: k = 1
    ReDim Res(1 To 1000, 1 To sCol)
    For Each sh In ThisWorkbook.Worksheets
      If Not sh.Name Like "T?ng" Then
        eRow = sh.Range("F" & Rows.Count).End(xlUp).Row + 1
        For i = 2 To eRow - 1
          If UCase(sh.Range("A" & i).Value) = GoiCuoc(n) Then
            eCol = sh.Cells(i - 1, Columns.Count).End(1).Column
            sArr = sh.Range(sh.Cells(i - 1, "A"), sh.Cells(eRow, eCol)).Value
            Exit For
          End If
        Next i
        For j = 6 To eCol
          iKey = sArr(1, j)
          If Dic.exists(iKey) = False Then
            sCol = sCol + 1
            ReDim Preserve Res(1 To 1000, 1 To sCol)
            Res(1, sCol) = iKey
            Dic.Add iKey, sCol
          End If
        Next j
        Res(2, 1) = GoiCuoc(n)
        For i = 2 To UBound(sArr)
          If Len(sArr(i, 3)) = 0 Then Exit For
          'iKey = sArr(i, 3) & "#" & sArr(i, 4)
          iKey = sArr(i, 4)
          If Dic.exists(iKey) = False Then
            k = k + 1
            Dic.Add iKey, k
            Res(k, 2) = k - 1
            'Res(k, 3) = sArr(i, 3)
            Res(k, 4) = sArr(i, 4)
          End If
          ik = Dic.Item(iKey)
          Res(ik, 5) = Res(ik, 5) + sArr(i, 5)
          For j = 6 To eCol
            jk = Dic.Item(sArr(1, j))
            If sArr(i, j) > 0 Then
              Res(ik, jk) = Res(ik, jk) + sArr(i, j)
            End If
          Next j
        Next i
      End If
    Next
    ik = 1
    If k > 1 Then
      Dic.RemoveAll
      ReDim Res2(1 To k + 2, 1 To sCol)
      Res2(k + 2, 5) = "Tong Cong"
      For j = 6 To sCol
        Res2(1, j) = Res(1, j)
      Next j
      For i = 2 To k
        iKey = Res(i, 3)
        If Dic.exists(iKey) = False Then
          Dic.Add iKey, Empty
          For q = i To k
            If iKey = Res(q, 3) Then
              ik = ik + 1
              For j = 1 To sCol
                Res2(ik, j) = Res(q, j)
                If j > 5 Then Res2(k + 2, j) = Res2(k + 2, j) + Res(ik, j)
              Next j
            End If
          Next q
        End If
      Next i
      With Sheets("Tong")
        eRow = .Range("E" & Rows.Count).End(xlUp).Row
        If eRow > 2 Then
          eRow = eRow + 2
          .Range("A" & eRow).Resize(, 5).Value = td
        End If
        .Range("A" & eRow + 1).Resize(k + 2, sCol).Value = Res2
        .Range("A" & eRow).Resize(k + 3, sCol).Borders.LineStyle = 1
        ReDim Res(1 To 1, 1 To sCol - 5)
        For j = 6 To sCol
          Res(1, j - 5) = Dic2.Item(Res2(1, j))
        Next j
        .Range("F" & eRow).Resize(, sCol - 5).Value = Res
      End With
    End If
    Dic.RemoveAll
  Next n
  Application.ScreenUpdating = True
End Sub
 
May Khong co tieng Viet !!!
Kiem tra lai code
Mã:
Option Explicit

Sub TongHop2()
  Dim sArr(), td(), GoiCuoc, Res(), Res2(), Ma(), sh As Worksheet, Dic As Object, Dic2 As Object, iKey As String
  Dim eCol As Long, sCol As Long, eRow As Long, i As Long, j As Long
  Dim k As Long, ik As Long, jk As Long, n As Long, q As Long

  Set Dic = CreateObject("scripting.dictionary")
  Set Dic2 = CreateObject("scripting.dictionary")
  With Sheets("MaHang")
    eRow = .Range("B" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("B2:C" & eRow).Value
      For i = 1 To UBound(sArr)
        Dic2.Item(sArr(i, 2)) = sArr(i, 1)
      Next i
    End If
  End With
  With Sheets("Tong")
    td = .Range("A1:E1").Value
    eRow = .Range("E" & Rows.Count).End(xlUp).Row
    eCol = .UsedRange.Columns.Count
    If eRow > 1 Then .Range("A2:A" & eRow).Resize(, eCol).Clear
  End With
  Application.ScreenUpdating = False
  GoiCuoc = Array("PON", "PAYTV")
  For n = 0 To UBound(GoiCuoc)
    sCol = 5: k = 1
    ReDim Res(1 To 1000, 1 To sCol)
    For Each sh In ThisWorkbook.Worksheets
      If Not sh.Name Like "T?ng" Then
        eRow = sh.Range("F" & Rows.Count).End(xlUp).Row + 1
        For i = 2 To eRow - 1
          If UCase(sh.Range("A" & i).Value) = GoiCuoc(n) Then
            eCol = sh.Cells(i - 1, Columns.Count).End(1).Column
            sArr = sh.Range(sh.Cells(i - 1, "A"), sh.Cells(eRow, eCol)).Value
            Exit For
          End If
        Next i
        For j = 6 To eCol
          iKey = sArr(1, j)
          If Dic.exists(iKey) = False Then
            sCol = sCol + 1
            ReDim Preserve Res(1 To 1000, 1 To sCol)
            Res(1, sCol) = iKey
            Dic.Add iKey, sCol
          End If
        Next j
        Res(2, 1) = GoiCuoc(n)
        For i = 2 To UBound(sArr)
          If Len(sArr(i, 3)) = 0 Then Exit For
          'iKey = sArr(i, 3) & "#" & sArr(i, 4)
          iKey = sArr(i, 4)
          If Dic.exists(iKey) = False Then
            k = k + 1
            Dic.Add iKey, k
            Res(k, 2) = k - 1
            'Res(k, 3) = sArr(i, 3)
            Res(k, 4) = sArr(i, 4)
          End If
          ik = Dic.Item(iKey)
          Res(ik, 5) = Res(ik, 5) + sArr(i, 5)
          For j = 6 To eCol
            jk = Dic.Item(sArr(1, j))
            If sArr(i, j) > 0 Then
              Res(ik, jk) = Res(ik, jk) + sArr(i, j)
            End If
          Next j
        Next i
      End If
    Next
    ik = 1
    If k > 1 Then
      Dic.RemoveAll
      ReDim Res2(1 To k + 2, 1 To sCol)
      Res2(k + 2, 5) = "Tong Cong"
      For j = 6 To sCol
        Res2(1, j) = Res(1, j)
      Next j
      For i = 2 To k
        iKey = Res(i, 3)
        If Dic.exists(iKey) = False Then
          Dic.Add iKey, Empty
          For q = i To k
            If iKey = Res(q, 3) Then
              ik = ik + 1
              For j = 1 To sCol
                Res2(ik, j) = Res(q, j)
                If j > 5 Then Res2(k + 2, j) = Res2(k + 2, j) + Res(ik, j)
              Next j
            End If
          Next q
        End If
      Next i
      With Sheets("Tong")
        eRow = .Range("E" & Rows.Count).End(xlUp).Row
        If eRow > 2 Then
          eRow = eRow + 2
          .Range("A" & eRow).Resize(, 5).Value = td
        End If
        .Range("A" & eRow + 1).Resize(k + 2, sCol).Value = Res2
        .Range("A" & eRow).Resize(k + 3, sCol).Borders.LineStyle = 1
        ReDim Res(1 To 1, 1 To sCol - 5)
        For j = 6 To sCol
          Res(1, j - 5) = Dic2.Item(Res2(1, j))
        Next j
        .Range("F" & eRow).Resize(, sCol - 5).Value = Res
      End With
    End If
    Dic.RemoveAll
  Next n
  Application.ScreenUpdating = True
End Sub
Dạ, Tổng số lượng các vật tư theo từng loại hình không đúng rồi anh ạ
 

File đính kèm

  • GPE_DATA.xlsm
    50.1 KB · Đọc: 9
Web KT
Back
Top Bottom