Professional Documents
Culture Documents
Press Alt + F11 to open the Visual Basic for Applications (VBA) editor in Excel.
Select the cells in which you want to apply the grid lines with hairpin lines.
This code will add hairpin grid lines (top and bottom borders) to the selected cells. Keep in mind
that this is a simple example, and you can modify the code to suit your specific needs, such as
adjusting the border styles, colors, and thicknesses.
Add to Notebook
Regenerate
1) create an Excel VBA code for creating grid lines with hairpin lines horizontally and normal lines
vertically in selected cells
Sub AddGridLinesWithHairpin()
If Selection.Cells.Count = 0 Then
Exit Sub
End If
' Set the selected range
With cell.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlHairline
End With
With cell.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlHairline
End With
With cell.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
Next cell
End Sub
2) create an Excel VBA code for auto fit columns and rows and aligning the contents to left and top
in selected cells
Sub AutoFitAndAlign()
If Selection.Cells.Count = 0 Then
Exit Sub
End If
cell.EntireColumn.AutoFit
cell.HorizontalAlignment = xlLeft
cell.VerticalAlignment = xlTop
Next cell
End Sub
Sub AlignCenterSelectedCells()
If Selection.Cells.Count = 0 Then
Exit Sub
End If
selectedRange.HorizontalAlignment = xlCenter
selectedRange.VerticalAlignment = xlCenter
End Sub