Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-24-2022, 06:09 AM
diwakaramit diwakaramit is offline Conditional formatting of a word table Windows 10 Conditional formatting of a word table Office 2016
Novice
Conditional formatting of a word table
 
Join Date: Mar 2022
Posts: 5
diwakaramit is on a distinguished road
Default Conditional formatting of a word table

Hello-I have some experience with VBA in excel however i am new to VBA in word. I am trying to perform conditional formatting on the whole row within a Table in word report that i am creating.
I have multiple tables within this word document however i need to perform conditional formatting on Table (4). Table(4) has 5 rows and 4 columns. I need to loop through starting from Row 2 and check if the cell value in column 3 is 5 and value in column 4 is A. If this condition is true for that particular row then this whole row should be colored Red.
I prepared the code below but keep getting "Compile Error: Method or data member not found" on Line 8

Line 1 Sub Demo()
Line 2 Dim Rng As Range
Line 3 Dim rw As Integer
Line 4 Set Rng = ActiveDocument.Content
Line 5 With ActiveDocument.Tables(4)
Line 6 For rw = 2 To 5


Line 7 If .Cell(rw, 4).Range.Text = "5" And .Cell(rw, 5).Range.Text = "A" Then
Line 8 .Range.Cell(rw).Shading.BackgroundPatternColorInde x = wdDarkRed
Line 9 End If
Line 10 Next
Line 11 End With
Line 12 End Sub


Any help with this VBA code will be really helpful.
Any help with this VBA code will be really helpful.
Attached Files
File Type: docm RBI-REPORT-3.docm (35.4 KB, 6 views)
Reply With Quote
  #2  
Old 03-24-2022, 05:07 PM
Guessed's Avatar
Guessed Guessed is online now Conditional formatting of a word table Windows 10 Conditional formatting of a word table Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,967
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

You were almost there.
I changed to Selection.Tables to make it easier to test for me so you can ignore that.
Two problems with your code is that the range.text includes more than just the text you can see so I changed this to just sample the first character in the range. Second problem is how to colour the entire row (.Cell needs a row AND column reference).
Code:
Sub Demo()
  Dim rw As Integer
  With Selection.Tables(1)
    For rw = 2 To 5
      If .Cell(rw, 4).Range.Text Like "5*" And .Cell(rw, 5).Range.Text Like "A*" Then
        .Rows(rw).Shading.BackgroundPatternColorIndex = wdDarkRed
      End If
    Next
  End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 03-25-2022, 04:39 AM
diwakaramit diwakaramit is offline Conditional formatting of a word table Windows 10 Conditional formatting of a word table Office 2016
Novice
Conditional formatting of a word table
 
Join Date: Mar 2022
Posts: 5
diwakaramit is on a distinguished road
Default

This worked perfectly!

One quick question. How would i refer to whole value of the cell, for example in excel i would have used Cells (2,3) or Range ("B3"), is there a similar function available in word vba?
Reply With Quote
  #4  
Old 03-27-2022, 03:52 AM
Guessed's Avatar
Guessed Guessed is online now Conditional formatting of a word table Windows 10 Conditional formatting of a word table Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,967
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

You really don't want the entire contents of the cell. The entire contents of a table cell is found by looking at the text and paragraph marks that you can see and then adding two more invisible characters - a paragraph character and an end-of-cell character. I normally use a function to strip these out along with trailing paragraph marks that people tend to put in because their quality control is poor.
Code:
Public Function CellText(aCell As cell) As String
  Dim str As String
  With aCell
    str = .Range.Text
    str = Left(str, Len(str) - 2)
    While Right(str, 1) = Chr(13)
      str = Left(str, Len(str) - 1)
    Wend
    CellText = Trim(str)
  End With
End Function
You would use this in the earlier code's If line like this
Code:
If CellText(.Cell(rw, 4) = "5" And CellText(.Cell(rw, 5)) = "A" Then
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 03-27-2022, 09:51 AM
diwakaramit diwakaramit is offline Conditional formatting of a word table Windows 10 Conditional formatting of a word table Office 2016
Novice
Conditional formatting of a word table
 
Join Date: Mar 2022
Posts: 5
diwakaramit is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
You really don't want the entire contents of the cell. The entire contents of a table cell is found by looking at the text and paragraph marks that you can see and then adding two more invisible characters - a paragraph character and an end-of-cell character. I normally use a function to strip these out along with trailing paragraph marks that people tend to put in because their quality control is poor.
Code:
Public Function CellText(aCell As cell) As String
  Dim str As String
  With aCell
    str = .Range.Text
    str = Left(str, Len(str) - 2)
    While Right(str, 1) = Chr(13)
      str = Left(str, Len(str) - 1)
    Wend
    CellText = Trim(str)
  End With
End Function
You would use this in the earlier code's If line like this
Code:
If CellText(.Cell(rw, 4) = "5" And CellText(.Cell(rw, 5)) = "A" Then

Thank you so much , it was good to know that there are some invisible characters that you should be aware of .

Your recommendation worked very well with my code . Thank you
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to run colour conditional formatting in just column, not whole table KateWord Word VBA 2 09-12-2019 05:30 AM
Must use VBA to achieve conditional formatting along with expanding table columns? tinfanide Excel 0 06-10-2015 12:25 AM
Conditional formatting of a word table Conditional Formatting on Word (Dropdown List) daviieejay Word VBA 21 12-07-2014 11:08 PM
Conditional formatting of a word table Conditional Formatting not expanding along with the table expanded tinfanide Excel 3 10-07-2014 10:00 AM
Conditional Formatting: How to dynamically apply to an expanding table? tinfanide Excel 0 10-18-2013 06:34 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:57 PM.


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