#  Other Applications & Softwares  > Word Formatting & General >  >  Conditional Formatting in ms Word for specific cells in ms Word table using VBA code??

## Carat

Hi,

I am new to VBA programming and I'm sure that this is where the solution lies to my current problem...

I am using the "Mail Merge" option in ms Word to bring in information from a table created in Excel. Information such as Name, Surname etc. but other information is also required.

The imported information must be presented in the form of a table in Word (but cannot be an Excel table). I am looking for a VBA code/answer to this.

The issue lies in the fact that the letter "Y" denotes to a particular colour in a particular cell. Conditional formatting isn't available in ms Word.

The following photos hopefully explain my problem better. *I need the word document to resemble the colour as well as the letter*.

Many thanks!

Excel Table:
Excel_photo.png

Word Table:
Word_photo.png

----------


## Carat

I think I'm getting there - but what's wrong with the following?

Public Sub Coch()

Dim tbl As Table

    If ActiveDocument.Tables(1).Cell(4, 3).ID = y Then
        ActiveDocument.Tables(1).Cell(4, 3).Shading.BackgroundPatternColor = wdColorRed

Else
    If Not ActiveDocument.Tables(1).Cell(4, 3).ID = y Then
        ActiveDocument.Tables(1).Cell(4, 3).Shading.BackgroundPatternColor = wdWhite

    End If
    End If


End Sub


If any other letter is in Cell (4,3) the cell is *still* highlighted red. I want the cell coloured white if empty.

Thanks.

----------


## macropod

You can do this as part of the mailmerge itself, with no need for post-merge VBA processing.

Although Word doesn’t have tools for the kind of conditional cell formatting one finds in Excel, the effect can be simulated via field coding that combines tab-stops and manual line breaks inside an IF test that conditionally outputs the various strings. Such a field might be coded like:
{IF«myData»= "Y" {*Q*UOTE "^t^t^l
^t«myData»^t^l
^t^t" \* Charformat}}
where ^t represents a TAB, ^l represents a manual line break and {*Q* is the character you format with the required background shading (and any other desired font attributes).

The foregoing construction could be used to output the 'Y', horizontally and vertically centred in the cell. The centring is achieved by using centred tab-stops, plus right-aligned tab-stops to extend each line to the cell border. An empty line above and below the output string is also inserted. Obviously, the cell would need a fixed width and you would also need to specify a 0-width internal border. Variations on the theme could be used to accommodate other data and cell formats. Via multiple IF tests, you could have as many as 16 conditional formats - one for each highlight option - plus however many different font attributes you might desire.

How's that for flexibility - and not a line of VBA code in sight!

*Note:* The field brace pairs (i.e. '{ }') for the above example are created in the document itself, via Ctrl-F9 (Cmd-F9 on a Mac); you can't simply type them or copy & paste them from this message. Nor is it practicable to add them via any of the standard Word dialogues. Likewise, you can't type or copy & paste the chevrons (i.e. '« »') - they're part of the actual mergefields, which you can insert from the mailmerge toolbar. The spaces represented in the field construction are all required.

----------

