Giúp sửa code liên quan conditional formatting (tự record marco) (1 người xem)

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

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

pomete

Thành viên hoạt động
Tham gia
13/10/08
Bài viết
170
Được thích
57
Tôi có một code tự record trong excel. Lúc record thì chạy ra kết quả ok nhưng khi chạy lại thì lại báo lỗi. Nhờ các bác xem giúp
Sub Macro2()
'
' Macro2 Macro
'
'
Range("J3:Y230").Select
Selection.NumberFormat = "0%"
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=OR($F3=""%HT"",$F3=""DK"")"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
ExecuteExcel4Macro "(2,1,""0%"")"

Selection.FormatConditions(1).StopIfTrue = False
Range("J3").Select
End Sub

FIle excel của tôi có nhiều cột, bài toán chỉ là format lại định dạng toàn bộ thông tin từ cột J đến cột Y theo giá trị tương ứng ở cột F. Nếu giá trị ở cột F là "%HT" hoặc "DK" thì toàn bộ định dạng tương ứng ở cột J đến Y sẽ là "%"
(Báo lỗi ở dòng bôi màu đỏ)
 
Mình không nghĩ ra macro4 bị gì nữa như bạn có thể chỉnh sửa lại

PHP:
Sub formatcon()
With Range("J3:Y230")
    .Parent.Cells.FormatConditions.Delete
    .FormatConditions.Add xlExpression, , "=OR($F3=""%HT"",$F3=""DK"")"
    .FormatConditions(.FormatConditions.Count).SetFirstPriority
    .FormatConditions(1).NumberFormat = "0%"
    .FormatConditions(1).StopIfTrue = False
End With

End Sub
 
Upvote 0
Sử dụng code của bạn cũng được nhưng kết quả chạy ra không đúng. Bạn xem lại giúp mình nhé, thanks bạn!
 

File đính kèm

Upvote 0
Sao máy lại báo lỗi dòng này nhỉ?

.FormatConditions(.FormatConditions.Count).SetFirstPriority

Bảo rằng không có thuộc tính trên (X2003)
 
Upvote 0
Đâu anh record macro xem có thuộc tính đó không?
Bạn sửa lại thành vầy xem chắc lỗi do selection
Mã:
Sub formatcon2()
Range("J3:Y230").Select
Selection.FormatConditions.Delete
Selection.FormatConditions.Add 2, , "=OR($F3=""%HT"",$F3=""DK"")"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
Selection.FormatConditions(1).NumberFormat = "0%"
Selection.FormatConditions(1).StopIfTrue = False
End Sub

Muốn đẹp thì sửa lại vầy
PHP:
Sub formatcon()
Dim Vung As Range
Range("J3:Y230").Select: Set Vung = Selection 'thay doi vung cho phu hop
With Vung
            .FormatConditions.Delete
            .FormatConditions.Add 2, , "=OR($F3=""%HT"",$F3=""DK"")"
            .FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
            .FormatConditions(1).NumberFormat = "0%"
            .FormatConditions(1).StopIfTrue = False
End With
End Sub
 
Upvote 0
FormatCondition.SetFirstPriority
Method
Sets the priority value for this conditional formatting rule to "1" so that it will be evaluated before all other rules on the worksheet.
Version Information
Version Added: Excel
2007
 
Upvote 0
Phần code đầu tiên của bạn nmhung49 thì chạy ok rùi nhưng phần code phía sau của bạn thì bị báo lỗi. Bây giờ mình muốn khi mở file excel ra thì nó tự động chạy luôn thì có phải là viết như thế này không:

Private Sub Workbook_Open()
Sheets("KT").Select
Range("J3:Y230").Select
Selection.FormatConditions.Delete
Selection.FormatConditions.Add 2, , "=OR($F3=""%HT"",$F3=""DK"")"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
Selection.FormatConditions(1).NumberFormat = "0%"
Selection.FormatConditions(1).StopIfTrue = False
End Sub

Thanks bạn!
 
Upvote 0
Phần code đầu tiên của bạn nmhung49 thì chạy ok rùi nhưng phần code phía sau của bạn thì bị báo lỗi. Bây giờ mình muốn khi mở file excel ra thì nó tự động chạy luôn thì có phải là viết như thế này không:

Private Sub Workbook_Open()
Sheets("KT").Select
Range("J3:Y230").Select
Selection.FormatConditions.Delete
Selection.FormatConditions.Add 2, , "=OR($F3=""%HT"",$F3=""DK"")"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
Selection.FormatConditions(1).NumberFormat = "0%"
Selection.FormatConditions(1).StopIfTrue = False
End Sub

Thanks bạn!

Theo tôi nghĩ, riêng vấn đề Conditional Formatting thì chỉ cần chạy tiếp khi có sự thay đổi (hơn) về số dòng (230) đã quy định, còn không thì nó đã tự có sẳn sau khi chạy lần đầu tiên rồi nên không cần phải chạy thêm mỗi lần mở file hay tự động gì cả.
 
Upvote 0
Đúng như anh nói chỉ cần formatting lần đầu là nó áp dụng toàn bộ các lần sau thôi, trừ khi có người khác delete formating thôi

Đoạn code trên với dưới là 1 mà bạn!!!
 
Upvote 0
Web KT

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

Back
Top Bottom