Xin giúp cải tiến hàm kẻ dòng trong bảng để nó chạy nhanh hơn

Liên hệ QC

civil

Thành viên mới
Tham gia
19/8/07
Bài viết
12
Được thích
0
Nghề nghiệp
Civil engineer
Em có hàm này, giúp việc kẻ dòng, kẻ bảng trong bảng dự toán chi tiết, nhưng thực hiện hơi chậm, Xin các cao thủ giúp cải tiến hàm này cho nó chạy nhanh hơn được không.
Mã:
Function kedong()
'Worksheets("Don gia chi tiet").Activate
For i = 7 To ActiveSheet.UsedRange.Rows.Count
If Cells(i, 1).Value <> 0 Then
Range(Cells(i, 1), Cells(i, 16)).Select
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
Else
Range(Cells(i, 1), Cells(i, 16)).Select
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlHairline
        .ColorIndex = xlAutomatic
    End With
End If
Next i
End Function
 
Mã:
Sub kehang()
'
' kehang Macro
' Macro recorded 24/01/2008 by VNN.R9
'
'
    Selection.FormatConditions.Delete
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=IF($A5<>"""";TRUE;FALSE)"
    Selection.FormatConditions(1).Borders(xlLeft).LineStyle = xlNone
    Selection.FormatConditions(1).Borders(xlRight).LineStyle = xlNone
    With Selection.FormatConditions(1).Borders(xlTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    Selection.FormatConditions(1).Borders(xlBottom).LineStyle = xlNone
    Selection.FormatConditions(1).Interior.ColorIndex = 34
    Range("A5:I5").Select
    Selection.FormatConditions.Delete
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=IF($A5<>"""";TRUE;FALSE)"
    Selection.FormatConditions(1).Borders(xlLeft).LineStyle = xlNone
    Selection.FormatConditions(1).Borders(xlRight).LineStyle = xlNone
    With Selection.FormatConditions(1).Borders(xlTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    Selection.FormatConditions(1).Borders(xlBottom).LineStyle = xlNone
    Selection.FormatConditions(1).Interior.ColorIndex = 34
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=IF($B5<>"""";TRUE;FALSE)"
    With Selection.FormatConditions(2).Borders(xlLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.FormatConditions(2).Borders(xlRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.FormatConditions(2).Borders(xlTop)
        .LineStyle = xlContinuous
        .Weight = xlHairline
        .ColorIndex = xlAutomatic
    End With
    With Selection.FormatConditions(2).Borders(xlBottom)
        .LineStyle = xlContinuous
        .Weight = xlHairline
        .ColorIndex = xlAutomatic
    End With
    Range("A5").Select
    ActiveCell.FormulaR1C1 = "1"
    Range("B5").Select
    ActiveCell.FormulaR1C1 = "a"
    Range("B6").Select
    ActiveCell.FormulaR1C1 = "a"
    Range("B7").Select
    ActiveCell.FormulaR1C1 = "a"
    Range("B8").Select
    ActiveCell.FormulaR1C1 = "a"
    Range("B9").Select
    ActiveCell.FormulaR1C1 = "a"
    Range("A5:I5").Select
    Selection.AutoFill Destination:=Range("A5:I168"), Type:=xlFillDefault
    Range("A5:I168").Select
    Range("F161").Select
    ActiveWindow.SmallScroll Down:=-168
    Range("A6").Select
    Selection.ClearContents
    Range("A7").Select
    Selection.ClearContents
    Range("A5:I5").Select
    Selection.FormatConditions.Delete
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=IF($A5<>"""";TRUE;FALSE)"
    With Selection.FormatConditions(1).Borders(xlLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.FormatConditions(1).Borders(xlRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.FormatConditions(1).Borders(xlTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.FormatConditions(1).Borders(xlBottom)
        .LineStyle = xlContinuous
        .Weight = xlHairline
        .ColorIndex = xlAutomatic
    End With
    Selection.FormatConditions(1).Interior.ColorIndex = 34
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=IF($B5<>"""";TRUE;FALSE)"
    With Selection.FormatConditions(2).Borders(xlLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.FormatConditions(2).Borders(xlRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.FormatConditions(2).Borders(xlTop)
        .LineStyle = xlContinuous
        .Weight = xlHairline
        .ColorIndex = xlAutomatic
    End With
    With Selection.FormatConditions(2).Borders(xlBottom)
        .LineStyle = xlContinuous
        .Weight = xlHairline
        .ColorIndex = xlAutomatic
    End With
    Range("A5:I5").Select
    Selection.AutoFill Destination:=Range("A5:I200"), Type:=xlFillDefault
    Range("A5:I200").Select
    ActiveWindow.SmallScroll Down:=-213
    Range("A6").Select
    Selection.ClearContents
    Range("A7").Select
    Selection.ClearContents
    Range("A8").Select
    Selection.ClearContents
    Range("A9").Select
    Selection.ClearContents
    Range("A10").Select
    Selection.ClearContents
    Range("A11").Select
    Selection.ClearContents
    Range("A14:A19").Select
    Selection.ClearContents
    Range("A13").Select
    Selection.ClearContents
    ActiveWindow.SmallScroll Down:=6
    Range("A21:A28").Select
    Selection.ClearContents
    Range("B25").Select
    ActiveWindow.SmallScroll Down:=9
    Range("A30:A35").Select
    Selection.ClearContents
    Range("B31").Select
End Sub
 
Chỉnh sửa lần cuối bởi điều hành viên:
Upvote 0
Trời... kẽ dòng mà sao lại là Function nhỉ? ... Tôi nhìn code và đoán rằng đây chính là Format cells theo điều kiện nào đó... Code này có lẽ se chạy nếu dùng sự kiện Change...Đúng ra nó phải vầy:
PHP:
Private Sub Worksheet_Change(ByVal Target As Range)
   For i = 7 To ActiveSheet.UsedRange.Rows.Count
      If Cells(i, 1).Value <> 0 Then
         Range(Cells(i, 1), Cells(i, 16)).Select
         With Selection.Borders(xlEdgeTop)
             .LineStyle = xlContinuous
             .Weight = xlThin
             .ColorIndex = xlAutomatic
         End With
      Else
         Range(Cells(i, 1), Cells(i, 16)).Select
         With Selection.Borders(xlEdgeTop)
             .LineStyle = xlContinuous
             .Weight = xlHairline
             .ColorIndex = xlAutomatic
         End With
      End If
   Next i
End Sub
Nhưng theo tôi thấy thì tại sao bạn ko dùng chức năng sẳn có cũa Excel: Conditional Formating đễ làm điều này nhỉ? Code chi cho cực vậy!
ANH TUẤN
 
Upvote 0
Bác chĩnh sửa lại cái này xem

Sub kehang()
'
' kehang Macro
' Macro recorded 24/01/2008 by VNN.R9
'
'
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=IF($A5<>"""";TRUE;FALSE)"
Selection.FormatConditions(1).Borders(xlLeft).LineStyle = xlNone
Selection.FormatConditions(1).Borders(xlRight).LineStyle = xlNone
With Selection.FormatConditions(1).Borders(xlTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Selection.FormatConditions(1).Borders(xlBottom).LineStyle = xlNone
Selection.FormatConditions(1).Interior.ColorIndex = 34
Range("A5:I5").Select
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=IF($A5<>"""";TRUE;FALSE)"
Selection.FormatConditions(1).Borders(xlLeft).LineStyle = xlNone
Selection.FormatConditions(1).Borders(xlRight).LineStyle = xlNone
With Selection.FormatConditions(1).Borders(xlTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Selection.FormatConditions(1).Borders(xlBottom).LineStyle = xlNone
Selection.FormatConditions(1).Interior.ColorIndex = 34
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=IF($B5<>"""";TRUE;FALSE)"
With Selection.FormatConditions(2).Borders(xlLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.FormatConditions(2).Borders(xlRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.FormatConditions(2).Borders(xlTop)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
With Selection.FormatConditions(2).Borders(xlBottom)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
Range("A5").Select
ActiveCell.FormulaR1C1 = "1"
Range("B5").Select
ActiveCell.FormulaR1C1 = "a"
Range("B6").Select
ActiveCell.FormulaR1C1 = "a"
Range("B7").Select
ActiveCell.FormulaR1C1 = "a"
Range("B8").Select
ActiveCell.FormulaR1C1 = "a"
Range("B9").Select
ActiveCell.FormulaR1C1 = "a"
Range("A5:I5").Select
Selection.AutoFill Destination:=Range("A5:I168"), Type:=xlFillDefault
Range("A5:I168").Select
Range("F161").Select
ActiveWindow.SmallScroll Down:=-168
Range("A6").Select
Selection.ClearContents
Range("A7").Select
Selection.ClearContents
Range("A5:I5").Select
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=IF($A5<>"""";TRUE;FALSE)"
With Selection.FormatConditions(1).Borders(xlLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.FormatConditions(1).Borders(xlRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.FormatConditions(1).Borders(xlTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.FormatConditions(1).Borders(xlBottom)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
Selection.FormatConditions(1).Interior.ColorIndex = 34
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=IF($B5<>"""";TRUE;FALSE)"
With Selection.FormatConditions(2).Borders(xlLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.FormatConditions(2).Borders(xlRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.FormatConditions(2).Borders(xlTop)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
With Selection.FormatConditions(2).Borders(xlBottom)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
Range("A5:I5").Select
Selection.AutoFill Destination:=Range("A5:I200"), Type:=xlFillDefault
Range("A5:I200").Select
ActiveWindow.SmallScroll Down:=-213
Range("A6").Select
Selection.ClearContents
Range("A7").Select
Selection.ClearContents
Range("A8").Select
Selection.ClearContents
Range("A9").Select
Selection.ClearContents
Range("A10").Select
Selection.ClearContents
Range("A11").Select
Selection.ClearContents
Range("A14:A19").Select
Selection.ClearContents
Range("A13").Select
Selection.ClearContents
ActiveWindow.SmallScroll Down:=6
Range("A21:A28").Select
Selection.ClearContents
Range("B25").Select
ActiveWindow.SmallScroll Down:=9
Range("A30:A35").Select
Selection.ClearContents
Range("B31").Select
End Sub
 
Upvote 0
Theo mình thì bạn Civil muốn kẻ dòng các ô trong vùng có dữ liệu và giá trị ở ô đầu tiên của các dòng sẽ quyết định kiểu đường nét kẻ.
Nếu đúng như vậy thì code của bạn Civil có nhiều điểm sai.
Đây là đoạn code mình sửa lại theo ý trên:
Mã:
Public Sub KeDong_1()
'Worksheets("Don gia chi tiet").Activate
Dim rngData As Range
Dim tg1, tg2
tg1 = Timer
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Set rngData = ActiveSheet.UsedRange
For i = 1 To rngData.Rows.Count
    If rngData.Cells(i, 1).Value <> 0 Then
        Range(rngData.Cells(i, 1), rngData.Cells(i, 16)).Select
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
    Else
        Range(rngData.Cells(i, 1), rngData.Cells(i, 16)).Select
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .Weight = xlHairline
            .ColorIndex = xlAutomatic
        End With
    End If
Next i
tg2 = Timer
MsgBox "Thoi gian chay: " & tg2 - tg1
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
Bạn thử chạy và so sánh với đoạn code sau:
Mã:
Public Sub KeDong_2()
Dim tg1, tg2
tg1 = Timer
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim rngData As Range, subRange1 As Range, subRange2 As Range
Set rngData = ActiveSheet.UsedRange
For i = 1 To rngData.Rows.Count
    If rngData.Cells(i, 1).Value <> 0 Then
        If subRange1 Is Nothing Then
            Set subRange1 = Range(rngData.Cells(i, 1), rngData.Cells(i, 16))
        Else
            Set subRange1 = Union(subRange1, Range(rngData.Cells(i, 1), rngData.Cells(i, 16)))
        End If
    Else
        If subRange2 Is Nothing Then
            Set subRange2 = Range(rngData.Cells(i, 1), rngData.Cells(i, 16))
        Else
            Set subRange2 = Union(subRange2, Range(rngData.Cells(i, 1), rngData.Cells(i, 16)))
        End If
    End If
Next i
If Not subRange1 Is Nothing Then
    With subRange1.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With subRange1.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
End If
If Not subRange2 Is Nothing Then
    With subRange2.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlHairline
        .ColorIndex = xlAutomatic
    End With
    With subRange2.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlHairline
        .ColorIndex = xlAutomatic
    End With
End If
tg2 = Timer
MsgBox "Thoi gian chay: " & tg2 - tg1
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
(Code dài chưa chắc đã chạy chậm hơn, quan trọng là thuật toán!!!!)
 
Upvote 0
civil đã viết:
Em có hàm này, giúp việc kẻ dòng, kẻ bảng trong bảng dự toán chi tiết, nhưng thực hiện hơi chậm, Xin các cao thủ giúp cải tiến hàm này cho nó chạy nhanh hơn được không.

+ Oh, sao lại dùng hàm (Function) nhỉ, có trả về giá trị nào đâu???-> Dùng Sub

+ Lâu là do vòng For đây, bạn kẻ từng hàng, chưa tắt thuộc tính application.ScreenUpdating

+ Giải pháp là dùng Format conditional, bạn thử với sub này xem sao

PHP:
Sub KeBang()
 
application.ScreenUpdating =False 
 
Range(Cells(7, 1), Cells(ActiveSheet.UsedRange.Rows.Count,16)).Select
Selection.FormatConditions.Delete
 Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$A7<>0"
    With Selection.FormatConditions(1).Borders(xlTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
 
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=NOT($A9<>0)"
    With Selection.FormatConditions(2).Borders(xlTop)
        .LineStyle = xlContinuous
        .Weight = xlHairline
        .ColorIndex = xlAutomatic
    End With

application.ScreenUpdating =True
End Sub

Dĩ nhiên trước đây bạn đã phaỉo ket 1 số đường rồi,..
 
Upvote 0
Cái này kết hợp với công thức thì ngon lành... Vì khi ấy ta chỉ kẽ khung từ dữ liệu phía trước nó đến nó mà thôi, ko kẽ hết bãng... Và file này là 1 gợi ý... Hãy sữa lại cho phù hợp
Chú ý thêm vụ Intersect đễ loại bõ trường hợp nào cần kẽ, trường hợp nào thì ko và thêm cái nữa là nhập liệu thứ tự từ trên xuống dưới!
Mến
ANH TUẤN
 

File đính kèm

  • Kedong.zip
    8.7 KB · Đọc: 123
Upvote 0
Thêm 1 tham khảo, giảm phân nữa thời lượng đây

PHP:
Option Explicit
 Dim Timer_ As Double
 
 Sub kedong()
 Sheet4.Select:         Dim iJ As Long
Application.ScreenUpdating = False

Timer_ = Timer

For iJ = 7 To ActiveSheet.UsedRange.Rows.Count
If Cells(iJ, 1).Value <> 0 Then
Range(Cells(iJ, 1), Cells(iJ, 16)).Select
    With Selection.Borders(xlEdgeTop)
        .Weight = xlThin
    End With
Else
Range(Cells(iJ, 1), Cells(iJ, 16)).Select
    With Selection.Borders(xlEdgeTop)
        .Weight = xlHairline
    End With
End If
Next iJ
MsgBox Str(Timer - Timer_)
End Sub
Mã:
[B]Sub Goto2()[/B]  
  Dim Rng As Range, Rng1 As Range, Rng0 As Range, Clls As Range
    
    Application.ScreenUpdating = False
    Sheet4.Select:          Timer_ = Timer

    Range("A7").Select
    Selection.CurrentRegion.Columns(1).Select
    Set Rng = Union(Selection.SpecialCells(xlCellTypeConstants, 1), _
      Selection.SpecialCells(xlCellTypeFormulas, 1))
    For Each Clls In Rng
        If Clls.Value = 0 Then
            If Rng0 Is Nothing Then
                Set Rng0 = Clls.Resize(, 16)
            Else
                Set Rng0 = Union(Rng0, Clls.Resize(, 16))
            End If
        Else
            If Rng1 Is Nothing Then
                Set Rng1 = Clls.Resize(, 16)
            Else
                Set Rng1 = Union(Rng1, Clls.Resize(, 16))
            End If
        End If
    Next Clls
    Rng0.Select
    With Selection.Borders(xlEdgeTop)
        .Weight = xlThin
    End With
    Rng1.Select
    With Selection.Borders(xlEdgeTop)
        .Weight = xlHairline
    End With

    MsgBox Str(Timer - Timer_)
[B]End Sub[/B]
 
Upvote 0
Cám ơn các bác rất nhiều, em mừng quá, đang thử chạy code của các bác. hi hi
 
Chỉnh sửa lần cuối bởi điều hành viên:
Upvote 0
Code đơn giản và nhanh nhất là của bác này.
tigertiger đã viết:
+ Giải pháp là dùng Format conditional
PHP:
Sub KeBang()
application.ScreenUpdating =False 
Range(Cells(7, 1), Cells(ActiveSheet.UsedRange.Rows.Count,16)).Select
Selection.FormatConditions.Delete
 Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$A7<>0"
    With Selection.FormatConditions(1).Borders(xlTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=NOT($A9<>0)"
    With Selection.FormatConditions(2).Borders(xlTop)
        .LineStyle = xlContinuous
        .Weight = xlHairline
        .ColorIndex = xlAutomatic
    End With
application.ScreenUpdating =True
End Sub
Nhưng em thích dùng code này, vì nó có cái union em đang tìm hiểu cách dùng, hè hè
nvson đã viết:
Mã:
 [LEFT]Public Sub KeDong_2()
Dim tg1, tg2
tg1 = Timer
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim rngData As Range, subRange1 As Range, subRange2 As Range
Set rngData = ActiveSheet.UsedRange
For i = 1 To rngData.Rows.Count
    If rngData.Cells(i, 1).Value <> 0 Then
        If subRange1 Is Nothing Then
            Set subRange1 = Range(rngData.Cells(i, 1), rngData.Cells(i, 16))
        Else
            Set subRange1 = Union(subRange1, Range(rngData.Cells(i, 1), rngData.Cells(i, 16)))
        End If
    Else
        If subRange2 Is Nothing Then
            Set subRange2 = Range(rngData.Cells(i, 1), rngData.Cells(i, 16))
        Else
            Set subRange2 = Union(subRange2, Range(rngData.Cells(i, 1), rngData.Cells(i, 16)))
        End If
    End If
Next i
If Not subRange1 Is Nothing Then
    With subRange1.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With subRange1.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
End If
If Not subRange2 Is Nothing Then
    With subRange2.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlHairline
        .ColorIndex = xlAutomatic
    End With
    With subRange2.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlHairline
        .ColorIndex = xlAutomatic
    End With
End If
tg2 = Timer
MsgBox "Thoi gian chay: " & tg2 - tg1
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
[/LEFT]
 
Upvote 0
Web KT
Back
Top Bottom