2016-03-17 77 views
1

我有一個工作宏,每6行添加一個空行,它工作的很棒!我有一個問題,雖然,這是很簡單,但它不工作:S 我希望這些顏色新行顏色:.TintAndShade = -0.249977111117893和H.如何在宏中着色行

只有細胞列A之間我不知道在哪裏在此代碼中添加此代碼。有人能幫我嗎?

Dim NumRowsToInsert As Long 
Dim RowIncrement As Long 
Dim ws As Excel.Worksheet 
Dim LastRow As Long 
Dim LastEvenlyDivisibleRow 
Dim i As Long 

NumRowsToInsert = 1 
RowIncrement = 6 
Set ws = ActiveSheet 
With ws 
    LastRow = .Range("A" & .Rows.Count).End(xlUp).Row 
    LastEvenlyDivisibleRow = Int(LastRow/RowIncrement) * RowIncrement 
    If LastEvenlyDivisibleRow = 0 Then 
     Exit Sub 
    End If 
    Application.ScreenUpdating = False 
    For i = LastEvenlyDivisibleRow To 1 Step -RowIncrement 
     .Range(i & ":" & i + (NumRowsToInsert - 1)).Insert xlShiftDown 
    Next i 
End With 
Application.ScreenUpdating = True 
End Sub 

回答

1

添加新行後添加它。

For i = LastEvenlyDivisibleRow To 1 Step -RowIncrement 
    .Range(i & ":" & i + (NumRowsToInsert - 1)).Insert xlShiftDown 
    .Range("A" & i & ":H" & i + (NumRowsToInsert - 1)).Interior.TintAndShade = -0.249977111117893 
Next i 

注意這個特殊的TintAndShade出來作爲無彩色的我...

+0

非常感謝!這工作。非常感激! @vacip – BMRobin

0

插入和排它沒有理想和慢速設置色彩行。更好的方法是將它們全部插入一次:

Sub InsertRowsAndSetColor() 

    Const StartRow = 1 
    Const NumRowsToInsert = 1 
    Const RowIncrement = 6 

    Dim ws As Worksheet, rg As Range, rowCount As Long, r As Long 
    Set ws = ActiveSheet 
    rowCount = ws.UsedRange.row + ws.UsedRange.Rows.count 

    ' exit if not enough rows 
    If rowCount <= StartRow + RowIncrement Then Exit Sub 

    ' collect all the rows requireing an insertion 
    Set rg = ws.Rows(StartRow + RowIncrement) 
    For r = StartRow + RowIncrement To rowCount Step RowIncrement 
    If NumRowsToInsert > 1 Then 
     Set rg = Union(rg, ws.Range(ws.Rows(r), ws.Rows(r + NumRowsToInsert - 1))) 
    Else 
     Set rg = Union(rg, ws.Rows(r)) 
    End If 
    Next 

    ' insert the rows 
    rg.Insert xlShiftDown 

    ' set the interior for the new rows within A:H 
    With Intersect(rg.offset(-NumRowsToInsert), ws.Columns("A:H")).Interior 
    .TintAndShade = -0.249977111117893 
    End With 

End Sub 
+0

我喜歡這個概念,但是如果關閉屏幕更新和計算,你確定在運行時有可測量的差異嗎? – vacip

+0

非常感謝!這工作。非常感激! @florentbr – BMRobin