Border around each cell in a range
📝 Blog Post: Adding Borders Around Each Cell in a Range - A Simple Solution! 🎨
Are you tired of generating a ton of code just to add borders around every cell in a particular range? We've all been there! Luckily, we have a simple and clear solution for you. In this blog post, we'll walk you through the common issues and provide an easy solution to this problem. Let's dive right in!
🤔 The Problem: Adding Borders Around Each Cell in a Range Our reader reached out to us with a common frustration. They needed to add borders around each cell in a range, but the recorded code was bloated and inefficient. Here's an example of the code they were struggling with:
Set DT = Sheets("DATA")
endRow = DT.Range("F" & Rows.Count).End(xlUp).Row
result = 3
For I = 2 To endRow
If DT.Cells(I, 6).Value = Range("B1").Value Then
Range("A" & result) = DT.Cells(I, 6).Value
Range("B" & result) = DT.Cells(I, 1).Value
Range("C" & result) = DT.Cells(I, 24).Value
Range("D" & result) = DT.Cells(I, 37).Value
Range("E" & result) = DT.Cells(I, 3).Value
Range("F" & result) = DT.Cells(I, 15).Value
Range("G" & result) = DT.Cells(I, 12).Value
Range("H" & result) = DT.Cells(I, 40).Value
Range("I" & result) = DT.Cells(I, 23).Value
result = result + 1
End If
Next I
💡 The Solution: Adding Borders Using VBA To add borders around each cell in a range efficiently, we can leverage the power of VBA. Here's a step-by-step solution:
Define the range you want to add borders to. In this case, it seems like we want to add borders around the cells with data in the "DATA" sheet.
Identify the end row of the range. By finding the last non-empty row in column F, we can determine the extent of our range.
Initialize a variable to keep track of the row where we'll start displaying the results. In this case, the variable
result
is set to 3.Iterate through each row in the range, starting from row 2. For each row, check if the value in column 6 (cell F) matches the value in cell B1. If it does, add borders to the cells.
Increment the
result
variable to move to the next row where the results will be displayed.
📌 Here's a simplified version of the code that adds borders to each cell in the specified range:
Sub AddBordersToRange()
Dim DT As Worksheet
Dim endRow As Long
Dim result As Long
Dim I As Long
Set DT = Sheets("DATA")
endRow = DT.Range("F" & Rows.Count).End(xlUp).Row
result = 3
For I = 2 To endRow
If DT.Cells(I, 6).Value = Range("B1").Value Then
DT.Range("A" & result & ":I" & result).Borders.LineStyle = xlContinuous
DT.Range("A" & result & ":I" & result).Borders.Color = RGB(0, 0, 0)
result = result + 1
End If
Next I
End Sub
🎉 TA-DA! You now have a much cleaner solution to add borders around each cell in your desired range. Give it a try and see the magic happen!
📢 Join the Conversation! We hope this blog post has provided a simple and clear solution to the common issue of adding borders around each cell in a range. Try out the code and let us know how it worked for you by leaving a comment below. We'd love to hear your thoughts and any additional insights or tricks you may have.
📣 Don't forget to share this post with your friends and colleagues who might find it helpful! Happy coding! 🚀