Thứ Bảy, 30 tháng 4, 2016

VB Script trong Excel

Hôm nay mới dùng thử VB để thao tác trên excel, lọ mọ cả buổi chiều rồi cũng xong, cũng không khó lắm, lưu vào đây để sau nhớ còn dùng

Script này dùng để lọc giữ liệu ở các cell đã chọn tổng hợp lại theo 3 level, nếu quen với VB rồi thì chắc sẽ mất không tới 20 phút là xong.

Sub test()

    Dim i As Integer
    Dim j As Integer
    For j = 2 To 2000
        For i = 5 To 100
            If Not IsEmpty(ThisWorkbook.Sheets("report_final").Cells(j, i).Value) Then
                If ThisWorkbook.Sheets("report_final").Cells(j, i).Value < 430 Then
                    If IsEmpty(ThisWorkbook.Sheets("report_final").Cells(j, 2).Value) Then
                        ThisWorkbook.Sheets("report_final").Cells(j, 2).Value = ThisWorkbook.Sheets("report_final").Cells(j, i).Value
                    Else
                        ThisWorkbook.Sheets("report_final").Cells(j, 2).Value = ThisWorkbook.Sheets("report_final").Cells(j, 2).Value & ", " & ThisWorkbook.Sheets("report_final").Cells(j, i).Value
                    End If
                End If
            End If
            
            If Not IsEmpty(ThisWorkbook.Sheets("report_final").Cells(j, i).Value) Then
                If ThisWorkbook.Sheets("report_final").Cells(j, i).Value > 429 And ThisWorkbook.Sheets("report_final").Cells(j, i).Value < 755 Then
                    If IsEmpty(ThisWorkbook.Sheets("report_final").Cells(j, 3).Value) Then
                        ThisWorkbook.Sheets("report_final").Cells(j, 3).Value = ThisWorkbook.Sheets("report_final").Cells(j, i).Value
                    Else
                        ThisWorkbook.Sheets("report_final").Cells(j, 3).Value = ThisWorkbook.Sheets("report_final").Cells(j, 3).Value & ", " & ThisWorkbook.Sheets("report_final").Cells(j, i).Value
                    End If
                End If
            End If
            
            If Not IsEmpty(ThisWorkbook.Sheets("report_final").Cells(j, i).Value) Then
                If ThisWorkbook.Sheets("report_final").Cells(j, i).Value > 754 Then
                    If IsEmpty(ThisWorkbook.Sheets("report_final").Cells(j, 4).Value) Then
                        ThisWorkbook.Sheets("report_final").Cells(j, 4).Value = ThisWorkbook.Sheets("report_final").Cells(j, i).Value
                    Else
                        ThisWorkbook.Sheets("report_final").Cells(j, 4).Value = ThisWorkbook.Sheets("report_final").Cells(j, 4).Value & ", " & ThisWorkbook.Sheets("report_final").Cells(j, i).Value
                    End If
                End If
            End If
                
        Next
    Next

End Sub


Không có nhận xét nào:

Đăng nhận xét