Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-19-2016, 04:55 AM
rocky2 rocky2 is offline Overcome issues in tables with vertically merged cells Windows 10 Overcome issues in tables with vertically merged cells Office 2013
Novice
Overcome issues in tables with vertically merged cells
 
Join Date: Feb 2016
Posts: 25
rocky2 is on a distinguished road
Default Overcome issues in tables with vertically merged cells

Hi,



I am trying to format tables. Have to check all kinds of issues.
However, when I try to check the row number in a cell that has vertically merged cells, I get an exception.
(I need the row number to know if the cell can be a heading or not etc).

Is there any way to access table rows even if there are vertically merged cells?

Thanks,
Rocky
Reply With Quote
  #2  
Old 12-19-2016, 03:15 PM
macropod's Avatar
macropod macropod is offline Overcome issues in tables with vertically merged cells Windows 7 64bit Overcome issues in tables with vertically merged cells Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,369
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 rocky2 View Post
when I try to check the row number in a cell that has vertically merged cells, I get an exception
You'll need to provide more information about this 'exception'. If it's something to do with code you're using, you'll need to post that code.

PS: When posting code, please use the code tags, indicated by the # button on the posting menu. Without them, your code loses much of whatever structure it had.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 12-19-2016, 04:04 PM
Guessed's Avatar
Guessed Guessed is offline Overcome issues in tables with vertically merged cells Windows 10 Overcome issues in tables with vertically merged cells Office 2013
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,159
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

Using Rows or Columns is problematic in non uniform tables. For that you will need to check each cell instead
Code:
Sub ExploreATable()
  Dim aCell As Cell, aRow As Row
  With Selection.Tables(1)
    If .Uniform Then
      For Each aRow In .Rows
        Debug.Print "Row: " & aRow.Index, "Cells: " & aRow.Cells.Count
      Next aRow
    Else
      For Each aCell In .Range.Cells
        Debug.Print aCell.RowIndex, aCell.ColumnIndex
      Next
    End If
  End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #4  
Old 12-20-2016, 01:08 AM
rocky2 rocky2 is offline Overcome issues in tables with vertically merged cells Windows 10 Overcome issues in tables with vertically merged cells Office 2013
Novice
Overcome issues in tables with vertically merged cells
 
Join Date: Feb 2016
Posts: 25
rocky2 is on a distinguished road
Default This is what I used

I am searching for a heading in a table and I need to modify it if it's not in the first 2 rows.
I can't (and don't want to) loop through the entire table. I just need to find out if the cell I found is in rows 1-2 or not.

Code:
While Selection.Find.Found
   
        If Selection.Information(wdWithInTable) Then
            Set oTable = Selection.Tables(1)
            
            iRow = Selection.Rows(1).Index
           
            If iRow > 2 Then
                Do something....
            End If
        End If
        Selection.Collapse Direction:=wdCollapseEnd
        Selection.Find.Execute
 Wend
the exception I get is run-time error 5991: Cannot access individual rows in this collection because the table has vertically merged cells.

I tried to access Selection.Range.Cells(1).RowIndex - but I get the same exception.

Thanks for your help,
Rocky
Reply With Quote
  #5  
Old 12-20-2016, 01:53 AM
macropod's Avatar
macropod macropod is offline Overcome issues in tables with vertically merged cells Windows 7 64bit Overcome issues in tables with vertically merged cells Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,369
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

If the heading uses a Heading Style (or some other Style whose scope is limited to headings), simply use Find/Replace to look for the Style as part of the Find parameters, then test whether it's in a table and, if so, get the cell's RowIndex - if it's 1 it's on the first row, and if it's 2 it's on the second row, thus:
Code:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = InputBox("What is the Text to Find")
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .Style = "Heading 1"
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
  End With
  Do While .Find.Found
    If .Information(wdWithInTable) Then
      If .Cells(1).RowIndex < 3 Then
        'Do something with the heading
        MsgBox .Text
      End If
    ElseIf .End = ActiveDocument.Range.End Then
      Exit Sub
    End If
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub
Even without employing Styles, you can still use the same general approach.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 12-20-2016, 03:39 AM
rocky2 rocky2 is offline Overcome issues in tables with vertically merged cells Windows 10 Overcome issues in tables with vertically merged cells Office 2013
Novice
Overcome issues in tables with vertically merged cells
 
Join Date: Feb 2016
Posts: 25
rocky2 is on a distinguished road
Default

Thanks.
But I get the same exception on :

Code:
If .Cells(1).RowIndex < 3 Then
If the table has vertically merged cells.

I also looked at what Guessed posted, but it requires me to go over every cell and that is very time consuming in long documents.
Reply With Quote
  #7  
Old 12-20-2016, 04:29 AM
Guessed's Avatar
Guessed Guessed is offline Overcome issues in tables with vertically merged cells Windows 10 Overcome issues in tables with vertically merged cells Office 2013
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,159
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

Paul's code works on vertically and horizontally merged cells in my testing. How is your table configured?
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #8  
Old 12-20-2016, 04:58 AM
rocky2 rocky2 is offline Overcome issues in tables with vertically merged cells Windows 10 Overcome issues in tables with vertically merged cells Office 2013
Novice
Overcome issues in tables with vertically merged cells
 
Join Date: Feb 2016
Posts: 25
rocky2 is on a distinguished road
Default

Not sure what you mean by "how is it configured".

It have one line that is merged horizontally. (some tables have more - but horizontally merged cells are no problem).

In all tables, the merged cells are in the first column to the left.
There are usually 2 or 3 cells that are merged into a single cell.

When other properties can affect this?
Reply With Quote
  #9  
Old 12-20-2016, 12:54 PM
macropod's Avatar
macropod macropod is offline Overcome issues in tables with vertically merged cells Windows 7 64bit Overcome issues in tables with vertically merged cells Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,369
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 I posted works fine for any table format - i.e. no merged cells, vertically merged cells, horizontally merged cells, varying cell widths in a column, etc. in any combination. If you're getting errors, that's most likely because you've not implemented it as provided. But, since you haven't posted your code, we can't see what you're doing...

Of course, it's also possible the table you're trying to modify has been corrupted somehow. Corrupt tables can often be 'repaired' by:
• converting the tables to text and back again;
• cutting & pasting them to another document that you save the document in RTF format, which you then close then re-open before copying them back to the source document; or
• saving the document in RTF format, closing the document then re-opening it and re-saving in the doc(x) format
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #10  
Old 12-20-2016, 11:12 PM
rocky2 rocky2 is offline Overcome issues in tables with vertically merged cells Windows 10 Overcome issues in tables with vertically merged cells Office 2013
Novice
Overcome issues in tables with vertically merged cells
 
Join Date: Feb 2016
Posts: 25
rocky2 is on a distinguished road
Default

This is the code I used (I marked the line in which I get an exception):

Code:
Sub FixWhiteFontInTable()

With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Style = ActiveDocument.Styles("Table Heading")
    .Text = ""
    .Forward = True
    .Execute Format:=True, Forward:=True
  End With
  Do While .Find.Found
    If .Information(wdWithInTable) Then
      If .Cells(1).RowIndex > 2 Then
        
        Selection.Font.ColorIndex = wdBlack
        
      End If
   
    End If
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With

End Sub
What did I miss?
Reply With Quote
  #11  
Old 12-21-2016, 01:19 AM
macropod's Avatar
macropod macropod is offline Overcome issues in tables with vertically merged cells Windows 7 64bit Overcome issues in tables with vertically merged cells Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,369
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

Did you check whether the table might be corrupted? I don't get an error using that code. Mind you, it also won't do what you're probably expecting it to... (Hint: the code doesn't select anything - and doesn't need to). That said, one wonders why you're overriding a Style definition with hard formatting instead of either modifying the existing Style or applying another one with the required formatting.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #12  
Old 12-22-2016, 01:21 AM
rocky2 rocky2 is offline Overcome issues in tables with vertically merged cells Windows 10 Overcome issues in tables with vertically merged cells Office 2013
Novice
Overcome issues in tables with vertically merged cells
 
Join Date: Feb 2016
Posts: 25
rocky2 is on a distinguished road
Default

OK- if I create a new table and merge cells, it does work.
However, the Word file I have is generated by another software.

What properties could a software give a table that would cause an exception in that row? Are there any hidden table properties? Anything I could reset?

(BTW - it does select and change the color, but you are right and I should change the style and not the color).
Reply With Quote
  #13  
Old 12-22-2016, 03:03 AM
Guessed's Avatar
Guessed Guessed is offline Overcome issues in tables with vertically merged cells Windows 10 Overcome issues in tables with vertically merged cells Office 2013
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,159
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

Is there a reason why you can't post a document that contains an example table we can test if you remove any sensitive content?

If we can see what you are dealing with it will be a lot faster to arrive at a solution.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply

Tags
tables;formula;automating



Similar Threads
Thread Thread Starter Forum Replies Last Post
Overcome issues in tables with vertically merged cells How to vertically align a merged cell to fit text paik1002 Excel 6 09-20-2016 03:00 AM
Combining 2 tables into 1 and use Table2's column widths (hoping for workaround dealing merged cells CodingGuruInTraining Word VBA 24 10-07-2015 07:48 PM
Overcome issues in tables with vertically merged cells 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
Overcome issues in tables with vertically merged cells Unable to vertically center align texts in table cells? tinfanide Word 3 11-24-2013 06:37 AM
How to merge matching cells vertically? Odiseh Excel 1 01-02-2010 02:41 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:53 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft