Tìm lỗi sai trong macro lọc dữ liệu (1 người xem)

  • Thread starter Thread starter ditimdl
  • Ngày gửi Ngày gửi
Liên hệ QC

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

ditimdl

Thành viên thường trực
Tham gia
11/10/06
Bài viết
378
Được thích
107
Giới tính
Nam
Nghề nghiệp
Pharmacist
Các bạn xem trong file đính kèm giúp mình xem macro sai ở chổ nào mà khi lọc nó không ra đúng với kết quả so với điều kiện lọc AF.
 

File đính kèm

Bạn đổi điều kiện từ dạng ngày/tháng/năm thành tháng/ngày/năm theo đúng dữ liệu thì mới được
>=6/1/2012 và <=6/10/2012
 
Cái này mình biết nhưng mình đã format dữ liệu theo định dạng dd/mm/yy và cả trong hệ thống mình cũng đã thay đổi theo tương tự nhưng nó vẫn không hiểu? Hay là đối với điều kiện lọc phải nhập theo định dạng mm/dd/yyyy?
 
Nếu muốn chắc chắn thì dùng công thức sau cho điều kiện lọc:
=">=" & DATE(2012, 6, 1) và
="<=" & DATE(2012, 6, 10)
 
Nếu muốn chắc chắn thì dùng công thức sau cho điều kiện lọc:
=">=" & DATE(2012, 6, 1) và
="<=" & DATE(2012, 6, 10)
Cái này thì chắc như bắp rồi :)
Cho mình hỏi khi chạy sub lọc, nếu số bản ghi = 0 thì dừng lại các quá trình sau đó thì phải thêm như thế nào?
Cám ơn bạn đã hổ trợ rất nhiều!
 
Tôi thêm 1 số ý vào code của bạn:
PHP:
Sub Macro1()
'
' Macro1 Macro
'
'
With Application
.ScreenUpdating = False
.EnableEvents = False
    Sheets("Sheet2").Select
    Rows("6:6").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Delete Shift:=xlUp
    Sheets("Sheet1").Select
    Range("A5:C2000").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
        Range("E2:G3"), Unique:=False
    Range("A5").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.SpecialCells(xlCellTypeVisible).Select
    If Selection.Rows.Count = 1 Then GoTo endSub
    Selection.Copy
    Sheets("Sheet2").Select
    Range("A6").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    
    Range("A5").Select
endSub:
    Sheet1.ShowAllData
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
 
Mã:
Sub Macro1()
'
' Macro1 Macro
'
'
With Application
.ScreenUpdating = False
.EnableEvents = False
    Sheets("Sheet2").Select
    Rows("6:6").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Delete Shift:=xlUp
    Sheets("Sheet1").Select
    Range("A5:C2000").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
        Range("E2:G3"), Unique:=False
    Range("A5").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.SpecialCells(xlCellTypeVisible).Select
    If Selection.Rows.Count = 1 Then GoTo endSub
    Selection.Copy
    Sheets("Sheet2").Select
    Range("A6").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    
    Range("A5").Select
endSub:
    Sheet1.ShowAllData
.EnableEvents = True
.ScreenUpdating = True
End With
Sheets("Sheet2").Select
Range("A6").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
    End With
    Range("A2:C2").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Selection.Merge
    ActiveCell.FormulaR1C1 = "Báo cáo"
    Range("A3").Select
End Sub
Không theo như ý muốn, sau khi lọc mình tiến hành kẻ khung... nhưng bản ghi = 0 thì nó vẫn tiến hành những công việc sau đó.
Bạn xem mình thêm vào sai ở đâu?
 
Mã:
Sub Macro1()
'
' Macro1 Macro
'
'
With Application
.ScreenUpdating = False
.EnableEvents = False
    Sheets("Sheet2").Select
    Rows("6:6").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Delete Shift:=xlUp
    Sheets("Sheet1").Select
    Range("A5:C2000").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
        Range("E2:G3"), Unique:=False
    Range("A5").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.SpecialCells(xlCellTypeVisible).Select
    If Selection.Rows.Count = 1 Then GoTo endSub
    Selection.Copy
    Sheets("Sheet2").Select
    Range("A6").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    
    Range("A5").Select

Sheets("Sheet2").Select
Range("A6").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
    End With
    Range("A2:C2").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Selection.Merge
    ActiveCell.FormulaR1C1 = "Báo cáo"
    Range("A3").Select
endSub:
    Sheet1.ShowAllData
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub

Tôi đã chèn thêm 1 lệnh nhảy (goto) khi không có dữ liệu thỏa điều kiện, nhưng bạn máy móc nối thêm vào dưới đoạn nhảy cho nên nó cứ làm tiếp. Bạn dùng lại cái mà tôi đã sửa ở trên.
 
Web KT

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

Back
Top Bottom