2016-07-15 145 views
1

我試圖在每個單元格周圍放置一個邊框,因爲我在FOR LOOP中移動,其中循環的數量由多少個列中包含內容確定,並且列的數量因片材而異。不同範圍的單元格邊框

看來我不能單獨在動態單元格位置周圍放置邊框,而必須指定一個範圍或特定的單元格。那是對的嗎?

這裏是我的代碼,我使用實現我的目標......

Sub Sheet_Formatting() 

    For Col_Count = 1 To Col_Count_Active_Sheet 

    ThisWorkbook.Worksheets(Active_Sheet).Cells(1, Col_Count).Interior.Color = RGB(100, 100, 100) ' sets to the color ??? 
    ThisWorkbook.Worksheets(Active_Sheet).Cells(1, Col_Count).Borders.LineStyle.xlContinuous  ' sets to the linestyle 
    ThisWorkbook.Worksheets(Active_Sheet).Cells(1, Col_Count).Borders.Weight.xlThick    ' sets to the border thickness 
    ThisWorkbook.Worksheets(Active_Sheet).Cells(1, Col_Count).Font.Bold = True      ' sets to the text format to bold 

    Next Col_Count 
End Sub 

我相信有辦法實現邊境規範,但我不能使用「單元格格式」指定任何邊界。內部顏色和字體按需要工作。

回答

1

還有就是之間的差異Range.Borders財產和Border Object。不要混淆兩者。

Sub Sheet_Formatting() 

    For Col_Count = 1 To Col_Count_Active_Sheet 

     With ThisWorkbook.Worksheets(Active_Sheet).Cells(1, col_Count) 
      .Interior.Color = RGB(100, 100, 100) ' sets to the color ??? 
      .Borders.LineStyle = xlContinuous  ' sets to the linestyle 
      .Borders.Weight = xlThick    ' sets to the border thickness 
      .Font.Bold = True      ' sets to the text format to bold 
     End With 

    Next Col_Count 

End Sub 

我已經清理了一些有With ... End With statement的重複.Parent引用。這不僅使代碼更簡潔,而且運行速度也更快。

2

你需要指定邊境的屬性值,就像你這樣做是爲了ColorBold

Sub Sheet_Formatting() 



    For Col_Count = 1 To Col_Count_Active_Sheet 

    ThisWorkbook.Worksheets(Active_Sheet).Cells(1, Col_Count).Interior.Color = RGB(100, 100, 100) ' sets to the color ??? 
    ThisWorkbook.Worksheets(Active_Sheet).Cells(1, Col_Count).Borders.LineStyle = xlContinuous  ' sets to the linestyle 
    ThisWorkbook.Worksheets(Active_Sheet).Cells(1, Col_Count).Borders.Weight = xlThick    ' sets to the border thickness 
    ThisWorkbook.Worksheets(Active_Sheet).Cells(1, Col_Count).Font.Bold = True      ' sets to the text format to bold 

    Next Col_Count 
End Sub 

此外,另一種方式是:

ThisWorkbook.Worksheets(Active_Sheet).Cells(1, Col_Count).BorderAround (xlThin)

+0

你是什麼意思爲邊界添加屬性值?我認爲這就是xlContinuous和xlThick的目的? – ViggyTheBadMonkey

+0

您正在使用'.Borders.LineStyle.xlContinuous'。它不是一項任務。這是賦值'.Borders.LineStyle = xlContinuous'另外,當你的查詢被解析時,總是將它標記爲回答/ upvote。 – cyboashu