Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-14-2017, 01:05 PM
PMC11 PMC11 is offline Change Text Colour in Cell Based on Text in Same Cell Windows 7 32bit Change Text Colour in Cell Based on Text in Same Cell Office 2010 32bit
Novice
Change Text Colour in Cell Based on Text in Same Cell
 
Join Date: Nov 2017
Posts: 2
PMC11 is on a distinguished road
Default Change Text Colour in Cell Based on Text in Same Cell

Hi,

I am new to VBA so I am struggling with what seems to be quite a simple task.

I have a few lines of text in each cell in a word document. Each cell contains a category such "Science" or "Health" or one of several others. At the minute I'm actually just using a special character such as "*" or "@" for testing purposes.

I need the text colour of all text in the cell to change depending on which category is in the cell. So the text would be e.g. green for "Science" and red for "Health".



It seems that running a macro is quickest way of making these changes (there will be over 200 such cells in my final document and colouring manually is such a waste of time). Basically, I'm struggling with first changing the colour of all the text in the cell, and secondly how to make the macro search again if the first criteria is not met. I would like 1 macro that could complete colouring for the entire document, rather than having multiple macros for each colour I need.

If you could give me some examples of VBA I could work with that would be most helpful. I'm really struggling and any help you could give will save me and my team so much time.
Reply With Quote
  #2  
Old 11-14-2017, 09:15 PM
gmayor's Avatar
gmayor gmayor is offline Change Text Colour in Cell Based on Text in Same Cell Windows 10 Change Text Colour in Cell Based on Text in Same Cell Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Maybe something like the following. Note the search is case sensitive.

Code:
Sub Macro1()
Dim oTable As Table
Dim oCell As Range
Dim i As Long
    For Each oTable In ActiveDocument.Tables
        For i = 1 To oTable.Range.Cells.Count
            Select Case True
                Case InStr(1, oTable.Range.Cells(i).Range.Text, "Science") > 0
                    Set oCell = oTable.Range.Cells(i).Range
                    oCell.End = oCell.End - 1
                    oCell.Font.Color = wdColorGreen
                Case InStr(1, oTable.Range.Cells(i).Range.Text, "Health") > 0
                    Set oCell = oTable.Range.Cells(i).Range
                    oCell.End = oCell.End - 1
                    oCell.Font.Color = wdColorRed
                    'etc
            End Select
        Next i
    Next oTable
lbl_Exit:
    Set oTable = Nothing
    Set oCell = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Change Text Colour in Cell Based on Text in Same Cell count the number of text in a cell based off a different cells text Kubi Excel 4 08-24-2017 05:53 PM
VBA to immediately change the colour of a cell depending on the code placed in anothe Phil Payne Excel Programming 2 07-27-2013 11:04 PM
Change Text Colour in Cell Based on Text in Same Cell Quadrant colour based on cell value RoyLittle0 Excel 2 05-05-2013 12:50 AM
Change formula cell range based on cell value Scoth Excel 4 10-25-2012 07:51 AM
CHange colour of footer if a cell changes to red OTPM Excel 0 05-26-2011 07:15 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:01 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft