2015-10-14 157 views
0

我需要更改下面的宏代碼插入的行(或特定行中的單元格A到H)的顏色。掃描Col H的if語句是顏色變化需要發生的地方。excel vba插入行並更改顏色

Sub SolidWorks() 

Application.ScreenUpdating = False 

Range("A100000").End(xlUp).Activate 
Range("N1") = ActiveCell.Row 

For lrow = Cells(Cells.Rows.Count, "B").End(xlUp).Row To 1 Step -1 
If Cells(lrow, "B") = 0 Then 
      Rows(lrow).EntireRow.Delete 
End If 
Next lrow 

For lrow = Cells(Cells.Rows.Count, "H").End(xlUp).Row To 1 Step -1 
    If Cells(lrow, "H") <> Cells(lrow - IIf(lrow = 1, 0, 1), "H") Then 
      Rows(lrow).EntireRow.Insert '<---- insert green row, instead of just a blank row 
'.Color = 5287936 
'    With Selection.Interior 
'    .Pattern = xlSolid 
'    .PatternColorIndex = xlAutomatic 
'    .Color = 5287936 
'    .TintAndShade = 0 
'    .PatternTintAndShade = 0 
'    End With 
    End If 
Next lrow 

For lrow = Cells(Cells.Rows.Count, "G").End(xlUp).Row To 1 Step -1 
    If Cells(lrow, "G") <> Cells(lrow - IIf(lrow = 1, 0, 1), "G") Then 
      Rows(lrow).EntireRow.Insert 
    End If 
Next lrow 

Range("A1").Select 

Application.ScreenUpdating = True 

End Sub 
+0

進入宏錄製和手動更改行的屬性。然後獲取這些知識並根據需要將代碼插入到VBA中。 –

回答

1

你明白了沒有,你只需要後 Rows(lRow).EntireRow.Insert

一條線,那就是Range("A" & lRow & ":H" & lRow).Interior.Color = 5287936

+0

我知道我一定要接近,但是我幾乎很尷尬。感謝斯科特 –