select case not selecting the case

User avatar
stuck
Panoramic Lounger
Posts: 8225
Joined: 25 Jan 2010, 09:09
Location: retirement

select case not selecting the case

Post by stuck »

I have a For i = 1 to numRows / Next i loop (numRows is the number of rows in a table).

Within that loop I want the code to apply specific paragraph formatting to the first 5 rows and the last row and common formatting to all the other rows. To do that I've got a Select Case i / End Select construct that has Case "1", Case "2" ... Case "5" and those bits work fine, as the For loop increments the relevant Case is acted on.

Adding a Case Else handles the other rows correctly but adding Case "numRows" to handle the last row doesn't work. when i reaches numRows it doesn't trigger the Case 'numRows' code, instead it runs the Case Else bit.

Which is a long way of asking how do I set appropriate Case criteria so the code knows to treat the last row differently?

Thanks,

Ken

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: select case not selecting the case

Post by macropod »

Try something along the lines of:

Code: Select all

Sub Demo()
Dim i As Long, j As Long
Const StrStl As String = "MyStyle1"
With ActiveDocument.Tables(1)
  .Range.Style = "MyStyle2"
  If .Rows.Count > 5 Then
    j = 5
  Else
    j = .Rows.Count - 1
  End If
  For i = 1 To j
    .Rows(i).Range.Style = StrStl
  Next
  .Rows(.Rows.Count).Range.Style = StrStl
End With
End Sub
where 'MyStyle2' is the Style for the desired paragraph format for the table as a whole and 'MyStyle1' is the Style for the desired paragraph format for the first 5 and last rows.
Paul Edstein
[Fmr MS MVP - Word]

User avatar
stuck
Panoramic Lounger
Posts: 8225
Joined: 25 Jan 2010, 09:09
Location: retirement

Re: select case not selecting the case

Post by stuck »

Thank you Paul but I'm not sure that will do what I want. The first 5 rows and the last row are not identical, they each have different space before and space after but they are not formatted as Styles, they are simply variations on the Normal style.

Is it not possible to get the Select Case to work out that when i=numRows it's on the last row so Case "numRows" should run? When i=1 or 2 or 3... 5 then the correct Case does run...

Meanwhile, I'll experiment with your suggestion.

Ken


Edited because I've solved it as follows... The For/Next loop and the Select Case/End Select within it format rows 1 to 5 and all the other rows, inc. the last row. On exiting the loop, reformat the last row to it's particular requirement.

Ken

PJ_in_FL
5StarLounger
Posts: 1113
Joined: 21 Jan 2011, 16:51
Location: Florida

Re: select case not selecting the case

Post by PJ_in_FL »

Can you post the Select Case statements? If you're using "1", "2", etc., then the Case statement is comparing the variable to a string not a number.

This seems to work for me to process rows 1 to 5 and last row differently. Note that the counting variable is dimensioned as Long, not Integer.

Code: Select all

Dim i as Long
With ActiveDocument.Tables(1)
    Debug.Print "Rows = ", .Rows.Count
    For i = 1 To .Rows.Count
        Select Case i
            Case 1 To 5
                Debug.Print "Rows 1 to 5"
            Case .Rows.Count
                Debug.Print "Last Row"
            Case Else
                Debug.Print "Other row", i
        End Select
    Next
End With
PJ in (usually sunny) FL