Avatar of Jagwarman
Jagwarman

asked on 

Colour code range of cells

I have used the below code previously but need to make a slight amendment in my current project.

In this project I need to colour cells B through T whereas the code below only colours the cell containing the word:

Sub CondtionalFormats()
   
    Dim vCells As Range

    For Each vCells In Range("A1:A32768").Cells
        If Left(vCells.Text, 4) = "Book" Then
            vCells.Interior.ColorIndex = 43
            vCells.Borders.LineStyle = xlContinuous
            vCells.Borders.Weight = xlThin
        End If
    Next vCells
     
End Sub

Would appreciate some help.
Thanks
Microsoft Excel

Avatar of undefined
Last Comment
Steve
Avatar of Steve
Steve
Flag of United Kingdom of Great Britain and Northern Ireland image

You would like to colour words starting wit B to T?
Or columns B to T words staring Book?
Avatar of Jagwarman
Jagwarman

ASKER

Rows, where the word in column 'B' start with Book [ but I may want to change the word from Book to some other word later] and I need to colour from B to T

Thanks
Avatar of Steve
Steve
Flag of United Kingdom of Great Britain and Northern Ireland image

ok...

Sub CondtionalFormats()    
    Dim vCells As Range

    For Each vCells In Range("B1:T32768").Cells
        If Left(vCells.Text, 4) = "Book" Then
            vCells.Interior.ColorIndex = 43
            vCells.Borders.LineStyle = xlContinuous
            vCells.Borders.Weight = xlThin
        End If
    Next vCells
     
End Sub 

Open in new window


This will do B to T but between rows 1 to 32768
Can set the rows to a number or to let excel work it out, but up to you

You can get conditional formatting to do this for you, rather than VBA.
Would you like a demo of the Conditional Formatting method?
Avatar of Jagwarman
Jagwarman

ASKER

I could do Conditional formatting but there are numerous 'words' where I need to change row colours so prefer to use VBA.

Not sure why but as I mentioned originally the code only colours the cell[s] that contain the word it does not colour the whole row B through T.

Is it not possible?
Avatar of Jagwarman
Jagwarman

ASKER

Just thought ?

Only the one cell [i.e. the one in column 'B'] will have the word 'Book' but I want all of the cells in the row B through T to be highlighted.
Avatar of Steve
Steve
Flag of United Kingdom of Great Britain and Northern Ireland image

Try the following...
looking for cells in B1 to the last full cell in B which start "book"
then those found are formatted B to T

Sub CondtionalFormats()
    Dim vCells As Range

    For Each vCells In Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row).Cells
        If Left(vCells.Text, 4) = "Book" Then
            With Range(Cells(vCells.Row, "B"), Cells(vCells.Row, "T"))
                .Interior.ColorIndex = 43
                .Borders.LineStyle = xlContinuous
                .Borders.Weight = xlThin
            End With
        End If
    Next vCells
     
End Sub

Open in new window


Is the word starting book actually in A?
If yes try:

Sub CondtionalFormats()
    Dim vCells As Range

    For Each vCells In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row).Cells
        If Left(vCells.Text, 4) = "Book" Then
            With Range(Cells(vCells.Row, "B"), Cells(vCells.Row, "T"))
                .Interior.ColorIndex = 43
                .Borders.LineStyle = xlContinuous
                .Borders.Weight = xlThin
            End With
        End If
    Next vCells
     
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Steve
Steve
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Jagwarman
Jagwarman

ASKER

Bingo Thanks. Have a great weekend
Avatar of Jagwarman
Jagwarman

ASKER

Exactly what I was looking for.
Avatar of Steve
Steve
Flag of United Kingdom of Great Britain and Northern Ireland image

You have a great weekend too, enjoy :)
Microsoft Excel
Microsoft Excel

Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.

144K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo