... Bài #7 ạChào cả nhà.
Mình có file trong đó có 3 cột dữ liệu (có công thức ) cần 3 cột công thức đó sang macro để chạy cho nhanh.
Mình đính kèm file cả nhà xem giúp mình nhé.
Cảm ơn
Thanks bạn để mình thưtHíc, thấy code dài dài mà hoang mang quá.
Bạn thử xem nhé, OT chạy thử không có lỗi gì & cũng ra kết quả.
Mã:Option Explicit Sub Tap_Code() Dim BangDo(), dlVao(), dlRa_DE(), dlRa_N(), dict As Object, key As Variant Dim lR As Long, i As Long, r As Long, k As Long, shDATA As Worksheet, shtType As Worksheet Const KoTimThay As String = "#NA" Set shDATA = ThisWorkbook.Worksheets("DATA") Set shtType = ThisWorkbook.Worksheets("TYPE") Set dict = CreateObject("Scripting.Dictionary") With shtType lR = .Range("B" & Rows.Count).End(xlUp).Row If lR < 2 Then GoTo Tieptuc BangDo = .Range("B2:D" & lR).Value For i = 1 To UBound(BangDo, 1) k = k + 1 dict.Item(BangDo(i, 1)) = k Next i End With Tieptuc: With shDATA lR = .Range("C" & Rows.Count).End(xlUp).Row If lR < 2 Then Exit Sub dlVao = .Range("A2:A" & lR).Resize(, 29).Value ReDim dlRa_DE(1 To UBound(dlVao, 1), 1 To 2) ReDim dlRa_N(1 To UBound(dlVao, 1), 1 To 1) For i = 1 To UBound(dlVao, 1) key = dlVao(i, 3) If dict.exists(key) Then r = dict.Item(key) dlRa_DE(i, 1) = BangDo(r, 2) dlRa_DE(i, 2) = BangDo(r, 3) Else dlRa_DE(i, 1) = KoTimThay dlRa_DE(i, 2) = KoTimThay End If If (dlVao(i, 5) = 1 And dlVao(i, 10) <= 1) Or _ (dlVao(i, 5) = 3 And dlVao(i, 10) <= 1) Or _ (dlVao(i, 5) = 5 And dlVao(i, 10) <= 3) Then _ dlRa_N(i, 1) = "OK" Else dlRa_N(i, 1) = "NG" Next i .Range("D2").Resize(UBound(dlRa_DE, 1), UBound(dlRa_DE, 2)).Value = dlRa_DE .Range("N2").Resize(UBound(dlRa_DE)).Value = dlRa_N End With End Sub
Cảm ơn bạn để mình thưt
Thanks bạn nhiều nhéBạn sửa lại giúp mình nhé:
k = k + 1
dict.Item(BangDo(i, 1)) = k
Thành:
dict.Item(BangDo(i, 1)) = i
OT mới bổ sung thêm cột J , bạn kiểm tra nhé:Cảm ơn bạn nhiều nhé
Option Explicit
Sub TapCode_DEJN()
Dim BangDo(), dlVao(), dlRa_DE(), dlRa_N(), dlRa_J(), dict As Object, key As Variant
Dim lR As Long, i As Long, r As Long, rOFF As Range, cE As Variant, cJ As Variant
Dim shDATA As Worksheet, shtType As Worksheet
Const KoTimThay As String = "#NA"
Set shDATA = ThisWorkbook.Worksheets("DATA")
Set shtType = ThisWorkbook.Worksheets("TYPE")
Set dict = CreateObject("Scripting.Dictionary")
With shtType
lR = .Range("B" & Rows.Count).End(xlUp).Row
If lR < 2 Then GoTo Tieptuc
BangDo = .Range("B2:D" & lR).Value
For i = 1 To UBound(BangDo, 1)
dict.Item(BangDo(i, 1)) = i
Next i
End With
Tieptuc:
With shDATA
lR = .Range("C" & Rows.Count).End(xlUp).Row
If lR < 2 Then Exit Sub
dlVao = .Range("A2:A" & lR).Resize(, 29).Value
Set rOFF = .Range("I2:I" & lR)
ReDim dlRa_DE(1 To UBound(dlVao, 1), 1 To 2)
ReDim dlRa_N(1 To UBound(dlVao, 1), 1 To 1)
ReDim dlRa_J(1 To UBound(dlVao, 1), 1 To 1)
For i = 1 To UBound(dlVao, 1)
key = dlVao(i, 3)
If dict.exists(key) Then
r = dict.Item(key)
dlRa_DE(i, 1) = BangDo(r, 2)
dlRa_DE(i, 2) = BangDo(r, 3)
Else
dlRa_DE(i, 1) = KoTimThay
dlRa_DE(i, 2) = KoTimThay
End If
cE = dlVao(i, 5): cJ = dlVao(i, 10)
If (cE = 1 And cJ <= 1) Or (cE = 3 And cJ <= 1) Or (cE = 5 And cJ <= 3) Then _
dlRa_N(i, 1) = "OK" Else dlRa_N(i, 1) = "NG"
dlRa_J(i, 1) = WorksheetFunction.NetworkDays_Intl(dlVao(i, 1), dlVao(i, 8), "0000000", rOFF) + 1
Next i
.Range("D2").Resize(UBound(dlRa_DE, 1), UBound(dlRa_DE, 2)).Value = dlRa_DE
.Range("J2").Resize(UBound(dlRa_J)).Value = dlRa_J
.Range("N2").Resize(UBound(dlRa_N)).Value = dlRa_N
End With
End Sub
Thanks bạn nhiều nhéOT mới bổ sung thêm cột J , bạn kiểm tra nhé:
Mã:Option Explicit Sub TapCode_DEJN() Dim BangDo(), dlVao(), dlRa_DE(), dlRa_N(), dlRa_J(), dict As Object, key As Variant Dim lR As Long, i As Long, r As Long, rOFF As Range, cE As Variant, cJ As Variant Dim shDATA As Worksheet, shtType As Worksheet Const KoTimThay As String = "#NA" Set shDATA = ThisWorkbook.Worksheets("DATA") Set shtType = ThisWorkbook.Worksheets("TYPE") Set dict = CreateObject("Scripting.Dictionary") With shtType lR = .Range("B" & Rows.Count).End(xlUp).Row If lR < 2 Then GoTo Tieptuc BangDo = .Range("B2:D" & lR).Value For i = 1 To UBound(BangDo, 1) dict.Item(BangDo(i, 1)) = i Next i End With Tieptuc: With shDATA lR = .Range("C" & Rows.Count).End(xlUp).Row If lR < 2 Then Exit Sub dlVao = .Range("A2:A" & lR).Resize(, 29).Value Set rOFF = .Range("I2:I" & lR) ReDim dlRa_DE(1 To UBound(dlVao, 1), 1 To 2) ReDim dlRa_N(1 To UBound(dlVao, 1), 1 To 1) ReDim dlRa_J(1 To UBound(dlVao, 1), 1 To 1) For i = 1 To UBound(dlVao, 1) key = dlVao(i, 3) If dict.exists(key) Then r = dict.Item(key) dlRa_DE(i, 1) = BangDo(r, 2) dlRa_DE(i, 2) = BangDo(r, 3) Else dlRa_DE(i, 1) = KoTimThay dlRa_DE(i, 2) = KoTimThay End If cE = dlVao(i, 5): cJ = dlVao(i, 10) If (cE = 1 And cJ <= 1) Or (cE = 3 And cJ <= 1) Or (cE = 5 And cJ <= 3) Then _ dlRa_N(i, 1) = "OK" Else dlRa_N(i, 1) = "NG" dlRa_J(i, 1) = WorksheetFunction.NetworkDays_Intl(dlVao(i, 1), dlVao(i, 8), "0000000", rOFF) + 1 Next i .Range("D2").Resize(UBound(dlRa_DE, 1), UBound(dlRa_DE, 2)).Value = dlRa_DE .Range("J2").Resize(UBound(dlRa_J)).Value = dlRa_J .Range("N2").Resize(UBound(dlRa_N)).Value = dlRa_N End With End Sub
Bạn ơi xem lại mình giúp chỗ đánh giá NG và Ok nó chạy không đúng ra toàn NGOT mới bổ sung thêm cột J , bạn kiểm tra nhé:
Mã:Option Explicit Sub TapCode_DEJN() Dim BangDo(), dlVao(), dlRa_DE(), dlRa_N(), dlRa_J(), dict As Object, key As Variant Dim lR As Long, i As Long, r As Long, rOFF As Range, cE As Variant, cJ As Variant Dim shDATA As Worksheet, shtType As Worksheet Const KoTimThay As String = "#NA" Set shDATA = ThisWorkbook.Worksheets("DATA") Set shtType = ThisWorkbook.Worksheets("TYPE") Set dict = CreateObject("Scripting.Dictionary") With shtType lR = .Range("B" & Rows.Count).End(xlUp).Row If lR < 2 Then GoTo Tieptuc BangDo = .Range("B2:D" & lR).Value For i = 1 To UBound(BangDo, 1) dict.Item(BangDo(i, 1)) = i Next i End With Tieptuc: With shDATA lR = .Range("C" & Rows.Count).End(xlUp).Row If lR < 2 Then Exit Sub dlVao = .Range("A2:A" & lR).Resize(, 29).Value Set rOFF = .Range("I2:I" & lR) ReDim dlRa_DE(1 To UBound(dlVao, 1), 1 To 2) ReDim dlRa_N(1 To UBound(dlVao, 1), 1 To 1) ReDim dlRa_J(1 To UBound(dlVao, 1), 1 To 1) For i = 1 To UBound(dlVao, 1) key = dlVao(i, 3) If dict.exists(key) Then r = dict.Item(key) dlRa_DE(i, 1) = BangDo(r, 2) dlRa_DE(i, 2) = BangDo(r, 3) Else dlRa_DE(i, 1) = KoTimThay dlRa_DE(i, 2) = KoTimThay End If cE = dlVao(i, 5): cJ = dlVao(i, 10) If (cE = 1 And cJ <= 1) Or (cE = 3 And cJ <= 1) Or (cE = 5 And cJ <= 3) Then _ dlRa_N(i, 1) = "OK" Else dlRa_N(i, 1) = "NG" dlRa_J(i, 1) = WorksheetFunction.NetworkDays_Intl(dlVao(i, 1), dlVao(i, 8), "0000000", rOFF) + 1 Next i .Range("D2").Resize(UBound(dlRa_DE, 1), UBound(dlRa_DE, 2)).Value = dlRa_DE .Range("J2").Resize(UBound(dlRa_J)).Value = dlRa_J .Range("N2").Resize(UBound(dlRa_N)).Value = dlRa_N End With End Sub
Trời ơi, là sao vậy ta. Tập tin bài 1 Bạn gửi lên cũng toàn "NG" mà.Bạn ơi xem lại mình giúp chỗ đánh giá NG và Ok nó chạy không đúng ra toàn NG
Cái này phải chạy sau hàm networkday chứ nhỉ
Hàm networkday hình như là phải -1(chứ không phải +1)
Khi lưu file nó báo lỗi gì ý
Bạn xem lại giúp mình nhé
Cảm ơn
Hihi bài 1 dòng 2 thỏa điều kiện nên đánh giá ok.Trời ơi, là sao vậy ta. Tập tin bài 1 Bạn gửi lên cũng toàn "NG" mà.
Bạn thử đặt trường hợp nào OK bằng công thức OT coi lại xem sao.
---------
Hic, mà sao bài này có mỗi một mình tham gia vậy. T_T
Hihi bài 1 dòng 2 thỏa điều kiện nên đánh giá ok.
Tại vì theo file của mình thì ở cột leadtime(hàm networkdays phải chạy trước sau đó mới tới cột đánh giá)
Sao hok ai vào hỗ trợ hết. Hihi
Option Explicit
Sub TapCode_DEJN()
Dim BangDo(), dlVao(), dlRa_DE(), dlRa_N(), dlRa_J(), dict As Object, key As Variant
Dim lR As Long, i As Long, r As Long, rOFF As Range, cE As Variant, cJ As Variant
Dim shDATA As Worksheet, shtType As Worksheet, wsF As WorksheetFunction
Const KoTimThay As String = "#NA"
Set wsF = WorksheetFunction
Set shDATA = ThisWorkbook.Worksheets("DATA")
Set shtType = ThisWorkbook.Worksheets("TYPE")
Set dict = CreateObject("Scripting.Dictionary")
With shtType
lR = .Range("B" & Rows.Count).End(xlUp).Row
If lR < 2 Then GoTo Tieptuc
BangDo = .Range("B2:D" & lR).Value
For i = 1 To UBound(BangDo, 1)
dict.Item(BangDo(i, 1)) = i
Next i
End With
Tieptuc:
With shDATA
lR = .Range("C" & Rows.Count).End(xlUp).Row
If lR < 2 Then Exit Sub
dlVao = .Range("A2:A" & lR).Resize(, 29).Value
Set rOFF = .Range("I2:I" & lR)
ReDim dlRa_DE(1 To UBound(dlVao, 1), 1 To 2)
ReDim dlRa_N(1 To UBound(dlVao, 1), 1 To 1)
ReDim dlRa_J(1 To UBound(dlVao, 1), 1 To 1)
For i = 1 To UBound(dlVao, 1)
key = dlVao(i, 3)
If dict.exists(key) Then
r = dict.Item(key)
dlRa_DE(i, 1) = BangDo(r, 2)
dlRa_DE(i, 2) = BangDo(r, 3)
Else
dlRa_DE(i, 1) = KoTimThay
dlRa_DE(i, 2) = KoTimThay
End If
dlRa_J(i, 1) = wsF.NetworkDays_Intl(dlVao(i, 1), dlVao(i, 8), "0000000", rOFF) + 1
cE = dlRa_DE(i, 2): cJ = dlRa_J(i, 1)
If (cE = 1 And cJ <= 1) Or (cE = 3 And cJ <= 1) Or (cE = 5 And cJ <= 3) Then _
dlRa_N(i, 1) = "OK" Else dlRa_N(i, 1) = "NG"
Next i
.Range("D2").Resize(UBound(dlRa_DE, 1), UBound(dlRa_DE, 2)).Value = dlRa_DE
.Range("J2").Resize(UBound(dlRa_J)).Value = dlRa_J
.Range("N2").Resize(UBound(dlRa_N)).Value = dlRa_N
End With
End Sub
Hihi thanks bạn để xem lại xem sao hok được nhờ diễn đàn tiếp hihiXin lỗi bạn nói OT mới để ý, công thức của bạn các cột sau phụ thuộc vào các cột trước.
OT sửa lại, nhưng vẫn thấy ra toàn "NG" không thôi, Bạn thử thay lại điều kiện xem sao.
OT cho bọn trẻ măm măm cái đã Hic!
Mã:Option Explicit Sub TapCode_DEJN() Dim BangDo(), dlVao(), dlRa_DE(), dlRa_N(), dlRa_J(), dict As Object, key As Variant Dim lR As Long, i As Long, r As Long, rOFF As Range, cE As Variant, cJ As Variant Dim shDATA As Worksheet, shtType As Worksheet, wsF As WorksheetFunction Const KoTimThay As String = "#NA" Set wsF = WorksheetFunction Set shDATA = ThisWorkbook.Worksheets("DATA") Set shtType = ThisWorkbook.Worksheets("TYPE") Set dict = CreateObject("Scripting.Dictionary") With shtType lR = .Range("B" & Rows.Count).End(xlUp).Row If lR < 2 Then GoTo Tieptuc BangDo = .Range("B2:D" & lR).Value For i = 1 To UBound(BangDo, 1) dict.Item(BangDo(i, 1)) = i Next i End With Tieptuc: With shDATA lR = .Range("C" & Rows.Count).End(xlUp).Row If lR < 2 Then Exit Sub dlVao = .Range("A2:A" & lR).Resize(, 29).Value Set rOFF = .Range("I2:I" & lR) ReDim dlRa_DE(1 To UBound(dlVao, 1), 1 To 2) ReDim dlRa_N(1 To UBound(dlVao, 1), 1 To 1) ReDim dlRa_J(1 To UBound(dlVao, 1), 1 To 1) For i = 1 To UBound(dlVao, 1) key = dlVao(i, 3) If dict.exists(key) Then r = dict.Item(key) dlRa_DE(i, 1) = BangDo(r, 2) dlRa_DE(i, 2) = BangDo(r, 3) Else dlRa_DE(i, 1) = KoTimThay dlRa_DE(i, 2) = KoTimThay End If dlRa_J(i, 1) = wsF.NetworkDays_Intl(dlVao(i, 1), dlVao(i, 8), "0000000", rOFF) + 1 cE = dlRa_DE(i, 2): cJ = dlRa_J(i, 1) If (cE = 1 And cJ <= 1) Or (cE = 3 And cJ <= 1) Or (cE = 5 And cJ <= 3) Then _ dlRa_N(i, 1) = "OK" Else dlRa_N(i, 1) = "NG" Next i .Range("D2").Resize(UBound(dlRa_DE, 1), UBound(dlRa_DE, 2)).Value = dlRa_DE .Range("J2").Resize(UBound(dlRa_J)).Value = dlRa_J .Range("N2").Resize(UBound(dlRa_N)).Value = dlRa_N End With End Sub
Sub Run()
Dim i As Integer, Dic As Object, TypeArr() As Variant, Data() As Variant
Dim TypeLr As Integer, DataLr As Integer, TypeIndex As Integer, CreateDay() As Variant
Dim Off() As Variant
With Sheet5
TypeLr = .Range("B" & .Rows.Count).End(xlUp).Row
TypeArr = .Range("B2:D" & TypeLr).Value
End With
With Sheet3
DataLr = .Range("A" & .Rows.Count).End(xlUp).Row
Data = .Range("C2:N" & DataLr).Value
CreateDay = .Range("A2:A" & DataLr).Value
Off = .Range("I2:I" & DataLr).Value
End With
Set Dic = CreateObject("Scripting.dictionary")
If TypeLr < 2 Or DataLr < 2 Then
MsgBox "No data"
Exit Sub
End If
'---------------------------------------------
' Xu ly cot D va cot E
For i = 1 To UBound(TypeArr, 1)
If Not Dic.exists(TypeArr(i, 1)) Then Dic.Add TypeArr(i, 1), i
Next i
For i = 1 To UBound(Data, 1)
If Dic.exists(Data(i, 1)) Then
TypeIndex = Dic.Item(Data(i, 1))
Data(i, 2) = TypeArr(TypeIndex, 2)
Data(i, 3) = TypeArr(TypeIndex, 3)
Else
Data(i, 2) = "Khong tim thay"
Data(i, 3) = TypeArr(TypeIndex, 3)
End If
'------------------------------------------
' Xu ly cot J
Data(i, 8) = WorksheetFunction.NetworkDays_Intl(CreateDay(i, 1), Data(i, 6), "0000000", Off) + 1
'------------------------------------------
' Xu ly cot N
If Data(i, 3) = 1 And Data(i, 8) <= 1 Then
Data(i, 12) = "OK"
ElseIf Data(i, 3) = 3 And Data(i, 8) <= 1 Then
Data(i, 12) = "OK"
ElseIf Data(i, 3) = 5 And Data(i, 8) <= 3 Then
Data(i, 12) = "OK"
Else
Data(i, 12) = "NG"
End If
Next i
Sheet3.Range("C2:N" & DataLr) = Data
MsgBox "Done", , "^_^"
End Sub