DMQ
Thành viên dốt
- Tham gia
- 21/3/12
- Bài viết
- 722
- Được thích
- 57
- Giới tính
- Nam
' . . . . . . . . . '
With Sheets("Rec")
lr = .Range("A" & Rows.Count).End(xlUp).Row
For I = 5 To lr
Next I
.Cells(Row, 5).Value = Description
End With
' . . . . . . . . . '
Private Sub Worksheet_Change(ByVal Target As Range)
Dim I As Long, lR As Long, counter As Integer, UnitWeight As Integer, Col As Integer, Row As Integer
Dim MaSo As String, Description As String
If Target.Count > 1 Then Exit Sub
2 If Not Intersect(Target, Range("D5:K10000")) Is Nothing Then
If IsEmpty(Target) Then Exit Sub
Row = Target.Row: Col = Target.Column
If IsEmpty(Cells(Row, 1)) Then
MsgBox "Ban Cân Nhâp Du Liêu Vào Côt Ngày Truóc", vbCritical
Exit Sub
End If
MaSo = Cells(Row, 4): DoDay = Cells(Row, 6)
Rong = Cells(Row, 7): Dai = Cells(Row, 8)
3 If Col = 4 Then '** '
With Sheets("Sue")
lR = .Range("B" & Rows.Count).End(xlUp).Row
For I = 8 To lR
If .Range("B" & I) = MaSo Then Description = .Range("I" & I):
Exit For
Next I
End With
With Sheets("Rec")
' lr = .Range("A" & Rows.Count).End(xlUp).Row
' For I = 5 To lr
' Next I
.Cells(Row, 5).Value = Description
End With
30 If Col = 4 Then
With Sheets("Sue")
lR = .Range("B" & Rows.Count).End(xlUp).Row
For I = 8 To lR
If .Range("B" & I) = MaSo Then DoDay = .Range("E" & I): Exit For
Next I
End With
With Sheets("Rec")
1
.Cells(Row, 6).Value = DoDay
End With
31 End If
40 If Col = 4 Then
With Sheets("Sue")
lR = .Range("B" & Rows.Count).End(xlUp).Row
For I = 8 To lR
If .Range("B" & I) = MaSo Then Rong = .Range("F" & I): Exit For
Next I
End With
With Sheets("Rec")
2
.Cells(Row, 7).Value = Rong
End With
41 End If
50 If Col = 4 Then
With Sheets("Sue")
lR = .Range("B" & Rows.Count).End(xlUp).Row
For I = 8 To lR
If .Range("B" & I) = MaSo Then Dai = .Range("G" & I): Exit For
Next I
End With
With Sheets("Rec")
3
.Cells(Row, 8).Value = Dai
End With
51 End If
60 If Col = 4 Then
With Sheets("Sue")
lR = .Range("B" & Rows.Count).End(xlUp).Row
For I = 8 To lR
If .Range("B" & I) = MaSo Then UnitWeight = .Range("H" & I): Exit For
Next I
End With
With Sheets("Rec")
4
.Cells(Row, 9).Value = UnitWeight
End With
61 End If
39 End If
End If
End Sub
Ý mình là mệnh đề lệnhÝ . . . là sao em không hiểu?????
If Col = 4 Then
If Not Intersect(Target, Range("D5:K10000")) Is Nothing Then
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r&, arr()
If Target.Column <> 4 Or Target.Count > 1 Or IsEmpty(Target) Then Exit Sub
r = Target.Row
If IsEmpty(Cells(r, 1)) Then
MsgBox "Ban can nhap du lieu vao cot Ngay truoc", vbCritical
Exit Sub
End If
With Sheets("Sue")
lr = .Range("B" & Rows.Count).End(xlUp).Row
For i = 6 To lr
If .Range("B" & i) = Target Then
ReDim arr(1 To 1, 1 To 6)
arr(1, 1) = Target: arr(1, 2) = .Cells(i, "I").Value
arr(1, 3) = .Cells(i, "E").Value: arr(1, 4) = .Cells(i, "F").Value
arr(1, 5) = .Cells(i, "G").Value: arr(1, 6) = .Cells(i, "H").Value
Target.Resize(1, 6).Value = arr
Exit Sub
End If
Next
End With
End Sub
Sao không phải là code khác nhanh hơn.Tiện hơn mà cứ phải là rút gọn code của bạn.Nếu đúng ý của tôi nói ở bài 12 thì code khác nhanh hơn.Bởi vậy em mới nhờ rút gọn lại dùm em, thầy @SA_DQ
rút gọn dùm em với.
Hình như code này chỉ là cập nhập giá trị cũ.Không có thêm mới.Đầu tiên em cứ tưởng bác sa viết hóa ra không phải.Code hiện tại có rất nhiều thừa thãi: Nếu chỉ quan tâm đến việc thay đổi giá trị cột D thì chỉ vầy là đủ:
PHP:Private Sub Worksheet_Change(ByVal Target As Range) Dim r&, arr() If Target.Column <> 4 Or Target.Count > 1 Or IsEmpty(Target) Then Exit Sub r = Target.Row If IsEmpty(Cells(r, 1)) Then MsgBox "Ban can nhap du lieu vao cot Ngay truoc", vbCritical Exit Sub End If With Sheets("Sue") lr = .Range("B" & Rows.Count).End(xlUp).Row For i = 6 To lr If .Range("B" & i) = Target Then ReDim arr(1 To 1, 1 To 6) arr(1, 1) = Target: arr(1, 2) = .Cells(i, "I").Value arr(1, 3) = .Cells(i, "E").Value: arr(1, 4) = .Cells(i, "F").Value arr(1, 5) = .Cells(i, "G").Value: arr(1, 6) = .Cells(i, "H").Value Target.Resize(1, 6).Value = arr Exit Sub End If Next End With End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r&, arr(), found As Boolean, newV, oldV
If Target.Column <> 4 Or Target.Count > 1 Or IsEmpty(Target) Then Exit Sub
r = Target.Row
If IsEmpty(Cells(r, 1)) Then
MsgBox "Ban can nhap du lieu vao cot Ngay truoc", vbCritical
Exit Sub
End If
With Sheets("Sue")
back:
lr = .Range("B" & Rows.Count).End(xlUp).Row
For i = 6 To lr
If .Range("B" & i) = Target Then
found = True
ReDim arr(1 To 1, 1 To 6)
arr(1, 1) = Target: arr(1, 2) = .Cells(i, "I").Value
arr(1, 3) = .Cells(i, "E").Value: arr(1, 4) = .Cells(i, "F").Value
arr(1, 5) = .Cells(i, "G").Value: arr(1, 6) = .Cells(i, "H").Value
Target.Resize(1, 6).Value = arr
Exit Sub
End If
Next
If Not found Then
If MsgBox("Ma so moi, ban co muon bo sung khong?", vbYesNo) = vbYes Then
ip1 = InputBox("nhap do Day:")
ip2 = InputBox("nhap chieu Rong:")
ip3 = InputBox("nhap chieu Dai")
ip4 = InputBox("nhap Trong Luong:")
If Not IsNumeric(ip1 + ip2 + ip3 + ip4) Or ip1 < 0 Or ip2 < 0 Or ip3 < 0 Or ip4 < 0 Then
MsgBox "Du liêu nhap sai!", vbCritical
Exit Sub
End If
.Cells(lr + 1, "B").Value = Target
.Cells(lr + 1, "E").Value = ip1
.Cells(lr + 1, "F").Value = ip2
.Cells(lr + 1, "G").Value = ip3
.Cells(lr + 1, "H").Value = ip4
.Cells(lr + 1, "I").Value = ip1 & "x" & ip2 & "x" & ip3
GoTo back
Else
newV = Target.Value
With Application
.EnableEvents = False
.Undo
oldV = Target.Value
.EnableEvents = True
End With
Target.Value = oldV
End If
End If
End With
End Sub
Thêm chút xíu vào nhé:Không cần tạo danh mục mới đâu bạn @bebo021999 ơi, ở sheet Rec cột tổng trọng lượng mình đang dùng công thức, bạn viết code thay công thức dùm mình được không ạ
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r&, arr()
If Target.Column <> 4 Or Target.Count > 1 Or IsEmpty(Target) Then Exit Sub
r = Target.Row
If IsEmpty(Cells(r, 1)) Then
MsgBox "Ban can nhap du lieu vao cot Ngay truoc", vbCritical
Exit Sub
End If
With Sheets("Sue")
lr = .Range("B" & Rows.Count).End(xlUp).Row
For i = 6 To lr
If .Range("B" & i) = Target Then
ReDim arr(1 To 1, 1 To 6)
arr(1, 1) = Target: arr(1, 2) = .Cells(i, "I").Value
arr(1, 3) = .Cells(i, "E").Value: arr(1, 4) = .Cells(i, "F").Value
arr(1, 5) = .Cells(i, "G").Value: arr(1, 6) = .Cells(i, "H").Value
Target.Resize(1, 6).Value = arr
With Sheets("Rec")
lr = .Range("D" & Rows.Count).End(xlUp).Row
.Range("K2:K" & lr).Formula = "=I2*J2"
End With
Exit Sub
End If
Next
End With
End Sub