Consulting

Results 1 to 7 of 7

Thread: Automate Conditional Formatting in Word

  1. #1

    Automate Conditional Formatting in Word

    Hi, I have a table in word and want to automate red, amber or green shading in column 8 for each row. This starts in row 5 of the table as the 4 rows above are merged for titles. I know I can do this in Excel but I want to use Word for this table. Can anyone help? Thank you.

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,218
    Location
    As this is your first post I'm going to give you a fish. However, the purpose of this forum is to "assist" you write your own code. Not write it for you. Working with tables that have merged cells can be very difficult. By putting the cursor in the first cells (i.e., row five, column 8) this may work:

    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    Dim lngIndex As Long: lngIndex = 1
      Do
        If lngIndex = 4 Then lngIndex = 1
        Select Case lngIndex
          Case 1
            Selection.Cells(1).Shading.BackgroundPatternColor = wdColorBlue
          Case 2
            Selection.Cells(1).Shading.BackgroundPatternColor = wdColorBrightGreen
          Case 3
            Selection.Cells(1).Shading.BackgroundPatternColor = wdColorLightYellow
        End Select
        lngIndex = lngIndex + 1
        'Escape clause:
        If Selection.Cells(1).Range.Rows(1).Range.End = Selection.Tables(1).Rows(Selection.Tables(1).Rows.Count).Range.End Then Exit Do
        'Next row.
        Selection.MoveRight Unit:=wdCell, Count:=Selection.Tables(1).Columns.Count
      Loop
    lbl_Exit:
      Exit Sub
    Err_Loop:
      Resume lbl_Exit
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    Hi Greg, that's very kind of you. I apologise, I didn't realise this was a site for people who knew code. That doesn't really apply to me. I will try a different site. I couldn't get the code to work. Kind regards.

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,218
    Location
    I'm not trying to run you off. I'm trying to tell you this isn't a "free" code writing service. We are here to help you find your solutions. That you should try to learn to do what you need to do with Word, just as you have with Excel.

    "I couldn't get the code to work." isn't very helpful.

    Can you attach a sanitized version of your document?
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    Quote Originally Posted by gmaxey View Post
    I'm not trying to run you off. I'm trying to tell you this isn't a "free" code writing service. We are here to help you find your solutions. That you should try to learn to do what you need to do with Word, just as you have with Excel.

    "I couldn't get the code to work." isn't very helpful.

    Can you attach a sanitized version of your document?
    Many thanks, Greg. I put that I couldn't get the code to work because I didn't want to be a fraud on the site and not be working to its terms of reference! I am a complete numpty with code in Word, never done it before. I get basic stuff in Excel and not too shabby but never attempted outside of this application or Access, and I am at a basic level!! Doing a course at work to improve but early days.
    Many thanks, I attach a sanitised version. Now column is 7 and not 8 and I apologise if that's not helped.
    Attached Files Attached Files

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,218
    Location
    Your table is more complex than you originally described. Try:

    Sub Test()
    Dim oCell As Cell
    Dim lngIndex As Long
    lngIndex = 1
    For Each oCell In ActiveDocument.Tables(1).Range.Cells
    If oCell.Range.Information(wdEndOfRangeColumnNumber) = 7 Then
    If lngIndex = 4 Then lngIndex = 1
    Select Case lngIndex
    Case 1: oCell.Shading.BackgroundPatternColor = wdColorBlue
    Case 2: oCell.Shading.BackgroundPatternColor = wdColorBrightGreen
    Case 3: oCell.Shading.BackgroundPatternColor = wdColorLightYellow
    End Select
    lngIndex = lngIndex + 1
    End If
    Next
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  7. #7
    Hi Greg, very many thanks for your reply with code. I will give this a go. Apologies for late reply, not been well. Kind regards.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •