Excel VBA Get Underline (Sample Code + Download)
In this article I’ve provided an example on how you can get the current underline style used in a cell. When the user presses the Run button the program loops through the cells in row 1. In row 2 the type of underline used is printed.
There are 4 different types of underlines:
- Single
- Double
- Single Accounting
- Double Accounting
For more information about the different underline types and their associate VBA code please see Excel VBA, Underline Font Style.
You can download the code and file related to this article here.
In the figure below there is some text written in row 1:
In the figure below the user has changed the underline style for the cells in row 1. After pressing the Run button:
The program uses a Button_Click event handler. The event handler executes when the user presses the run button:
'executes when the user presses the run button
Private Sub btnRun_Click()
Dim i As Integer
'loops through the cells in row 1
For i = 1 To 5
'checks if there is no underline style
If Range(Cells(1, i), Cells(1, i)).Font.Underline _
= xlNone Then
Cells(2, i) = "None"
'checks if the Single underline style is used
ElseIf Range(Cells(1, i), Cells(1, i)).Font.Underline _
= xlSingle Then
Cells(2, i) = "Single"
'checks if the Double underline style is used
ElseIf Range(Cells(1, i), Cells(1, i)).Font.Underline _
= xlDouble Then
Cells(2, i) = "Double"
'checks if the Single Accounting underline style is used
ElseIf Range(Cells(1, i), Cells(1, i)).Font.Underline _
= xlSingleAccounting Then
Cells(2, i) = "Single Accounting"
'checks if the Double Accounting underline style is used
ElseIf Range(Cells(1, i), Cells(1, i)).Font.Underline _
= xlDoubleAccounting Then
Cells(2, i) = "Double Accounting"
End If
Next i
End Sub
The event handler loops through the cells in row 1:
For i = 1 To 5
...
Next i
The If statement below checks if the cell is without the underline font style:
If Range(Cells(1, i), Cells(1, i)).Font.Underline _
= xlNone Then
Cells(2, i) = "None"
The If statement below checks if the cell is using the Single underline font style:
ElseIf Range(Cells(1, i), Cells(1, i)).Font.Underline _
= xlSingle Then
Cells(2, i) = "Single"
The If statement below checks if the cell is using the Double underline font style:
ElseIf Range(Cells(1, i), Cells(1, i)).Font.Underline _
= xlDouble Then
Cells(2, i) = "Double"
The If statement below checks if the cell is using the Single Accounting underline font style:
ElseIf Range(Cells(1, i), Cells(1, i)).Font.Underline _
= xlSingleAccounting Then
Cells(2, i) = "Single Accounting"
The If statement below checks if the cell is using the Double Accounting underline font style:
ElseIf Range(Cells(1, i), Cells(1, i)).Font.Underline _
= xlDoubleAccounting Then
Cells(2, i) = "Double Accounting"
You can download the code and file related to this article here.
See also:
- VBA Excel Font Formatting
- Excel VBA Set Underline (Sample Code + Download)
- Excel VBA, Formatting Cells and Ranges Using the Macro Recorder
If you need assistance with your code, or you are looking for a VBA programmer to hire feel free to contact me. Also please visit my website www.software-solutions-online.com