Use VBA to shade table row


"Excel VBA Help"

 


Play Friv Now - Play Friv Games
Go Back   The Code Cage Forums > Microsoft Topics > Microsoft Word Forum
Our Info Pages:-
The Code Cage - Microsoft Office help

Notices

Microsoft Word Forum Members, post questions in this forum if they are related to using Microsoft Word, this includes Document Properties, VBA Programming & Functionality etc.

New Reply
Thread Tools Rate Thread Display Modes
  #1  
Unread 8th February 2014, 08:02
sharky12345's Avatar
sharky12345 sharky12345 is offline
Junior Member
MS Office Version: MS Office 97, 2003, 2007,2010
MS Office Skill Level: Some VBA experience
 
Join Date: Oct 2013
Location: UK
Posts: 54
Posts Per Day: 0.04
My Top Tip Count: 0
Thanks to others: 0
Thanked 0 Times in 0 Posts
Reputation: 10 (4) sharky12345 is on a distinguished road
Default Use VBA to shade table row
     Word: VBA   


------ Register to get rid of these "In Post" ads! ------

Category: Word: VBA   

I'm using this to format a Table cell;

VBA CODE:
Sub Shading() 
    Dim oCell As Cell 
    Dim oTable As Table 
     
    Set oTable = ActiveDocument.Tables(1) 
     
    For Each oCell In oTable.Range.Cells 
        If InStr(1, oCell.Range.Text, "ME") > 0 Then 
            oCell.Range.Shading.BackgroundPatternColor = wdColorGray20 
            oCell.Range.Next.Shading.BackgroundPatternColor = wdColorGray20 
        End If 
    Next 
    For Each oCell In oTable.Range.Cells 
        If InStr(1, oCell.Range.Text, "REPLY") > 0 Then 
            oCell.Range.Shading.BackgroundPatternColor = wdColorWhite 
            oCell.Range.Next.Shading.BackgroundPatternColor = wdColorWhite 
        End If 
    Next 
     
End Sub 

This only formats the first cell in any row though, where my Tables has 2 columns - how can I amend it so that it formats that whole row, if the cell in column 1 complies with the criteria set?

The Code Cage Advertisment
Advertisement
  #2  
Unread 8th February 2014, 08:19
RoyUK's Avatar
RoyUK RoyUK is offline
Administrator
Solution Centre Professional
MS Office Version: MS Office 2003, 2007, 2010
MS Office Skill Level: Proficient with MS Office
 
Join Date: Nov 2008
Location: Derbyshire, UK
Posts: 2,706
Posts Per Day: 0.86
My Top Tip Count: 4
Thanks to others: 2
Thanked 195 Times in 193 Posts
Reputation: 3320 (69) RoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond repute
4 Top Tips
England
Default Re: Use VBA to shade table row

------ Register to get rid of these "In Post" ads! ------


Why? Tables have dozens of inbuilt formats
__________________
Hope that helps, RoyUK
For tips & examples visit free excel help
DatabaseForm
About me

  #3  
Unread 8th February 2014, 08:22
sharky12345's Avatar
sharky12345 sharky12345 is offline
Junior Member
MS Office Version: MS Office 97, 2003, 2007,2010
MS Office Skill Level: Some VBA experience
 
Join Date: Oct 2013
Location: UK
Posts: 54
Posts Per Day: 0.04
My Top Tip Count: 0
Thanks to others: 0
Thanked 0 Times in 0 Posts
Reputation: 10 (4) sharky12345 is on a distinguished road
Default Re: Use VBA to shade table row

------ Register to get rid of these "In Post" ads! ------


The document is a template which starts with 2 rows but the user adds a row using a button of a form,

VBA CODE:
ActiveDocument.Tables(1).Rows.Add 

Depending on the criteria the table is formatted.

If this can be done without VBA then I didn't know so.

The Code Cage Advertisment
Advertisement
  #4  
Unread 8th February 2014, 08:27
RoyUK's Avatar
RoyUK RoyUK is offline
Administrator
Solution Centre Professional
MS Office Version: MS Office 2003, 2007, 2010
MS Office Skill Level: Proficient with MS Office
 
Join Date: Nov 2008
Location: Derbyshire, UK
Posts: 2,706
Posts Per Day: 0.86
My Top Tip Count: 4
Thanks to others: 2
Thanked 195 Times in 193 Posts
Reputation: 3320 (69) RoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond repute
4 Top Tips
England
Default Re: Use VBA to shade table row

------ Register to get rid of these "In Post" ads! ------


Looking through your code I would think Conditional Formatting might be an option. Try attaching a file
__________________
Hope that helps, RoyUK
For tips & examples visit free excel help
DatabaseForm
About me

  #5  
Unread 8th February 2014, 08:30
sharky12345's Avatar
sharky12345 sharky12345 is offline
Junior Member
MS Office Version: MS Office 97, 2003, 2007,2010
MS Office Skill Level: Some VBA experience
 
Join Date: Oct 2013
Location: UK
Posts: 54
Posts Per Day: 0.04
My Top Tip Count: 0
Thanks to others: 0
Thanked 0 Times in 0 Posts
Reputation: 10 (4) sharky12345 is on a distinguished road
Default Re: Use VBA to shade table row

------ Register to get rid of these "In Post" ads! ------


Attaching the file is not possible as it is a restricted document - I could compile a sanitised file but it will take a bit of time.

I didn't think Word had the facility for Conditional Formatting?

  #6  
Unread 8th February 2014, 08:32
RoyUK's Avatar
RoyUK RoyUK is offline
Administrator
Solution Centre Professional
MS Office Version: MS Office 2003, 2007, 2010
MS Office Skill Level: Proficient with MS Office
 
Join Date: Nov 2008
Location: Derbyshire, UK
Posts: 2,706
Posts Per Day: 0.86
My Top Tip Count: 4
Thanks to others: 2
Thanked 195 Times in 193 Posts
Reputation: 3320 (69) RoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond repute
4 Top Tips
England
Default Re: Use VBA to shade table row

------ Register to get rid of these "In Post" ads! ------


Sorry, I didn't notice you were using word.
__________________
Hope that helps, RoyUK
For tips & examples visit free excel help
DatabaseForm
About me

The Code Cage Advertisment
Advertisement
  #7  
Unread 8th February 2014, 08:38
sharky12345's Avatar
sharky12345 sharky12345 is offline
Junior Member
MS Office Version: MS Office 97, 2003, 2007,2010
MS Office Skill Level: Some VBA experience
 
Join Date: Oct 2013
Location: UK
Posts: 54
Posts Per Day: 0.04
My Top Tip Count: 0
Thanks to others: 0
Thanked 0 Times in 0 Posts
Reputation: 10 (4) sharky12345 is on a distinguished road
Default Re: Use VBA to shade table row

------ Register to get rid of these "In Post" ads! ------


No worries, solved it anyway by using this;

VBA CODE:
oCell.Row.Shading.BackgroundPatternColor = wdColorGray20 

  #8  
Unread 9th February 2014, 05:38
macropod's Avatar
macropod macropod is offline
Solution Centre Professional
MS Office Version: MS Office 2000, 2003 & 2010
MS Office Skill Level: Proficient with MS Office
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 172
Posts Per Day: 0.07
My Top Tip Count: 0
Thanks to others: 0
Thanked 20 Times in 20 Posts
Reputation: 380 (11) macropod is just really nicemacropod is just really nicemacropod is just really nicemacropod is just really nice
Australia
Default Re: Use VBA to shade table row

------ Register to get rid of these "In Post" ads! ------


Quote:
Originally Posted by sharky12345 View Post

I'm using this to format a Table cell;
...
This only formats the first cell in any row though, where my Tables has 2 columns - how can I amend it so that it formats that whole row, if the cell in column 1 complies with the criteria set?

Actually, your code (which has an unnecessary extra loop and an ineffectual 'oCell.Range.Next' shades whatever cell the found text is in...

Try:
VBA CODE:
Sub Shading() 
    Dim oCell As Cell 
    Dim oTable As Table 
     
    Set oTable = ActiveDocument.Tables(1) 
     
    For Each oCell In oTable.Range.Cells 
        With oCell.Range 
            If InStr(1, .Text, "ME") > 0 Then 
                .Rows(1).Shading.BackgroundPatternColor = wdColorGray20 
            ElseIf InStr(1, .Text, "REPLY") > 0 Then 
                .Rows(1).Shading.BackgroundPatternColor = wdColorWhite 
            End If 
        End With 
    Next 
End Sub 
__________________
Cheers,
Paul Edstein
[MS MVP - Word]

  #9  
Unread 9th February 2014, 18:18
sharky12345's Avatar
sharky12345 sharky12345 is offline
Junior Member
MS Office Version: MS Office 97, 2003, 2007,2010
MS Office Skill Level: Some VBA experience
 
Join Date: Oct 2013
Location: UK
Posts: 54
Posts Per Day: 0.04
My Top Tip Count: 0
Thanks to others: 0
Thanked 0 Times in 0 Posts
Reputation: 10 (4) sharky12345 is on a distinguished road
Default Re: Use VBA to shade table row

------ Register to get rid of these "In Post" ads! ------


Thank you!

The Code Cage Advertisment
Advertisement
New Reply
The Code Cage at Facebookhe Code Cage at Twitter


Tags
row, shade, table, vba

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

The Code Cage Affilliates



http://www.thecodecage.com/forumz tested by McAfee Security http://www.thecodecage.com/forumz tested by McAfee Security

All times are GMT +1. The time now is 08:46.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Template-Modifications by TMS
No part of this board, functionality or creatives may be copied or reproduced either in part or full without the express permission of The Code Cage Team.
Copyright ©2008 - , Simon lloyd.
We are not associated with nor employed by Microsoft in any way, we simply provide resources!
All MS office icons are registered trademarks of the application they represent and are used with permission from Microsoft
UA-8135493-1
Cultural Forum | Study at Malaysian University