#1
|
|||
|
|||
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 |
#2
|
||||
|
||||
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] |
#3
|
|||
|
|||
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! |
#4
|
||||
|
||||
Well, yes, but how is the code to know whether it should be replaced or left alone?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
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! |
#6
|
||||
|
||||
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] |
#7
|
|||
|
|||
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 |
#8
|
||||
|
||||
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] |
#9
|
|||
|
|||
is there something Im not doing right with keep with next
|
#10
|
||||
|
||||
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] |
#11
|
|||
|
|||
Macro name is GBM2
Hmm Im quite confused because it works on other tables |
#12
|
||||
|
||||
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] |
#13
|
|||
|
|||
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! |
#14
|
||||
|
||||
Via the Thread Tools dropdown at the top of the thread. Done.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Text position in merged table cells - word 2010 | ellentk | Word Tables | 3 | 09-09-2015 03:33 PM |
inserting wordwrap table causes paragraph break in text | sodiumkpump | Word Tables | 1 | 08-05-2015 06:36 PM |
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 |
programmatically inserting hidden text into a Word 2010 table | epid011 | Word VBA | 16 | 12-30-2013 11:29 AM |