Search code examples
excelvbaloopsitalic

Find Italics in a named range excel 2010 vba then add values to a variable and output to form control


I am trying to search each cell individually in a named range and find each cell that has italic font. If the font is italic then I would like to add that cell's number to a variable so I can get a total.

Any help is much appreciated.

Justin


Solution

  • You are looking for the .Font.Italic property. Test the following. Modify accordingly.

    Sub AddItalicizedNums()
        Dim TargetRng As Range, Cell As Range
        Dim SumOfItalics
        Set TargetRng = Range("OneToTwenty")
        SumOfItalics = 0
        For Each Cell In TargetRng
            If Cell.Font.Italic = True Then
                SumOfItalics = SumOfItalics + Cell.Value
            End If
        Next Cell
        MsgBox SumOfItalics
    End Sub
    

    Screenshot:

    enter image description here

    My named range is OneToTwenty and I italicized (and boldfaced) all even numbers in it. The sum is showing correctly, as shown in the message box.

    Let us know if this helps.