Ask Your Question
3

What is the way to implement the Worksheets.Cells format within a for loop?

asked 2023-01-25 11:00:00 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-07-08 05:00:00 +0000

lalupa gravatar image

Assuming you want to format cells in each row of a worksheet using a For loop, you can use the following code:

For i = 1 To numRows
    With Worksheets("Sheet1").Cells(i, 1).Resize(1, numCols)
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .Font.Bold = True
        .Interior.ColorIndex = 15
        .Borders.LineStyle = xlContinuous
        .Borders.Weight = xlThin
    End With
Next i

In this code, numRows and numCols are the number of rows and columns in your worksheet, respectively. The With statement applies the formatting to the cells in each row, starting from column 1 (Cells(i, 1)) and spanning numCols columns (Resize(1, numCols)). You can modify the formatting properties within the With statement to suit your needs.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-01-25 11:00:00 +0000

Seen: 18 times

Last updated: Jul 08 '22