Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-28-2015, 02:54 PM
Snaybot Snaybot is offline Inserting a Merged Row after a certain text is found in a table Windows 7 64bit Inserting a Merged Row after a certain text is found in a table Office 2013
Novice
Inserting a Merged Row after a certain text is found in a table
 
Join Date: Sep 2015
Posts: 22
Snaybot is on a distinguished road
Lightbulb

I have a 6 Column Table



With the First Column a automatic numbering format

So the first column farthest to the left is 4.1.1, 4.1.2, 4.1.3, ect ect

And the second column (Second from the left) is formatted such as "Ensure SXXXXX" (Where X is a number 1-9999)

I want to write a macro where if there is a value of "Ensure S" then add a Grey colored Merged Row spanning all 6 columns with the Value of "Step SXXXXX"


Is this possible?

Image attached

This is what I want the macro to do... Im ok at excel vba but I have no idea what to do here
Attached Images
File Type: jpg MOF1.jpg (94.4 KB, 18 views)
Reply With Quote
  #2  
Old 09-30-2015, 01:46 AM
macropod's Avatar
macropod macropod is offline Inserting a Merged Row after a certain text is found in a table Windows 7 64bit Inserting a Merged Row after a certain text is found in a table Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try something based on:
Code:
Sub Demo()
Dim i As Long, StrTxt As String
With ActiveDocument.Tables(1)
  For i = .Rows.Count To 1 Step -1
    If InStr(.Cell(i, 2).Range.Text, "Ensure S") = 1 Then
      StrTxt = Split(.Cell(i, 2).Range.Text, " ")(1)
      .Rows.Add BeforeRow:=.Rows(i)
      With .Rows(i)
        .Cells.Merge
        .Shading.BackgroundPatternColor = -603930625
        .Range.Style = wdStyleNormal
        .Range.Text = "Step " & StrTxt
      End With
    End If
  Next
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 09-30-2015, 09:57 AM
Snaybot Snaybot is offline Inserting a Merged Row after a certain text is found in a table Windows 7 64bit Inserting a Merged Row after a certain text is found in a table Office 2013
Novice
Inserting a Merged Row after a certain text is found in a table
 
Join Date: Sep 2015
Posts: 22
Snaybot is on a distinguished road
Default

Amazing thank you so much!


But is there anyway to say that if there already is a merged grey row above the "Ensure SXXXXXX" then delete that Merged grey row and replace it OR do not do anything to that "Ensure SXXXXXX"



The code works great thanks again!
Reply With Quote
  #4  
Old 09-30-2015, 01:41 PM
macropod's Avatar
macropod macropod is offline Inserting a Merged Row after a certain text is found in a table Windows 7 64bit Inserting a Merged Row after a certain text is found in a table Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by Snaybot View Post
But is there anyway to say that if there already is a merged grey row above the "Ensure SXXXXXX" then delete that Merged grey row and replace it OR do not do anything to that "Ensure SXXXXXX"
Well, yes, but how is the code to know whether it should be replaced or left alone?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 09-30-2015, 02:08 PM
Snaybot Snaybot is offline Inserting a Merged Row after a certain text is found in a table Windows 7 64bit Inserting a Merged Row after a certain text is found in a table Office 2013
Novice
Inserting a Merged Row after a certain text is found in a table
 
Join Date: Sep 2015
Posts: 22
Snaybot is on a distinguished road
Default

Oh sorry,

My question may of been confusing


Is there a way to run the macro multiple times and only have 1 grey merged row per "Ensure SXXXXXX"

It doesnt really matter if it is replaced or left alone! What ever is Easier!

Thanks a bunch!
Reply With Quote
  #6  
Old 09-30-2015, 02:16 PM
macropod's Avatar
macropod macropod is offline Inserting a Merged Row after a certain text is found in a table Windows 7 64bit Inserting a Merged Row after a certain text is found in a table Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The simplest approach would be to test whether the rows preceding the 'Ensure SXXXXX' content have more than one cell and, if not, skip over them, this:
Code:
Sub Demo()
Dim i As Long, StrTxt As String
With ActiveDocument.Tables(1)
  For i = .Rows.Count To 2 Step -1
    If .Rows(i - 1).Cells.Count > 1 Then
      If InStr(.Cell(i, 2).Range.Text, "Ensure S") = 1 Then
        StrTxt = Split(.Cell(i, 2).Range.Text, " ")(1)
        .Rows.Add BeforeRow:=.Rows(i)
        With .Rows(i)
          .Cells.Merge
          .Shading.BackgroundPatternColor = -603930625
          .Range.Style = wdStyleNormal
          .Range.Text = "Step " & StrTxt
        End With
      End If
    Else
      i = i - 1
    End If
  Next
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 09-30-2015, 02:23 PM
Snaybot Snaybot is offline Inserting a Merged Row after a certain text is found in a table Windows 7 64bit Inserting a Merged Row after a certain text is found in a table Office 2013
Novice
Inserting a Merged Row after a certain text is found in a table
 
Join Date: Sep 2015
Posts: 22
Snaybot is on a distinguished road
Default

oh awesome!

One last request!


Is there any way to keep all of the Grey Bars

To keep with Next?


I tried

.Range.ParagraphFormat.KeepWithNext = wdToggle

But it does not work
Reply With Quote
  #8  
Old 09-30-2015, 02:39 PM
macropod's Avatar
macropod macropod is offline Inserting a Merged Row after a certain text is found in a table Windows 7 64bit Inserting a Merged Row after a certain text is found in a table Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The 'Keep with Next' attribute should be managed by whatever paragraph Style you use for the text on the grey lines. Although the code I posted uses Word's Normal Style, that isn't fixed in concrete - you could use any other suitable Style that has all the font, alignment & other attributes you want. That said:
.Range.ParagraphFormat.KeepWithNext = True
works for me when inserted after:
.Range.Style = wdStyleNormal
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 09-30-2015, 02:40 PM
Snaybot Snaybot is offline Inserting a Merged Row after a certain text is found in a table Windows 7 64bit Inserting a Merged Row after a certain text is found in a table Office 2013
Novice
Inserting a Merged Row after a certain text is found in a table
 
Join Date: Sep 2015
Posts: 22
Snaybot is on a distinguished road
Default

is there something Im not doing right with keep with next
Reply With Quote
  #10  
Old 09-30-2015, 02:44 PM
macropod's Avatar
macropod macropod is offline Inserting a Merged Row after a certain text is found in a table Windows 7 64bit Inserting a Merged Row after a certain text is found in a table Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Without seeing how you're implementing it and, perhaps, a document containing a table that shows what's not working, I can't really say.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #11  
Old 09-30-2015, 02:47 PM
Snaybot Snaybot is offline Inserting a Merged Row after a certain text is found in a table Windows 7 64bit Inserting a Merged Row after a certain text is found in a table Office 2013
Novice
Inserting a Merged Row after a certain text is found in a table
 
Join Date: Sep 2015
Posts: 22
Snaybot is on a distinguished road
Default

Macro name is GBM2

Hmm Im quite confused because it works on other tables
Attached Files
File Type: docm TEMP Functional Verification.docm (30.4 KB, 10 views)
Reply With Quote
  #12  
Old 09-30-2015, 02:57 PM
macropod's Avatar
macropod macropod is offline Inserting a Merged Row after a certain text is found in a table Windows 7 64bit Inserting a Merged Row after a certain text is found in a table Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The code is working fine; the problem is with the table.

What your table has is a bunch of rows that have been configured to not break across pages. With the very tall rows, some of which are more than the page height, not only is this preventing the grey 'heading' rows from keeping with them but those rows are being cut off at the bottom of their pages.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #13  
Old 09-30-2015, 02:59 PM
Snaybot Snaybot is offline Inserting a Merged Row after a certain text is found in a table Windows 7 64bit Inserting a Merged Row after a certain text is found in a table Office 2013
Novice
Inserting a Merged Row after a certain text is found in a table
 
Join Date: Sep 2015
Posts: 22
Snaybot is on a distinguished road
Default

Ok Im an idiot...


I figured it out, some of my cells were too large and could not keep with next



Last question since im new to the forum!

How do I mark this post as SOLVED

Thanks again

Im new to the Commands in Word as opposed to excel!

Thank you
Thank you
Thank ya!
Reply With Quote
  #14  
Old 09-30-2015, 03:12 PM
macropod's Avatar
macropod macropod is offline Inserting a Merged Row after a certain text is found in a table Windows 7 64bit Inserting a Merged Row after a certain text is found in a table Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by Snaybot View Post
How do I mark this post as SOLVED
Via the Thread Tools dropdown at the top of the thread. Done.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting a Merged Row after a certain text is found in a table Text position in merged table cells - word 2010 ellentk Word Tables 3 09-09-2015 03:33 PM
Inserting a Merged Row after a certain text is found in a table inserting wordwrap table causes paragraph break in text sodiumkpump Word Tables 1 08-05-2015 06:36 PM
Inserting a Merged Row after a certain text is found in a table Table will not allow sorting because "cells are merged". I can't find the merged cells. wendyloooo Word Tables 1 05-26-2015 01:19 PM
Inserting Images into a Table Cell without Moving the Text suzan Word 0 06-27-2014 05:20 AM
Inserting a Merged Row after a certain text is found in a table programmatically inserting hidden text into a Word 2010 table epid011 Word VBA 16 12-30-2013 11:29 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:24 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