bbalch

Board Regular
Joined
Feb 23, 2015
Messages
61
Hello everyone,

I am trying to create an IF statement using VBA to format the cells in a row (columns A:H) if certain text is found in column A.

The workbook contains account names in Column A. If the name in Column A = "Total Operating Income", "Total Expense", "Total Non-Operating Income", or "Total Income" I would like to apply the format below to the cells in A:H of that row. If the name in Column A does not contain one of the names listed no formatting changes need to be made. The account names in column A start in cell A6 and continue down anywhere from 10 - 150 rows (depending on how many accounts are used by the department).

Code:
Range("A:H").Select    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -0.249977111117893
        .PatternTintAndShade = 0
    End With
    Selection.Font.Bold = True

Any suggestions on how to incorporate an if statement to format cells based on the text contents found in column A?

Thanks in advance for your help.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Does this do what you are looking for?

Code:
Sub FormatColA()


Dim LastRow As Long
LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row


For Each C In Range("A6:A" & LastRow).Cells
C.Select
If C.Value = "Total Operating Income" Or C.Value = "Total Expense" Or C.Value = "Total Non-Operating Income" Or C.Value = "Total Income" Then
        C.Offset(0, 7).Select
        Range(Selection, Selection.End(xlToLeft)).Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -0.249977111117893
        .PatternTintAndShade = 0
    End With
    Else
    End If
    Next
End Sub
 
Upvote 0
No need to Select. I would also clear the formats at the beginning in case the data changes the next time the macro is run. You can also simplify the if by using Select Case.

Code:
Sub FormatColA()
Dim LastRow As Long, c As Range
LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
Range("A:H").ClearFormats
For Each c In Range("A6:A" & LastRow)
    Select Case c
        Case "Total Operating Income", "Total Expense", "Total Non-Operating Income"
            With c.Resize(, 7).Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .ThemeColor = xlThemeColorDark1
                .TintAndShade = -0.249977111117893
                .PatternTintAndShade = 0
            End With
    End Select
Next
End Sub
 
Last edited:
Upvote 0
Thanks for the advice Scott Huish!

I am new to this, but think it is fun to try to find answers to these problems. It is a great way to learn excel/VBA a lot better as well.

Thank you!
 
Upvote 0

Forum statistics

Threads
1,214,579
Messages
6,120,365
Members
448,956
Latest member
Adamsxl

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top