locked
Number rows based on punctuation at end of row RRS feed

  • Question

  • Hi all.

    I'm working on a project, which I am writing out a LOT of questions. 

    Once I finish, I need to number each question. Presently I am doing this manually. My question is---

    What would be the vba code to number each question, based on having the question mark at the end of the question? 

    EG

    Where may these resources be found?

    1.150 PURPOSE

    What is the purpose of this manual?

    Where is this reference located?

    In the four lines above, I have 3 questions, and one header. As you can see, there's no question mark at the end of the header. 

    I'd like to programmatically number all of my questions, while ignoring the headers (the one common feature is that the headers do not have question marks at the end of the row)

    So that once it's done it looks like 

    4- Where may these resources be found?

    1.150 PURPOSE

    5- What is the purpose of this manual?

    6- Where is this reference located?

    I'm writing out thousands of questions for a really large manual we use, as a training procedure, and I'm just tired of numbering these things manually. 

    TYIA.

    Wednesday, December 16, 2020 8:56 PM

Answers

  • You might try this macro. Make sure that paragraphs really end in a question mark. The macro won't work as intended if there is a question mark followed by a space at the end of a paragraph.

    Sub AddNumbering()
        Dim par As Paragraph
        Application.ScreenUpdating = False
        For Each par In ActiveDocument.Paragraphs
            If Len(par.Range) > 1 Then
                If Mid(par.Range.Text, Len(par.Range) - 1, 1) = "?" Then
                    par.Style = ActiveDocument.Styles(wdStyleListNumber)
                End If
            End If
        Next par
        Application.ScreenUpdating = True
    End Sub


    Regards, Hans Vogelaar (https://www.eileenslounge.com)

    • Marked as answer by SteveDB1 Thursday, December 17, 2020 2:58 PM
    Wednesday, December 16, 2020 11:04 PM

All replies

  • Why don't you apply a numbered  style such as the built-in style List Number to those paragraphs? Word will number them automatically, and if you move paragraphs around, the numbering will be updated automatically.

    Regards, Hans Vogelaar (https://www.eileenslounge.com)

    Wednesday, December 16, 2020 10:21 PM
  • Why don't you apply a numbered  style such as the built-in style List Number to those paragraphs? Word will number them automatically, and if you move paragraphs around, the numbering will be updated automatically.

    Regards, Hans Vogelaar (https://www.eileenslounge.com)

    Honestly, I hadn't thought about that (previous attempts not-related to this project have been frustrating, so I pretty much forgot all about it.), and the attempt to use it just now.... it's not letting me do so. 

    The questions are all asked already. I'm just working through numbering them. Which is why I wanted to use the question mark as the determining factor. 

    Wednesday, December 16, 2020 10:43 PM
  • Part of the reason I also have not been using the built-in numbering tool Word has is because the questions are being placed in comments.

    So, each segment I'm dealing with has different comment groupings. I then run a macro, after I'm finished asking all my questions, and that macro extracts the questions from the comments, and drops them into a new word document. 

    I then order the questions with the headers accordingly, in the new document, and number them there. 

    Wednesday, December 16, 2020 10:51 PM
  • You might try this macro. Make sure that paragraphs really end in a question mark. The macro won't work as intended if there is a question mark followed by a space at the end of a paragraph.

    Sub AddNumbering()
        Dim par As Paragraph
        Application.ScreenUpdating = False
        For Each par In ActiveDocument.Paragraphs
            If Len(par.Range) > 1 Then
                If Mid(par.Range.Text, Len(par.Range) - 1, 1) = "?" Then
                    par.Style = ActiveDocument.Styles(wdStyleListNumber)
                End If
            End If
        Next par
        Application.ScreenUpdating = True
    End Sub


    Regards, Hans Vogelaar (https://www.eileenslounge.com)

    • Marked as answer by SteveDB1 Thursday, December 17, 2020 2:58 PM
    Wednesday, December 16, 2020 11:04 PM
  • You might try this macro. Make sure that paragraphs really end in a question mark. The macro won't work as intended if there is a question mark followed by a space at the end of a paragraph.

    Sub AddNumbering()
        Dim par As Paragraph
        Application.ScreenUpdating = False
        For Each par In ActiveDocument.Paragraphs
            If Len(par.Range) > 1 Then
                If Mid(par.Range.Text, Len(par.Range) - 1, 1) = "?" Then
                    par.Style = ActiveDocument.Styles(wdStyleListNumber)
                End If
            End If
        Next par
        Application.ScreenUpdating = True
    End Sub


    Regards, Hans Vogelaar (https://www.eileenslounge.com)

    Thank you Hans! 

    You Rock! 

    And thank you for pointing out the space after the question mark. I never would've thought about it. Turns out most of them did have the space, so using Ctrl+h, I cleaned them up. 

    What code would I use to change the numbering style to have a hyphen instead of a period? 

    Next, what would I need to do to pick up in the middle of one that I've already started. I got up to 560 on my existing count, and would like to finish with the present format--- "xxx- "


    • Edited by SteveDB1 Wednesday, December 16, 2020 11:53 PM append statement
    Wednesday, December 16, 2020 11:51 PM
  • The following macro will set the numbering to use a hyphen. You have to run it only once.

    Sub SetHyphen()
        With ListGalleries(wdNumberGallery).ListTemplates(1).ListLevels(1)
            .NumberFormat = "%1 -"
            .LinkedStyle = ActiveDocument.Styles(wdStyleListNumber)
        End With
        ActiveDocument.Styles(wdStyleListNumber).LinkToListTemplate _
            ListTemplate:=ListGalleries(wdNumberGallery).ListTemplates(1), _
            ListLevelNumber:=1
    End Sub

    To apply numbering from the current location down, change the AddNumbering macro to

    Sub AddNumbering()
        Dim rng As Range
        Dim par As Paragraph
        Application.ScreenUpdating = False
        Set rng = ActiveDocument.Range(Start:=Selection.Start, End:=ActiveDocument.Content.End)
        For Each par In rng.Paragraphs
            If Len(par.Range) > 1 Then
                If Mid(par.Range.Text, Len(par.Range) - 1, 1) = "?" Then
                    par.Style = ActiveDocument.Styles(wdStyleListNumber)
                End If
            End If
        Next par
        Application.ScreenUpdating = True
    End Sub


    Regards, Hans Vogelaar (https://www.eileenslounge.com)

    Thursday, December 17, 2020 11:17 AM
  • Good morning Hans. 

    Thank you very much!!!!!! 

    These do exactly what I want.

    Have a great rest of your day and/or evening. 

    Best.

    SteveDB

    Thursday, December 17, 2020 3:54 PM