Trích lọc dữ liệu theo nhiều điều kiện

Liên hệ QC

huonglien1901

GPE là ngôi nhà thứ 2 của tôi!!!
Tham gia
17/4/16
Bài viết
2,701
Được thích
2,434
Giới tính
Nam
Nghề nghiệp
Nhân viên kỹ thuật in ấn
Chào mọi người!

Em có vấn đề nhờ mọi người hỗ trợ.

1 .Em muốn gõ mã nhà cung cấp(Supplier code) ở cột C3 thì sẽ trích lọc dữ liệu như trong sheet LOC(em có làm ví dụ)
Dữ liệu từ dòng D1:K3 được lấy ở sheet MOQ,sheet LGH, sheet GIO COLLECT, sheet LDH ở file TIMKIEM1.
Dữ liệu ở cột A,B được lấy ở sheet CAR $ PROPOSAL.
2 Khi em gõ vào cột C2 thì sẽ lấy dữ liệu đó ra( như yêu cầu 1)
- Tại vì một nhà cung cấp có nhiều số PO đơn hàng(Order no_1923030791395 số này được gọi số PO đơn hàng).
trong file em làm:
Nhà cung cấp ANH HONG có 2 số PO đơn hàng:
1922030737659
1922030737662
2 số này hiện ra ở ô A3 giống như kiểu tạo Validation.
Em sẽ chọn 01 số thì dữ liệu sẽ thay đổi theo số PO đơn hàng này.
- Dữ liệu khi trích lọc ra tự động căn chỉnh dòng và cột.

Không biết em diễn đạt vậy mọi người có hiểu chưa?

Nhờ mọi người hỗ trợ.

Em cảm ơn mọi người nhiều!
 

File đính kèm

Chào mọi người!

Em có vấn đề nhờ mọi người hỗ trợ.

1 .Em muốn gõ mã nhà cung cấp(Supplier code) ở cột C3 thì sẽ trích lọc dữ liệu như trong sheet LOC(em có làm ví dụ)
Dữ liệu từ dòng D1:K3 được lấy ở sheet MOQ,sheet LGH, sheet GIO COLLECT, sheet LDH ở file TIMKIEM1.
Dữ liệu ở cột A,B được lấy ở sheet CAR $ PROPOSAL.
2 Khi em gõ vào cột C2 thì sẽ lấy dữ liệu đó ra( như yêu cầu 1)
- Tại vì một nhà cung cấp có nhiều số PO đơn hàng(Order no_1923030791395 số này được gọi số PO đơn hàng).
trong file em làm:
Nhà cung cấp ANH HONG có 2 số PO đơn hàng:
1922030737659
1922030737662
2 số này hiện ra ở ô A3 giống như kiểu tạo Validation.
Em sẽ chọn 01 số thì dữ liệu sẽ thay đổi theo số PO đơn hàng này.
- Dữ liệu khi trích lọc ra tự động căn chỉnh dòng và cột.

Không biết em diễn đạt vậy mọi người có hiểu chưa?

Nhờ mọi người hỗ trợ.

Em cảm ơn mọi người nhiều!
Trong file không có sheet "CAR 14.04"
Nhìn dữ liệu không biết lấy từ cột nào
Gởi lại file với ghi chú thật cụ thể từng cột lấy dữ liệu từ cột nào
Kết quả chỉ liệt kê hay tính tổng? ghi rỏ từng cột
Cho ví dụ 1 dòng
 
Upvote 0
Trong file không có sheet "CAR 14.04"
Nhìn dữ liệu không biết lấy từ cột nào
Gởi lại file với ghi chú thật cụ thể từng cột lấy dữ liệu từ cột nào
Kết quả chỉ liệt kê hay tính tổng? ghi rỏ từng cột
Cho ví dụ 1 dòng
Trong file em có làm ví dụ và ghi chú ở sheet LOC.
- Trong file có sheet CAR 14.4 được thay thành sheet( CAR ORDER).
- Kết quả này chỉ liệt kê ra thôi Anh , không tính tổng.

Anh còn vấn đề nào chưa rõ, Anh hỏi em trả lời cho Anh.

Em cảm ơn Anh rất nhiều!
 

File đính kèm

Upvote 0
Trong file em có làm ví dụ và ghi chú ở sheet LOC.
- Trong file có sheet CAR 14.4 được thay thành sheet( CAR ORDER).
- Kết quả này chỉ liệt kê ra thôi Anh , không tính tổng.

Anh còn vấn đề nào chưa rõ, Anh hỏi em trả lời cho Anh.

Em cảm ơn Anh rất nhiều!
Kết quả ở file nào, sheet nào?
 
Upvote 0
Kết quả trả về sheet Loc của file car proposal đó Anh.
Chép code vào sheet loc, lưu file theo đuôi: .xlsm hay .xlsb
2 file phải cùng thư mục
Mã:
Dim eR As Long

Private Sub Worksheet_Change(ByVal Target As Range)
  Dim eRow As Long, Rng As Range
  If Target.Address(0, 0) = "A3" Then
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    
    eRow = Range("A" & Rows.Count).End(xlUp).Row
    If eRow > 4 Then Range("A5:K" & eRow).Clear

    Call Cot_A_K(Target.Value)
    Set Rng = Range("D3:K3")
    Rng.ClearContents
    Call Dong_3(Rng, Range("C3").Value)

    Application.ScreenUpdating = True
    Application.EnableEvents = True
  End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Target.Address(0, 0) = "A3" Then
    Application.EnableEvents = False
    Dim sArr(), Res(), oSList As Object
    Dim i As Long, eRow As Long, sRow As Long, k As Long
 
    With Sheets("CAR proposal")
      eRow = .Range("C" & Rows.Count).End(xlUp).Row
      If eRow <> eR Then
        eR = eRow
        sArr = .Range("C2:C" & eR).Value
        sRow = UBound(sArr)
        Set oSList = CreateObject("System.Collections.SortedList")
        For i = 1 To sRow
          If oSList.ContainsKey(sArr(i, 1)) = False Then oSList.Add sArr(i, 1), ""
        Next
        k = oSList.Count - 1
        ReDim Res(0 To k)
        For i = 0 To k
          Res(i) = oSList.GetKey(i)
        Next i
        Set oSList = Nothing
        Range("A3").Validation.Delete
        Range("A3").Validation.Add 3, , , Join(Res, ",")
      End If
    End With
    Application.EnableEvents = True
  End If
End Sub

Private Sub Dong_3(ByRef Rng, ByVal iKey As String)
  Dim wb As Workbook, sArr()
  Dim i As Long, eRow As Long, sRow As Long
  
  Set wb = Workbooks.Open(ThisWorkbook.Path & "\DU LIEU TIM KIEM1.xlsm")
  With wb.Sheets("MOQ")
    eRow = .Range("B" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("B2:H" & eRow).Value
      sRow = UBound(sArr)
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          If Len(sArr(i, 4)) > 0 Then
            Rng(1, 1) = sArr(i, 4)
          ElseIf Len(sArr(i, 7)) > 0 Then
            Rng(1, 1) = sArr(i, 7)
          End If
          If Len(sArr(i, 3)) > 0 Then
            Rng(1, 2) = sArr(i, 3)
          ElseIf Len(sArr(i, 5)) > 0 Then
            Rng(1, 2) = sArr(i, 5)
          End If
          Exit For
        End If
      Next i
    End If
  End With

  With wb.Sheets("LGH")
    eRow = .Range("B" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("B2:I" & eRow).Value
      sRow = UBound(sArr)
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          If Len(sArr(i, 8)) > 0 Then Rng(1, 3) = sArr(i, 8)
          If Len(sArr(i, 3)) > 0 Then Rng(1, 4) = sArr(i, 3)
          Exit For
        End If
      Next i
    End If
  End With
      
  With wb.Sheets("GIO COLLECT")
    eRow = .Range("A" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("A2:C" & eRow).Value
      sRow = UBound(sArr)
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          If Len(sArr(i, 3)) > 0 Then Rng(1, 5) = sArr(i, 3)
          Exit For
        End If
      Next i
    End If
  End With
 
  With wb.Sheets("LDH")
    eRow = .Range("B" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("B2:P" & eRow).Value
      sRow = UBound(sArr)
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          If Len(sArr(i, 15)) > 0 Then Rng(1, 6) = Replace(Application.Trim(Replace(sArr(i, 15), ",", " ")), " ", ",")
          If Len(sArr(i, 4)) > 0 Then Rng(1, 7) = sArr(i, 4)
          If Len(sArr(i, 5)) > 0 Then Rng(1, 8) = sArr(i, 5)
          Exit For
        End If
      Next i
    End If
  End With
  wb.Close False
End Sub

Private Sub Cot_A_K(ByVal iKey As String)
    Dim sArr(), Res(), colArr()
    Dim i As Long, eRow As Long, q As Long, sRow As Long, k As Long, j As Long
    
    With Sheets("CAR proposal")
      eRow = .Range("C" & Rows.Count).End(xlUp).Row
      q = Application.CountIf(.Range("C2:C" & eRow), iKey)
      If q > 0 Then sArr = .Range("C2:AK" & eRow).Value
    End With
    If q > 0 Then
      sRow = UBound(sArr)
      colArr = Array(, 9, 11, 21, 22, 23, 26, 31, 32, 33, 34, 35)
      ReDim Res(1 To q, 1 To UBound(colArr))
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          k = k + 1
          If k = 1 Then
            Range("B3") = sArr(i, 3)
            Range("C3") = sArr(i, 2)
          End If
          For j = 1 To UBound(colArr)
            Res(k, j) = sArr(i, colArr(j))
          Next j
        End If
      Next
      
      Range("A5").Resize(k).NumberFormat = "@"
      Range("A5:K5").Resize(k) = Res
      Range("A5:K5").Resize(k).Borders.LineStyle = 1
    End If
End Sub
 

File đính kèm

Upvote 0
Chép code vào sheet loc, lưu file theo đuôi: .xlsm hay .xlsb
2 file phải cùng thư mục
Mã:
Dim eR As Long

Private Sub Worksheet_Change(ByVal Target As Range)
  Dim eRow As Long, Rng As Range
  If Target.Address(0, 0) = "A3" Then
    Application.EnableEvents = False
    Application.ScreenUpdating = False
   
    eRow = Range("A" & Rows.Count).End(xlUp).Row
    If eRow > 4 Then Range("A5:K" & eRow).Clear

    Call Cot_A_K(Target.Value)
    Set Rng = Range("D3:K3")
    Rng.ClearContents
    Call Dong_3(Rng, Range("C3").Value)

    Application.ScreenUpdating = True
    Application.EnableEvents = True
  End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Target.Address(0, 0) = "A3" Then
    Application.EnableEvents = False
    Dim sArr(), Res(), oSList As Object
    Dim i As Long, eRow As Long, sRow As Long, k As Long

    With Sheets("CAR proposal")
      eRow = .Range("C" & Rows.Count).End(xlUp).Row
      If eRow <> eR Then
        eR = eRow
        sArr = .Range("C2:C" & eR).Value
        sRow = UBound(sArr)
        Set oSList = CreateObject("System.Collections.SortedList")
        For i = 1 To sRow
          If oSList.ContainsKey(sArr(i, 1)) = False Then oSList.Add sArr(i, 1), ""
        Next
        k = oSList.Count - 1
        ReDim Res(0 To k)
        For i = 0 To k
          Res(i) = oSList.GetKey(i)
        Next i
        Set oSList = Nothing
        Range("A3").Validation.Delete
        Range("A3").Validation.Add 3, , , Join(Res, ",")
      End If
    End With
    Application.EnableEvents = True
  End If
End Sub

Private Sub Dong_3(ByRef Rng, ByVal iKey As String)
  Dim wb As Workbook, sArr()
  Dim i As Long, eRow As Long, sRow As Long
 
  Set wb = Workbooks.Open(ThisWorkbook.Path & "\DU LIEU TIM KIEM1.xlsm")
  With wb.Sheets("MOQ")
    eRow = .Range("B" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("B2:H" & eRow).Value
      sRow = UBound(sArr)
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          If Len(sArr(i, 4)) > 0 Then
            Rng(1, 1) = sArr(i, 4)
          ElseIf Len(sArr(i, 7)) > 0 Then
            Rng(1, 1) = sArr(i, 7)
          End If
          If Len(sArr(i, 3)) > 0 Then
            Rng(1, 2) = sArr(i, 3)
          ElseIf Len(sArr(i, 5)) > 0 Then
            Rng(1, 2) = sArr(i, 5)
          End If
          Exit For
        End If
      Next i
    End If
  End With

  With wb.Sheets("LGH")
    eRow = .Range("B" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("B2:I" & eRow).Value
      sRow = UBound(sArr)
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          If Len(sArr(i, 8)) > 0 Then Rng(1, 3) = sArr(i, 8)
          If Len(sArr(i, 3)) > 0 Then Rng(1, 4) = sArr(i, 3)
          Exit For
        End If
      Next i
    End If
  End With
     
  With wb.Sheets("GIO COLLECT")
    eRow = .Range("A" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("A2:C" & eRow).Value
      sRow = UBound(sArr)
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          If Len(sArr(i, 3)) > 0 Then Rng(1, 5) = sArr(i, 3)
          Exit For
        End If
      Next i
    End If
  End With

  With wb.Sheets("LDH")
    eRow = .Range("B" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("B2:P" & eRow).Value
      sRow = UBound(sArr)
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          If Len(sArr(i, 15)) > 0 Then Rng(1, 6) = Replace(Application.Trim(Replace(sArr(i, 15), ",", " ")), " ", ",")
          If Len(sArr(i, 4)) > 0 Then Rng(1, 7) = sArr(i, 4)
          If Len(sArr(i, 5)) > 0 Then Rng(1, 8) = sArr(i, 5)
          Exit For
        End If
      Next i
    End If
  End With
  wb.Close False
End Sub

Private Sub Cot_A_K(ByVal iKey As String)
    Dim sArr(), Res(), colArr()
    Dim i As Long, eRow As Long, q As Long, sRow As Long, k As Long, j As Long
   
    With Sheets("CAR proposal")
      eRow = .Range("C" & Rows.Count).End(xlUp).Row
      q = Application.CountIf(.Range("C2:C" & eRow), iKey)
      If q > 0 Then sArr = .Range("C2:AK" & eRow).Value
    End With
    If q > 0 Then
      sRow = UBound(sArr)
      colArr = Array(, 9, 11, 21, 22, 23, 26, 31, 32, 33, 34, 35)
      ReDim Res(1 To q, 1 To UBound(colArr))
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          k = k + 1
          If k = 1 Then
            Range("B3") = sArr(i, 3)
            Range("C3") = sArr(i, 2)
          End If
          For j = 1 To UBound(colArr)
            Res(k, j) = sArr(i, colArr(j))
          Next j
        End If
      Next
     
      Range("A5").Resize(k).NumberFormat = "@"
      Range("A5:K5").Resize(k) = Res
      Range("A5:K5").Resize(k).Borders.LineStyle = 1
    End If
End Sub
khi em mở file lên nó báo lỗi dòng đó.
1.PNG
 
Upvote 0
Bạn thử đặt Code dưới vào đầu thủ tục xem sao, phương thức này giúp thêm thư viện cho dự án nếu dự án không có.
Hoặc tạo thủ tục riêng rồi thực thi.
Code dưới tôi hướng dẫn đơn giản, nếu thật đưa vào dự án, chỉnh sửa nghiêm chỉnh hơn, Và code phải đặt ở sự kiện mở workbook

JavaScript:
On Error Resume Next
ThisWorkbook.VBProject.References.AddFromFile "C:\Windows\system32\scrrun.dll"
ThisWorkbook.VBProject.References.AddFromFile "C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\mscorlib.tlb"
'ThisWorkbook.VBProject.References.AddFromguid "{BED7F4EA-1A96-11D2-8F08-00A0C9A6186D}", 2, 4
On Error Goto 0
 
Upvote 0
Bạn thử đặt Code dưới vào đầu thủ tục xem sao, phương thức này giúp thêm thư viện cho dự án nếu dự án không có.
Hoặc tạo thủ tục riêng rồi thực thi.
Code dưới tôi hướng dẫn đơn giản, nếu thật đưa vào dự án, chỉnh sửa nghiêm chỉnh hơn, Và code phải đặt ở sự kiện mở workbook

JavaScript:
On Error Resume Next
ThisWorkbook.VBProject.References.AddFromFile "C:\Windows\system32\scrrun.dll"
ThisWorkbook.VBProject.References.AddFromFile "C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\mscorlib.tlb"
'ThisWorkbook.VBProject.References.AddFromguid "{BED7F4EA-1A96-11D2-8F08-00A0C9A6186D}", 2, 4
On Error Goto 0
Sao em add vào mà không thấy tác dụng gì Anh.
PHP:
On Error Resume Next
ThisWorkbook.VBProject.References.AddFromFile "C:\Windows\system32\scrrun.dll"
ThisWorkbook.VBProject.References.AddFromFile "C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\mscorlib.tlb"
'ThisWorkbook.VBProject.References.AddFromguid "{BED7F4EA-1A96-11D2-8F08-00A0C9A6186D}", 2, 4
On Error GoTo 0
Dim eR As Long

Private Sub Worksheet_Change(ByVal Target As Range)
  Dim eRow As Long, Rng As Range
  If Target.Address(0, 0) = "A3" Then
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    
    eRow = Range("A" & Rows.Count).End(xlUp).Row
    If eRow > 4 Then Range("A5:K" & eRow).Clear

    Call Cot_A_K(Target.Value)
    Set Rng = Range("D3:K3")
    Rng.ClearContents
    Call Dong_3(Rng, Range("C3").Value)

    Application.ScreenUpdating = True
    Application.EnableEvents = True
  End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Target.Address(0, 0) = "A3" Then
    Application.EnableEvents = False
    Dim sArr(), Res(), oSList As Object
    Dim i As Long, eRow As Long, sRow As Long, k As Long
 
    With Sheets("CAR proposal")
      eRow = .Range("C" & Rows.Count).End(xlUp).Row
      If eRow <> eR Then
        eR = eRow
        sArr = .Range("C2:C" & eR).Value
        sRow = UBound(sArr)
        Set oSList = CreateObject("System.Collections.SortedList")
        For i = 1 To sRow
          If oSList.ContainsKey(sArr(i, 1)) = False Then oSList.Add sArr(i, 1), ""
        Next
        k = oSList.Count - 1
        ReDim Res(0 To k)
        For i = 0 To k
          Res(i) = oSList.GetKey(i)
        Next i
        Set oSList = Nothing
        Range("A3").Validation.Delete
        Range("A3").Validation.Add 3, , , Join(Res, ",")
      End If
    End With
    Application.EnableEvents = True
  End If
End Sub

Private Sub Dong_3(ByRef Rng, ByVal iKey As String)
  Dim wb As Workbook, sArr()
  Dim i As Long, eRow As Long, sRow As Long
  
  Set wb = Workbooks.Open(ThisWorkbook.Path & "\DU LIEU TIM KIEM1.xlsm")
  With wb.Sheets("MOQ")
    eRow = .Range("B" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("B2:H" & eRow).Value
      sRow = UBound(sArr)
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          If Len(sArr(i, 4)) > 0 Then
            Rng(1, 1) = sArr(i, 4)
          ElseIf Len(sArr(i, 7)) > 0 Then
            Rng(1, 1) = sArr(i, 7)
          End If
          If Len(sArr(i, 3)) > 0 Then
            Rng(1, 2) = sArr(i, 3)
          ElseIf Len(sArr(i, 5)) > 0 Then
            Rng(1, 2) = sArr(i, 5)
          End If
          Exit For
        End If
      Next i
    End If
  End With

  With wb.Sheets("LGH")
    eRow = .Range("B" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("B2:I" & eRow).Value
      sRow = UBound(sArr)
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          If Len(sArr(i, 8)) > 0 Then Rng(1, 3) = sArr(i, 8)
          If Len(sArr(i, 3)) > 0 Then Rng(1, 4) = sArr(i, 3)
          Exit For
        End If
      Next i
    End If
  End With
      
  With wb.Sheets("GIO COLLECT")
    eRow = .Range("A" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("A2:C" & eRow).Value
      sRow = UBound(sArr)
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          If Len(sArr(i, 3)) > 0 Then Rng(1, 5) = sArr(i, 3)
          Exit For
        End If
      Next i
    End If
  End With
 
  With wb.Sheets("LDH")
    eRow = .Range("B" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("B2:P" & eRow).Value
      sRow = UBound(sArr)
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          If Len(sArr(i, 15)) > 0 Then Rng(1, 6) = Replace(Application.Trim(Replace(sArr(i, 15), ",", " ")), " ", ",")
          If Len(sArr(i, 4)) > 0 Then Rng(1, 7) = sArr(i, 4)
          If Len(sArr(i, 5)) > 0 Then Rng(1, 8) = sArr(i, 5)
          Exit For
        End If
      Next i
    End If
  End With
  wb.Close False
End Sub

Private Sub Cot_A_K(ByVal iKey As String)
    Dim sArr(), Res(), colArr()
    Dim i As Long, eRow As Long, q As Long, sRow As Long, k As Long, j As Long
    
    With Sheets("CAR proposal")
      eRow = .Range("C" & Rows.Count).End(xlUp).Row
      q = Application.CountIf(.Range("C2:C" & eRow), iKey)
      If q > 0 Then sArr = .Range("C2:AK" & eRow).Value
    End With
    If q > 0 Then
      sRow = UBound(sArr)
      colArr = Array(, 9, 11, 21, 22, 23, 26, 31, 32, 33, 34, 35)
      ReDim Res(1 To q, 1 To UBound(colArr))
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          k = k + 1
          If k = 1 Then
            Range("B3") = sArr(i, 3)
            Range("C3") = sArr(i, 2)
          End If
          For j = 1 To UBound(colArr)
            Res(k, j) = sArr(i, colArr(j))
          Next j
        End If
      Next
      
      Range("A5").Resize(k).NumberFormat = "@"
      Range("A5:K5").Resize(k) = Res
      Range("A5:K5").Resize(k).Borders.LineStyle = 1
    End If
End Sub
 
Upvote 0
Sao em add vào mà không thấy tác dụng gì Anh.
PHP:
On Error Resume Next
ThisWorkbook.VBProject.References.AddFromFile "C:\Windows\system32\scrrun.dll"
ThisWorkbook.VBProject.References.AddFromFile "C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\mscorlib.tlb"
'ThisWorkbook.VBProject.References.AddFromguid "{BED7F4EA-1A96-11D2-8F08-00A0C9A6186D}", 2, 4
On Error GoTo 0
Dim eR As Long

Private Sub Worksheet_Change(ByVal Target As Range)
  Dim eRow As Long, Rng As Range
  If Target.Address(0, 0) = "A3" Then
    Application.EnableEvents = False
    Application.ScreenUpdating = False

    eRow = Range("A" & Rows.Count).End(xlUp).Row
    If eRow > 4 Then Range("A5:K" & eRow).Clear

    Call Cot_A_K(Target.Value)
    Set Rng = Range("D3:K3")
    Rng.ClearContents
    Call Dong_3(Rng, Range("C3").Value)

    Application.ScreenUpdating = True
    Application.EnableEvents = True
  End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Target.Address(0, 0) = "A3" Then
    Application.EnableEvents = False
    Dim sArr(), Res(), oSList As Object
    Dim i As Long, eRow As Long, sRow As Long, k As Long

    With Sheets("CAR proposal")
      eRow = .Range("C" & Rows.Count).End(xlUp).Row
      If eRow <> eR Then
        eR = eRow
        sArr = .Range("C2:C" & eR).Value
        sRow = UBound(sArr)
        Set oSList = CreateObject("System.Collections.SortedList")
        For i = 1 To sRow
          If oSList.ContainsKey(sArr(i, 1)) = False Then oSList.Add sArr(i, 1), ""
        Next
        k = oSList.Count - 1
        ReDim Res(0 To k)
        For i = 0 To k
          Res(i) = oSList.GetKey(i)
        Next i
        Set oSList = Nothing
        Range("A3").Validation.Delete
        Range("A3").Validation.Add 3, , , Join(Res, ",")
      End If
    End With
    Application.EnableEvents = True
  End If
End Sub

Private Sub Dong_3(ByRef Rng, ByVal iKey As String)
  Dim wb As Workbook, sArr()
  Dim i As Long, eRow As Long, sRow As Long

  Set wb = Workbooks.Open(ThisWorkbook.Path & "\DU LIEU TIM KIEM1.xlsm")
  With wb.Sheets("MOQ")
    eRow = .Range("B" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("B2:H" & eRow).Value
      sRow = UBound(sArr)
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          If Len(sArr(i, 4)) > 0 Then
            Rng(1, 1) = sArr(i, 4)
          ElseIf Len(sArr(i, 7)) > 0 Then
            Rng(1, 1) = sArr(i, 7)
          End If
          If Len(sArr(i, 3)) > 0 Then
            Rng(1, 2) = sArr(i, 3)
          ElseIf Len(sArr(i, 5)) > 0 Then
            Rng(1, 2) = sArr(i, 5)
          End If
          Exit For
        End If
      Next i
    End If
  End With

  With wb.Sheets("LGH")
    eRow = .Range("B" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("B2:I" & eRow).Value
      sRow = UBound(sArr)
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          If Len(sArr(i, 8)) > 0 Then Rng(1, 3) = sArr(i, 8)
          If Len(sArr(i, 3)) > 0 Then Rng(1, 4) = sArr(i, 3)
          Exit For
        End If
      Next i
    End If
  End With
  
  With wb.Sheets("GIO COLLECT")
    eRow = .Range("A" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("A2:C" & eRow).Value
      sRow = UBound(sArr)
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          If Len(sArr(i, 3)) > 0 Then Rng(1, 5) = sArr(i, 3)
          Exit For
        End If
      Next i
    End If
  End With

  With wb.Sheets("LDH")
    eRow = .Range("B" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("B2:P" & eRow).Value
      sRow = UBound(sArr)
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          If Len(sArr(i, 15)) > 0 Then Rng(1, 6) = Replace(Application.Trim(Replace(sArr(i, 15), ",", " ")), " ", ",")
          If Len(sArr(i, 4)) > 0 Then Rng(1, 7) = sArr(i, 4)
          If Len(sArr(i, 5)) > 0 Then Rng(1, 8) = sArr(i, 5)
          Exit For
        End If
      Next i
    End If
  End With
  wb.Close False
End Sub

Private Sub Cot_A_K(ByVal iKey As String)
    Dim sArr(), Res(), colArr()
    Dim i As Long, eRow As Long, q As Long, sRow As Long, k As Long, j As Long

    With Sheets("CAR proposal")
      eRow = .Range("C" & Rows.Count).End(xlUp).Row
      q = Application.CountIf(.Range("C2:C" & eRow), iKey)
      If q > 0 Then sArr = .Range("C2:AK" & eRow).Value
    End With
    If q > 0 Then
      sRow = UBound(sArr)
      colArr = Array(, 9, 11, 21, 22, 23, 26, 31, 32, 33, 34, 35)
      ReDim Res(1 To q, 1 To UBound(colArr))
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          k = k + 1
          If k = 1 Then
            Range("B3") = sArr(i, 3)
            Range("C3") = sArr(i, 2)
          End If
          For j = 1 To UBound(colArr)
            Res(k, j) = sArr(i, colArr(j))
          Next j
        End If
      Next
  
      Range("A5").Resize(k).NumberFormat = "@"
      Range("A5:K5").Resize(k) = Res
      Range("A5:K5").Resize(k).Borders.LineStyle = 1
    End If
End Sub
Bạn vui tính đấy, đầu thủ tục chứ có phải đầu Module / Document hay Class đâu
Chắc do bạn không hiểu "thủ tục" -> Sub và Function hay các Prototype nó gọi chung là thủ tục
 
Upvote 0
Chép code vào sheet loc, lưu file theo đuôi: .xlsm hay .xlsb
2 file phải cùng thư mục
Mã:
Dim eR As Long

Private Sub Worksheet_Change(ByVal Target As Range)
  Dim eRow As Long, Rng As Range
  If Target.Address(0, 0) = "A3" Then
    Application.EnableEvents = False
    Application.ScreenUpdating = False
   
    eRow = Range("A" & Rows.Count).End(xlUp).Row
    If eRow > 4 Then Range("A5:K" & eRow).Clear

    Call Cot_A_K(Target.Value)
    Set Rng = Range("D3:K3")
    Rng.ClearContents
    Call Dong_3(Rng, Range("C3").Value)

    Application.ScreenUpdating = True
    Application.EnableEvents = True
  End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Target.Address(0, 0) = "A3" Then
    Application.EnableEvents = False
    Dim sArr(), Res(), oSList As Object
    Dim i As Long, eRow As Long, sRow As Long, k As Long

    With Sheets("CAR proposal")
      eRow = .Range("C" & Rows.Count).End(xlUp).Row
      If eRow <> eR Then
        eR = eRow
        sArr = .Range("C2:C" & eR).Value
        sRow = UBound(sArr)
        Set oSList = CreateObject("System.Collections.SortedList")
        For i = 1 To sRow
          If oSList.ContainsKey(sArr(i, 1)) = False Then oSList.Add sArr(i, 1), ""
        Next
        k = oSList.Count - 1
        ReDim Res(0 To k)
        For i = 0 To k
          Res(i) = oSList.GetKey(i)
        Next i
        Set oSList = Nothing
        Range("A3").Validation.Delete
        Range("A3").Validation.Add 3, , , Join(Res, ",")
      End If
    End With
    Application.EnableEvents = True
  End If
End Sub

Private Sub Dong_3(ByRef Rng, ByVal iKey As String)
  Dim wb As Workbook, sArr()
  Dim i As Long, eRow As Long, sRow As Long
 
  Set wb = Workbooks.Open(ThisWorkbook.Path & "\DU LIEU TIM KIEM1.xlsm")
  With wb.Sheets("MOQ")
    eRow = .Range("B" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("B2:H" & eRow).Value
      sRow = UBound(sArr)
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          If Len(sArr(i, 4)) > 0 Then
            Rng(1, 1) = sArr(i, 4)
          ElseIf Len(sArr(i, 7)) > 0 Then
            Rng(1, 1) = sArr(i, 7)
          End If
          If Len(sArr(i, 3)) > 0 Then
            Rng(1, 2) = sArr(i, 3)
          ElseIf Len(sArr(i, 5)) > 0 Then
            Rng(1, 2) = sArr(i, 5)
          End If
          Exit For
        End If
      Next i
    End If
  End With

  With wb.Sheets("LGH")
    eRow = .Range("B" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("B2:I" & eRow).Value
      sRow = UBound(sArr)
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          If Len(sArr(i, 8)) > 0 Then Rng(1, 3) = sArr(i, 8)
          If Len(sArr(i, 3)) > 0 Then Rng(1, 4) = sArr(i, 3)
          Exit For
        End If
      Next i
    End If
  End With
     
  With wb.Sheets("GIO COLLECT")
    eRow = .Range("A" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("A2:C" & eRow).Value
      sRow = UBound(sArr)
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          If Len(sArr(i, 3)) > 0 Then Rng(1, 5) = sArr(i, 3)
          Exit For
        End If
      Next i
    End If
  End With

  With wb.Sheets("LDH")
    eRow = .Range("B" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("B2:P" & eRow).Value
      sRow = UBound(sArr)
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          If Len(sArr(i, 15)) > 0 Then Rng(1, 6) = Replace(Application.Trim(Replace(sArr(i, 15), ",", " ")), " ", ",")
          If Len(sArr(i, 4)) > 0 Then Rng(1, 7) = sArr(i, 4)
          If Len(sArr(i, 5)) > 0 Then Rng(1, 8) = sArr(i, 5)
          Exit For
        End If
      Next i
    End If
  End With
  wb.Close False
End Sub

Private Sub Cot_A_K(ByVal iKey As String)
    Dim sArr(), Res(), colArr()
    Dim i As Long, eRow As Long, q As Long, sRow As Long, k As Long, j As Long
   
    With Sheets("CAR proposal")
      eRow = .Range("C" & Rows.Count).End(xlUp).Row
      q = Application.CountIf(.Range("C2:C" & eRow), iKey)
      If q > 0 Then sArr = .Range("C2:AK" & eRow).Value
    End With
    If q > 0 Then
      sRow = UBound(sArr)
      colArr = Array(, 9, 11, 21, 22, 23, 26, 31, 32, 33, 34, 35)
      ReDim Res(1 To q, 1 To UBound(colArr))
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          k = k + 1
          If k = 1 Then
            Range("B3") = sArr(i, 3)
            Range("C3") = sArr(i, 2)
          End If
          For j = 1 To UBound(colArr)
            Res(k, j) = sArr(i, colArr(j))
          Next j
        End If
      Next
     
      Range("A5").Resize(k).NumberFormat = "@"
      Range("A5:K5").Resize(k) = Res
      Range("A5:K5").Resize(k).Borders.LineStyle = 1
    End If
End Sub
Sao em add vào code không chạy đó Anh, không biết em có làm sai gì không nữa.
 

File đính kèm

Upvote 0
Lỡ đăng bài rồi, thôi thì tôi giúp bạn cho trót:
Hai dòng code tôi đăng ở trên là thêm hai thư viện là:

1. Microsoft Scripting Runtime: ví dụ: CreateObject / GetObject / ... và để khởi tạo biến ở dạng Early Binding là khai báo sớm.
2. Microsoft Collection Library: Nó hỗ trợ ArrayList , Sorted List

Khi tạo một dự án, ta vào Tools - References để thêm thư viện, tuy nhiên có một có thư viện chưa được Import. Ta phải tự tay Import vào.
Và thay vì dùng tay, ta dùng code. Một dự án phải dùng code, thay vì dùng tay, để khi người khác copy lại sẽ không gặp lỗi.
 
Upvote 0
Lỡ đăng bài rồi, thôi thì tôi giúp bạn cho trót:
Hai dòng code tôi đăng ở trên là thêm hai thư viện là:

1. Microsoft Scripting Runtime: ví dụ: CreateObject / GetObject / ... và để khởi tạo biến ở dạng Early Binding là khai báo sớm.
2. Microsoft Collection Library: Nó hỗ trợ ArrayList , Sorted List

Khi tạo một dự án, ta vào Tools - References để thêm thư viện, tuy nhiên có một có thư viện chưa được Import. Ta phải tự tay Import vào.
Và thay vì dùng tay, ta dùng code. Một dự án phải dùng code, thay vì dùng tay, để khi người khác copy lại sẽ không gặp lỗi.
Nói thật code em tìm lấy trên diễn đàn về chế lại phục vụ công việc của mình, em không được học bài bản về vấn đề VBA này. Chỉ là lượm mót rồi chế biến lại thôi.
Nếu vậy nhờ Anh kiểm tra code ở bài trên sao không chạy được à.
Em cảm ơn Anh rất nhiều!
 
Upvote 0
Bạn Click vào code của ThisWorkbook, rồi copy vào, và Save là được rồi
JavaScript:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
  AddRef
End Sub

Private Sub Workbook_Open()
  AddRef
End Sub

Private Sub AddRef()
  On Error Resume Next
  ThisWorkbook.VBProject.References.AddFromFile "C:\Windows\system32\scrrun.dll"
  ThisWorkbook.VBProject.References.AddFromguid "{BED7F4EA-1A96-11D2-8F08-00A0C9A6186D}", 2, 4
  On Error GoTo 0
End Sub
 
Upvote 0
Yêu cầu của em vấn đề này là:
- khi em copy dữ liệu vào ô C3 ở sheet LOC thì sẽ trích lọc dữ liệu ra.
- khi copy vào(nếu NCC có 2 số PO thì sẽ tạo nút giống như kiểu VALIDATION.(nằm ở ô A3)
-khi em chọn 01 số PO thì dữ liệu sẽ thay đổi theo-
-các dữ liệu khi trích lọc ra tự động căn chỉnh dòng và cột.
Bài đã được tự động gộp:

Chép code vào sheet loc, lưu file theo đuôi: .xlsm hay .xlsb
2 file phải cùng thư mục
Mã:
Dim eR As Long

Private Sub Worksheet_Change(ByVal Target As Range)
  Dim eRow As Long, Rng As Range
  If Target.Address(0, 0) = "A3" Then
    Application.EnableEvents = False
    Application.ScreenUpdating = False
   
    eRow = Range("A" & Rows.Count).End(xlUp).Row
    If eRow > 4 Then Range("A5:K" & eRow).Clear

    Call Cot_A_K(Target.Value)
    Set Rng = Range("D3:K3")
    Rng.ClearContents
    Call Dong_3(Rng, Range("C3").Value)

    Application.ScreenUpdating = True
    Application.EnableEvents = True
  End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Target.Address(0, 0) = "A3" Then
    Application.EnableEvents = False
    Dim sArr(), Res(), oSList As Object
    Dim i As Long, eRow As Long, sRow As Long, k As Long

    With Sheets("CAR proposal")
      eRow = .Range("C" & Rows.Count).End(xlUp).Row
      If eRow <> eR Then
        eR = eRow
        sArr = .Range("C2:C" & eR).Value
        sRow = UBound(sArr)
        Set oSList = CreateObject("System.Collections.SortedList")
        For i = 1 To sRow
          If oSList.ContainsKey(sArr(i, 1)) = False Then oSList.Add sArr(i, 1), ""
        Next
        k = oSList.Count - 1
        ReDim Res(0 To k)
        For i = 0 To k
          Res(i) = oSList.GetKey(i)
        Next i
        Set oSList = Nothing
        Range("A3").Validation.Delete
        Range("A3").Validation.Add 3, , , Join(Res, ",")
      End If
    End With
    Application.EnableEvents = True
  End If
End Sub

Private Sub Dong_3(ByRef Rng, ByVal iKey As String)
  Dim wb As Workbook, sArr()
  Dim i As Long, eRow As Long, sRow As Long
 
  Set wb = Workbooks.Open(ThisWorkbook.Path & "\DU LIEU TIM KIEM1.xlsm")
  With wb.Sheets("MOQ")
    eRow = .Range("B" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("B2:H" & eRow).Value
      sRow = UBound(sArr)
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          If Len(sArr(i, 4)) > 0 Then
            Rng(1, 1) = sArr(i, 4)
          ElseIf Len(sArr(i, 7)) > 0 Then
            Rng(1, 1) = sArr(i, 7)
          End If
          If Len(sArr(i, 3)) > 0 Then
            Rng(1, 2) = sArr(i, 3)
          ElseIf Len(sArr(i, 5)) > 0 Then
            Rng(1, 2) = sArr(i, 5)
          End If
          Exit For
        End If
      Next i
    End If
  End With

  With wb.Sheets("LGH")
    eRow = .Range("B" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("B2:I" & eRow).Value
      sRow = UBound(sArr)
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          If Len(sArr(i, 8)) > 0 Then Rng(1, 3) = sArr(i, 8)
          If Len(sArr(i, 3)) > 0 Then Rng(1, 4) = sArr(i, 3)
          Exit For
        End If
      Next i
    End If
  End With
     
  With wb.Sheets("GIO COLLECT")
    eRow = .Range("A" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("A2:C" & eRow).Value
      sRow = UBound(sArr)
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          If Len(sArr(i, 3)) > 0 Then Rng(1, 5) = sArr(i, 3)
          Exit For
        End If
      Next i
    End If
  End With

  With wb.Sheets("LDH")
    eRow = .Range("B" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("B2:P" & eRow).Value
      sRow = UBound(sArr)
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          If Len(sArr(i, 15)) > 0 Then Rng(1, 6) = Replace(Application.Trim(Replace(sArr(i, 15), ",", " ")), " ", ",")
          If Len(sArr(i, 4)) > 0 Then Rng(1, 7) = sArr(i, 4)
          If Len(sArr(i, 5)) > 0 Then Rng(1, 8) = sArr(i, 5)
          Exit For
        End If
      Next i
    End If
  End With
  wb.Close False
End Sub

Private Sub Cot_A_K(ByVal iKey As String)
    Dim sArr(), Res(), colArr()
    Dim i As Long, eRow As Long, q As Long, sRow As Long, k As Long, j As Long
   
    With Sheets("CAR proposal")
      eRow = .Range("C" & Rows.Count).End(xlUp).Row
      q = Application.CountIf(.Range("C2:C" & eRow), iKey)
      If q > 0 Then sArr = .Range("C2:AK" & eRow).Value
    End With
    If q > 0 Then
      sRow = UBound(sArr)
      colArr = Array(, 9, 11, 21, 22, 23, 26, 31, 32, 33, 34, 35)
      ReDim Res(1 To q, 1 To UBound(colArr))
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          k = k + 1
          If k = 1 Then
            Range("B3") = sArr(i, 3)
            Range("C3") = sArr(i, 2)
          End If
          For j = 1 To UBound(colArr)
            Res(k, j) = sArr(i, colArr(j))
          Next j
        End If
      Next
     
      Range("A5").Resize(k).NumberFormat = "@"
      Range("A5:K5").Resize(k) = Res
      Range("A5:K5").Resize(k).Borders.LineStyle = 1
    End If
End Sub
Anh xem giúp em dữ liệu từ dòng D3:k3 không chạy code Anh ơi.

Nhờ Anh kiểm tra giúp em.
Bài đã được tự động gộp:

Bạn Click vào code của ThisWorkbook, rồi copy vào, và Save là được rồi
JavaScript:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
  AddRef
End Sub

Private Sub Workbook_Open()
  AddRef
End Sub

Private Sub AddRef()
  On Error Resume Next
  ThisWorkbook.VBProject.References.AddFromFile "C:\Windows\system32\scrrun.dll"
  ThisWorkbook.VBProject.References.AddFromguid "{BED7F4EA-1A96-11D2-8F08-00A0C9A6186D}", 2, 4
  On Error GoTo 0
End Sub
Code chạy được rồi Anh, nhưng tại sao dòng D3:K3 lại không chạy,
Anh có thể kiểm tra giúp em được không Anh?
Em cảm ơn Anh nhiều.
 
Lần chỉnh sửa cuối:
Upvote 0
Để Bác Hiếu sẽ giúp bạn, khi viết code giúp bạn xong không biết bác ấy có giúp bạn kiểm soát code không, ví dụ dự án phình to, thêm cột, thêm điều kiện ... Tôi nghĩ là giúp xong còn lại công việc là của bạn đấy.

Bác ấy đã bỏ công viết code giúp bạn, tuy nhiên cách viết code của bác ấy theo xu hướng trợ giúp chứ không phải viết cho một dự án thực sự, bạn đừng buồn khi tôi nói vậy nhé.

Dự án của bạn, vào trong Option tắt chế độ soát lỗi là số đi, không thì mỗi lần dự án được mở lên là luôn luôn soát lỗi.
 
Upvote 0
Chép code vào sheet loc, lưu file theo đuôi: .xlsm hay .xlsb
2 file phải cùng thư mục
Mã:
Dim eR As Long

Private Sub Worksheet_Change(ByVal Target As Range)
  Dim eRow As Long, Rng As Range
  If Target.Address(0, 0) = "A3" Then
    Application.EnableEvents = False
    Application.ScreenUpdating = False
   
    eRow = Range("A" & Rows.Count).End(xlUp).Row
    If eRow > 4 Then Range("A5:K" & eRow).Clear

    Call Cot_A_K(Target.Value)
    Set Rng = Range("D3:K3")
    Rng.ClearContents
    Call Dong_3(Rng, Range("C3").Value)

    Application.ScreenUpdating = True
    Application.EnableEvents = True
  End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Target.Address(0, 0) = "A3" Then
    Application.EnableEvents = False
    Dim sArr(), Res(), oSList As Object
    Dim i As Long, eRow As Long, sRow As Long, k As Long

    With Sheets("CAR proposal")
      eRow = .Range("C" & Rows.Count).End(xlUp).Row
      If eRow <> eR Then
        eR = eRow
        sArr = .Range("C2:C" & eR).Value
        sRow = UBound(sArr)
        Set oSList = CreateObject("System.Collections.SortedList")
        For i = 1 To sRow
          If oSList.ContainsKey(sArr(i, 1)) = False Then oSList.Add sArr(i, 1), ""
        Next
        k = oSList.Count - 1
        ReDim Res(0 To k)
        For i = 0 To k
          Res(i) = oSList.GetKey(i)
        Next i
        Set oSList = Nothing
        Range("A3").Validation.Delete
        Range("A3").Validation.Add 3, , , Join(Res, ",")
      End If
    End With
    Application.EnableEvents = True
  End If
End Sub

Private Sub Dong_3(ByRef Rng, ByVal iKey As String)
  Dim wb As Workbook, sArr()
  Dim i As Long, eRow As Long, sRow As Long
 
  Set wb = Workbooks.Open(ThisWorkbook.Path & "\DU LIEU TIM KIEM1.xlsm")
  With wb.Sheets("MOQ")
    eRow = .Range("B" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("B2:H" & eRow).Value
      sRow = UBound(sArr)
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          If Len(sArr(i, 4)) > 0 Then
            Rng(1, 1) = sArr(i, 4)
          ElseIf Len(sArr(i, 7)) > 0 Then
            Rng(1, 1) = sArr(i, 7)
          End If
          If Len(sArr(i, 3)) > 0 Then
            Rng(1, 2) = sArr(i, 3)
          ElseIf Len(sArr(i, 5)) > 0 Then
            Rng(1, 2) = sArr(i, 5)
          End If
          Exit For
        End If
      Next i
    End If
  End With

  With wb.Sheets("LGH")
    eRow = .Range("B" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("B2:I" & eRow).Value
      sRow = UBound(sArr)
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          If Len(sArr(i, 8)) > 0 Then Rng(1, 3) = sArr(i, 8)
          If Len(sArr(i, 3)) > 0 Then Rng(1, 4) = sArr(i, 3)
          Exit For
        End If
      Next i
    End If
  End With
     
  With wb.Sheets("GIO COLLECT")
    eRow = .Range("A" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("A2:C" & eRow).Value
      sRow = UBound(sArr)
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          If Len(sArr(i, 3)) > 0 Then Rng(1, 5) = sArr(i, 3)
          Exit For
        End If
      Next i
    End If
  End With

  With wb.Sheets("LDH")
    eRow = .Range("B" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      sArr = .Range("B2:P" & eRow).Value
      sRow = UBound(sArr)
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          If Len(sArr(i, 15)) > 0 Then Rng(1, 6) = Replace(Application.Trim(Replace(sArr(i, 15), ",", " ")), " ", ",")
          If Len(sArr(i, 4)) > 0 Then Rng(1, 7) = sArr(i, 4)
          If Len(sArr(i, 5)) > 0 Then Rng(1, 8) = sArr(i, 5)
          Exit For
        End If
      Next i
    End If
  End With
  wb.Close False
End Sub

Private Sub Cot_A_K(ByVal iKey As String)
    Dim sArr(), Res(), colArr()
    Dim i As Long, eRow As Long, q As Long, sRow As Long, k As Long, j As Long
   
    With Sheets("CAR proposal")
      eRow = .Range("C" & Rows.Count).End(xlUp).Row
      q = Application.CountIf(.Range("C2:C" & eRow), iKey)
      If q > 0 Then sArr = .Range("C2:AK" & eRow).Value
    End With
    If q > 0 Then
      sRow = UBound(sArr)
      colArr = Array(, 9, 11, 21, 22, 23, 26, 31, 32, 33, 34, 35)
      ReDim Res(1 To q, 1 To UBound(colArr))
      For i = 1 To sRow
        If sArr(i, 1) = iKey Then
          k = k + 1
          If k = 1 Then
            Range("B3") = sArr(i, 3)
            Range("C3") = sArr(i, 2)
          End If
          For j = 1 To UBound(colArr)
            Res(k, j) = sArr(i, colArr(j))
          Next j
        End If
      Next
     
      Range("A5").Resize(k).NumberFormat = "@"
      Range("A5:K5").Resize(k) = Res
      Range("A5:K5").Resize(k).Borders.LineStyle = 1
    End If
End Sub
Em còn vấn đề này nhờ Anh hỗ trợ , dữ liệu ở ô C3 là dạng số không phải dạng chuỗi đó Anh,
em nhờ Anh khi copy vào dữ liệu chuyển thành Text. giống như cột D ở sheet Car proposal đó Anh.

Em cảm ơn Anh rất nhiều!
Bài đã được tự động gộp:

Để Bác Hiếu sẽ giúp bạn, khi viết code giúp bạn xong không biết bác ấy có giúp bạn kiểm soát code không, ví dụ dự án phình to, thêm cột, thêm điều kiện ... Tôi nghĩ là giúp xong còn lại công việc là của bạn đấy.

Bác ấy đã bỏ công viết code giúp bạn, tuy nhiên cách viết code của bác ấy theo xu hướng trợ giúp chứ không phải viết cho một dự án thực sự, bạn đừng buồn khi tôi nói vậy nhé.

Dự án của bạn, vào trong Option tắt chế độ soát lỗi là số đi, không thì mỗi lần dự án được mở lên là luôn luôn soát lỗi.
Em nhờ Anh một việc được không?
Ý em là ô C3 ở sheet LOC sẽ giống như cột D(supplier code) đó Anh.

Em cảm ơn Anh rất nhiều!
 
Upvote 0
Upvote 0
Web KT

Bài viết mới nhất

Back
Top Bottom