Link to home
Create AccountLog in
Avatar of Andreas Hermle
Andreas HermleFlag for Germany

asked on

Set specific row height on Condtion, Word Table

Dear Expert,

my code sets a specific height to those rows where the contents of the second column is '99-999-99-99'. I used the Mid-Function. The 99-999-99-99 is the only string in this column, hence I believe there is another approach to this task. My code is kind of slow.

Help is much appreciated. Thank you very much  in advance.

Regards, Andreas


Sub Specific_Height_On_Condition_AllTables()

For i = 1 To ActiveDocument.Tables.Count
    With ActiveDocument
        With .Tables(i)
            For Each rw In .Rows
                If Mid(rw.Cells(2).Range.Text, 1, 12) = "99-999-99-99" Then
                rw.Height = CentimetersToPoints(1.1)
                End If
            Next
        End With
    End With
Next i
End Sub

Open in new window

Avatar of IrogSinta
IrogSinta
Flag of United States of America image

If 99-999-99-99 is the only string in the column, you don't need to use the Mid function.
                If rw.Cells(2).Range.Text = "99-999-99-99" Then
                    rw.Height = CentimetersToPoints(1.1)
                End If

Open in new window

I'm not certain that there would be a great improvement in speed though.
An alternative to using the count of your tables.  Again, I don't know if it would make a speed difference.
    Dim tbl As Table
    For Each tbl In ActiveDocument.Tables
        For Each rw In tbl.Rows
            If rw.Cells(2).Range.Text = "99-999-99-99" Then
                rw.Height = CentimetersToPoints(1.1)
            End If
        Next
    Next

Open in new window

SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Andreas Hermle

ASKER

Thank you very much for your overwhelming support. I really appreciate it.

IrogSinta: thank you very much for your help. Regrettably the code does not work. One has either run my initial code or the ones created by Chris Bottomley

Graham: thank  you very much for bringing this to my attention, ie. 'Each tbl' being faster than  the other alternative

Chris: really a great, professional job!! I did not know that there are another two characters (Chr(13) & Chr(7)) to look for. Very interesting.

Again thank you very much for  your great help.

Regards, Andreas
Rather than iterating each row in each table the second option of course simply searches for the key string and therefore even whilst administration of the search process may be more time consuming per string found ... when there are relatively few it has the potential to be quicker overall.  I also used constants to avoid even the tiny overherads of looking up conversions repetitively.

All told glad it seemed to help ... always enjoy finding another approach - even if search is hardly rocket science!

Chris
Also, re the cell text - I always use to this function to read the inserted text from a cell:

Function GetCellText(cl As Cell) As String
    Dim rng As Range
   
    Set rng = cl.Range
    rng.MoveEnd wdCharacter, -1
    GetCellText = rng.Text
End Function
Graham

I have seen you do so and the general range handling is something I gained from your posts, not to say I am as good but i'm still learning ;o)

I didn't use it for the first suggestion in this context as I felt (rightly or wrongly) that managing the contents as a range rather than simply testing for a constant string would be slower and since speed was the key requirement ...

Regards
Chris
Thank, Chris

I didn't include it in the main question because
a) it was getting a bit crowded, and
b) you had question covered.

I  added it since because of the non-intuitive nature of what a first-glance approach would suggest.  Cell.Range.Text delivers two more characters than the visible text, but it is only necessary to step back one character to retrieve the apparent text.

The Word object model seems to lack some consistency here.