S
StargateFanFromWork
I need to apply a border style to the top of 2 cells in columns A and B
determined by the cell that is active. i.e., if I need to apply the border
to the tops of A18 and B18, for example, would like to just go to either
cell and click on the button to do this without selecting.
To do this manually and with prior selecting, the recorded macro works and
this is the result:
****************************************************************************
********************************
Sub APPLY_ColouredTopBorder()
'
ActiveSheet.Unprotect 'place at the beginning of the code
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlDash
.Weight = xlMedium
.ColorIndex = 3
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
ActiveSheet.Protect ' place at end of code
End Sub
****************************************************************************
********************************
To revert back to the regular formatting of that border for selected cells,
the recorded macro comes out like this:
****************************************************************************
********************************
Sub REMOVE_ColouredTopBorder()
'
ActiveSheet.Unprotect 'place at the beginning of the code
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
ActiveSheet.Protect ' place at end of code
End Sub
****************************************************************************
********************************
I'm hoping that there is a perhaps better way of coding this that will do
the same thing, but that also applies/removes the border style without
having to select the target cells? TIA. D
determined by the cell that is active. i.e., if I need to apply the border
to the tops of A18 and B18, for example, would like to just go to either
cell and click on the button to do this without selecting.
To do this manually and with prior selecting, the recorded macro works and
this is the result:
****************************************************************************
********************************
Sub APPLY_ColouredTopBorder()
'
ActiveSheet.Unprotect 'place at the beginning of the code
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlDash
.Weight = xlMedium
.ColorIndex = 3
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
ActiveSheet.Protect ' place at end of code
End Sub
****************************************************************************
********************************
To revert back to the regular formatting of that border for selected cells,
the recorded macro comes out like this:
****************************************************************************
********************************
Sub REMOVE_ColouredTopBorder()
'
ActiveSheet.Unprotect 'place at the beginning of the code
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
ActiveSheet.Protect ' place at end of code
End Sub
****************************************************************************
********************************
I'm hoping that there is a perhaps better way of coding this that will do
the same thing, but that also applies/removes the border style without
having to select the target cells? TIA. D