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

StackOverflow https://stackoverflow.com/questions/21245104

  •  30-09-2022
  •  | 
  •  

Question

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

Was it helpful?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top