Hỏi về cách xóa dữ liệu trong file Excel (1 người xem)

Liên hệ QC

Người dùng đang xem chủ đề này

stomperinky

Thành viên mới
Tham gia
10/8/11
Bài viết
8
Được thích
0
[Solved] Hỏi về cách xóa dữ liệu trong file Excel

Em có 2 file Excel: goi mail 2014.xls, unsubscribe email.xls
Xin hỏi cách nào để xóa những email trong file goi mail 2014.xls mà xuất hiện trong file unsubscribe email.xls không? Cảm ơn ạ!
 

File đính kèm

Lần chỉnh sửa cuối:
Em có 2 file Excel: goi mail 2014.xls, unsubscribe email.xls
Xin hỏi cách nào để xóa những email trong file goi mail 2014.xls mà xuất hiện trong file unsubscribe email.xls không? Cảm ơn ạ!

bạn tải file đính kèm, cho chạy Macro.
- mình đã copy số liệu file unsubscribe email vào sheet2
- đặt điều kiện xoá tại cột C, sheet1
- bạn kiểm tra thấy đúng thì click nút Xoá nhé !
Mã:
Sub DeleteRows()
Dim vung As Range, cellsToDelete As Range, cell As Range
    Set cellsToDelete = Nothing
    Set vung = Range(Range("C65000").End(xlUp), Range("C2"))
    
    Application.ScreenUpdating = False
        For Each cell In vung
        If cell.Value = "xoa" Then
            If cellsToDelete Is Nothing Then
                Set cellsToDelete = cell
            Else
                Set cellsToDelete = Union(cellsToDelete, cell)
            End If
        End If
        Next cell
        
        cellsToDelete.EntireRow.Delete
    Application.ScreenUpdating = True
End Sub

Link: https://www.mediafire.com/?yqpew4t87dwx8za
 
Lần chỉnh sửa cuối:
Em có 2 file Excel: goi mail 2014.xls, unsubscribe email.xls
Xin hỏi cách nào để xóa những email trong file goi mail 2014.xls mà xuất hiện trong file unsubscribe email.xls không? Cảm ơn ạ!

Chào bạn,
Mình đã viết code xong cho bạn:

CÁCH 1:
Mã:
[COLOR=#0000ff]'bad on:
'1. Range(strtmp).Delete: phat' sinh loi~ khi strtmp nhan^. qua' nhieu phan tu?
'2. Msgbox(strtmp): khong the hien duoc het list can xoa !
[/COLOR]
Sub test()
    Dim cllDta As Range
    Dim cllLst As Range
    Dim strtmp As String
    
    Application.ScreenUpdating = False
    
    For Each cllDta In Range("data")
        For Each cllLst In Range("list")
            If cllDta.Value = cllLst.Value Then
                strtmp = strtmp & cllDta.Address & ","
                Exit For
            End If
        Next cllLst
    Next cllDta
    
    If strtmp = "" Then: Exit Sub 'catch error on next step
    strtmp = Mid(strtmp, 1, Len(strtmp) - 1)
    Range(strtmp).Delete
    MsgBox "Deleted: " & strtmp
    
    Application.ScreenUpdating = True
End Sub

CÁCH 2:

Mã:
[COLOR=#0000ff]'bad on
'MsgBox info
'khong the hien duoc het list xoa'[/COLOR]
[COLOR=#0000ff]' .Find tra? ve gia'  tri. co' chua' doi' tuong. cll.value[/COLOR]
Sub deleteFormList()
Dim rng As Range: Set rng = Range("data")
Dim cll As Range
Dim result As Range
Dim tmp As String
Dim info As String

    With rng
        For Each cll In Range("list")
            Set result = .Find(what:=cll.Value, _
                            after:=.Cells(1, 1), _
                            LookIn:=xlValues, _
                            lookat:=xlPart, _
                            searchdirection:=xlPrevious, _
                            MatchCase:=True)
            If Not result Is Nothing Then
                Do
                    Set result = .FindNext(result)
                    If result Is Nothing Then: Exit Do 'catch error
                    tmp = result.Address
                    info = info & result.Address & ": " & result.Value & Chr(13)
                    result.Delete
                    Set result = Range(tmp)
                Loop
            End If
        Next cll
    End With
    MsgBox info
End Sub

Hai cách trên khi xử lý, đều cho kết quả như nhau; và
Khi thực hiện check 25.000 rows dữ liệu như trong file của bạn,
cả 2 code trên đều hoạt động khá chậm chập.

Bạn nào có cách viết hay hơn, vui lòng fix lại giúp mình !
Mình sẽ rất vui...
Thân ái !
 
Lần chỉnh sửa cuối:
Web KT

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

Back
Top Bottom