简体   繁体   中英

Cell border with a varying range

I am trying to put a border around individual cells as I move through a FOR LOOP where the number of loops is determined by how many columns have content in them and the number of columns will vary between sheets.

It appears that I can't individually put a border around a dynamic cell location, rather I have to specify a range or a particular cell. Is that correct?

Here is my code that I'm using to accomplish my goal...

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

I believe there is a way to accomplish the border specification but I can't use "Cells" to specify any borders. The interior color and font work as desired.

You need assign the property values for Border , just like you did it for Color and Bold

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

Also, another way is :

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

There is a difference between the Range.Borders property and the Border Object . Do not confuse the two.

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

I've cleaned up a few of the repetitious .Parent references with a With ... End With statement . Not only does this make for cleaner code but it runs faster as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM