2010-04-05 25 views
1

說我想要做的Excel的SS像這樣的第一行:Excel 2003中 - VBA通​​過每一個細胞都在一行中提供的屬性循環/格式化

    .Rows("1:1").Select 
        With Selection.Borders(xlEdgeLeft) 
         .LineStyle = xlContinuous 
         .Weight = xlMedium 
         .ColorIndex = xlAutomatic 
        End With 
        With Selection.Borders(xlEdgeTop) 
         .LineStyle = xlContinuous 
         .Weight = xlMedium 
         .ColorIndex = xlAutomatic 
        End With 
        With Selection.Borders(xlEdgeBottom) 
         .LineStyle = xlContinuous 
         .Weight = xlMedium 
         .ColorIndex = xlAutomatic 
        End With 
        With Selection.Borders(xlEdgeRight) 
         .LineStyle = xlContinuous 
         .Weight = xlMedium 
         .ColorIndex = xlAutomatic 
        End With 

只是我想每個人的細胞有輪廓,不是全部選擇。我怎麼能說第1行的每個單元格,做上述想法

謝謝!

回答

1

這很簡單:

Dim c As Range 
For Each c In ActiveSheet.Range("1:1") 
    c.Select 
    With Selection.Borders(xlEdgeLeft) 
     .LineStyle = xlContinuous 
     .Weight = xlMedium 
     .ColorIndex = xlAutomatic 
    End With 
Next 

但是這可能需要很長的時間,所以設定Application.ScreenUpdating到在你開始之前虛假!

+0

非常感謝! – Justin 2010-04-05 20:54:18

2

還設置xlInsideHorizontal

錄製宏,結果是

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 
Selection.Borders(xlInsideVertical).LineStyle = xlNone 

'I think this is what you were missing 
With Selection.Borders(xlInsideHorizontal) 
    .LineStyle = xlContinuous 
    .ColorIndex = xlAutomatic 
    .TintAndShade = 0 
    .Weight = xlThin 
End With 
+0

應該是xlInsideVertical,因爲它在一行上,對嗎? (我認爲) – guitarthrower 2010-04-05 17:28:02

+0

我測試了這個使用記錄宏,所以這是代碼生成X) – 2010-04-05 17:33:33