Xin rút gọn câu lệnh

Liên hệ QC

mymichau

Thành viên hoạt động
Tham gia
28/11/06
Bài viết
172
Được thích
114
Đây là code của file Tim_no (trên diễn đàn), em có sử dụng và tăng hêm nhiều cột dữ liệu. Nhưng với dữ liệu khỏang trên 30 cột, thì có cách nào rút gọn lại được không.
Mã:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim dong1, dong2
Dim gia_tri
If Target.Row = 2 And Target.Column = 3 Then
    dong1 = 5
    dong2 = 5
    
    gia_tri = Target.Value
    Sheets("out_put").Range("B5:G100").Clear
    Do While Len(Trim(Sheets("in_put").Cells(dong1, 2).Value)) > 0
        If Sheets("in_put").Cells(dong1, 2).Value = gia_tri Then
            
            1/ Sheets("out_put").Cells(dong2, 2).Value = Sheets("in_put").Cells(dong1, 2).Value
            2/ Sheets("out_put").Cells(dong2, 3).Value = Sheets("in_put").Cells(dong1, 3).Value
            3/ Sheets("out_put").Cells(dong2, 4).Value = Sheets("in_put").Cells(dong1, 4).Value
            4/ Sheets("out_put").Cells(dong2, 5).Value = Sheets("in_put").Cells(dong1, 5).Value
            5/ Sheets("out_put").Cells(dong2, 6).Value = Sheets("in_put").Cells(dong1, 6).Value
            6/ Sheets("out_put").Cells(dong2, 7).Value = Sheets("in_put").Cells(dong1, 7).Value
            dong2 = dong2 + 1
            
        End If
        dong1 = dong1 + 1
    Loop
End If
End Sub
Rút gọn lại từ 1>6
 
Chỉnh sửa lần cuối bởi điều hành viên:
Bạn thử xem sao:
Mã:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim dong1, dong2
Dim gia_tri
[COLOR=red][B]Dim socot, i[/B]
[/COLOR]If Target.Row = 2 And Target.Column = 3 Then
    dong1 = 5
    dong2 = 5
    [COLOR=red][B]socot = 30[/B]
[/COLOR]   gia_tri = Target.Value
    Sheets("out_put").Range("B5:G100").Clear
    Do While Len(Trim(Sheets("in_put").Cells(dong1, 2).Value)) > 0
        If Sheets("in_put").Cells(dong1, 2).Value = gia_tri Then
            [COLOR=red]For i = 1 To socot
                Sheets("out_put").Cells(dong2, i+1).Value = Sheets("in_put").Cells(dong1, i+1).Value
            Next i
[/COLOR]           dong2 = dong2 + 1
        End If
        dong1 = dong1 + 1
    Loop
End If
End Sub
 
Đoạn nầy bạn có thề rút ngắn

Đổi câu - If Target.Row = 2 And Target.Column = 3 Then

Thành là - If Target.Address = "$C$2" Then

Tức là rút gọn 2 test thành 1 nếu workbook có nhiều giữ liệu thì máy sẽ chạy nhanh hơn một tí.

Mến
 
Em cảm ơn các anh NVSON và DIGITA nhiều !
 
Web KT

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

Back
Top Bottom