PDA

View Full Version : changing numbering of lists



hlucsa
12-08-2012, 02:54 PM
I'm a medical transcriptionist and I often get lists that look like this:

1. Blah blah blah
3. blah blah blah
2. Blah Blah Blah
6. Blah Blah blah

I need a code that will start from the top of the list and change the numbers so they are consecutive until it hits the end of the list. I plan to share this with a whole forum of other transcriptionists so it would help out a lot of us. I know c++ and tried to code it myself but this language is totally different...any help would be greatly appreciated!

macropod
12-09-2012, 01:28 AM
Try:
Sub Demo()
Dim Rng As Range, Para As Paragraph
With ActiveDocument
For Each Para In .Paragraphs
Set Rng = Para.Range.Words.First
Rng.End = Rng.End + 1
If Rng.Text Like "#." Or Rng.Text Like "##." Then
Rng.Text = vbNullString
Para.Range.ListFormat.ApplyNumberDefault
End If
Next
End With
End Sub

hlucsa
12-09-2012, 08:35 AM
Hi Paul, thanks for the code it worked perfect! But I forgot to mention we use a software called Editscript and it doesn't allow the list function. So we have to type literally 1. space space and then the text. Is there any way to change the code so it does that? And also we have multiple lists in a report, so is there a way to make it stop once it hits a line with no text? Thanks again for your help! :)

macropod
12-09-2012, 01:17 PM
When you ask "is there a way to make it stop once it hits a line with no text?", do you mean you want the macro to exit, or that you want the numbering to re-start?

If it's the former and you need to be able to re-number a range that excludes any numbered paragraphs before a certain point, the user will have to select the range to be processed.

hlucsa
12-09-2012, 02:18 PM
I'd like to be able to put the cursor at the beginning of a list like:

(cursor here)1. ......
2. .....
4. .....

Then type a short cut to start the macro like ctrl-O and have it run down the list until it hits the end of the list and then terminate.

macropod
12-09-2012, 04:12 PM
Try the following macro. It lets you nominate the starting #. As coded, the macro skips over un-numbered & empty paragraphs in the selection.
Sub Renumber()
Application.ScreenUpdating = False
Dim Rng As Range, Para As Paragraph, i As Long
i = 1
On Error GoTo ErrExit
With Selection.Range
Set Rng = .Paragraphs.First.Range.Words.First
If Trim(Rng.Text) Like "#" Or Trim(Rng.Text) Like "##" Then
i = Trim(Rng.Text)
End If
i = CLng(InputBox("Please enter the starting number", "Paragraph re-numbering", i))
For Each Para In .Paragraphs
Set Rng = Para.Range.Words.First
Rng.End = Rng.End + 1
If Rng.Text Like "#." Or Rng.Text Like "##." Then
Rng.Text = i & "."
i = i + 1
End If
Next
End With
ErrExit:
Application.ScreenUpdating = True
End Sub

hlucsa
12-09-2012, 04:40 PM
Hi Paul, thanks :) That popup is pretty cool who knew VBA could do that. I tried the code and it stopped after numbering the first line. I tried different numbers too...

But what I was hoping for is a program that will terminate once it hits an empty paragraph so only that one list is edited. Because the transcriptionists that I'll share it with and myself we edit large reports. So if it stops after the list ends and doesn't go through the rest of the report its safer for us, we don't have to read through the rest of the report to make sure it didn't do something "bad"....

like I know in c++ it could be done...but I don't know if VBA has the functionality to recognize to stop after the list ends...and not go through the whole report...

macropod
12-09-2012, 04:59 PM
With the code in my last post, you have to select the paragraphs to be renumbered. The following version starts with the first selected paragraph then continues through the document until an empty paragraph is found.
Sub Renumber()
Application.ScreenUpdating = False
Dim Rng As Range, Para As Paragraph, i As Long
i = 1
On Error GoTo ErrExit
With Selection.Range
Set Rng = .Paragraphs.First.Range.Words.First
If Trim(Rng.Text) Like "#" Or Trim(Rng.Text) Like "##" Then
i = Trim(Rng.Text)
End If
i = CLng(InputBox("Please enter the starting number", "Paragraph re-numbering", i))
.End = ActiveDocument.Range.End
For Each Para In .Paragraphs
If Len(Para.Range) = 1 Then GoTo ErrExit
Set Rng = Para.Range.Words.First
Rng.End = Rng.End + 1
If Rng.Text Like "#." Or Rng.Text Like "##." Then
Rng.Text = i & "."
i = i + 1
End If
Next
.Collapse wdCollapseStart
End With
ErrExit:
Application.ScreenUpdating = True
End Sub

fumei
12-09-2012, 05:00 PM
Try:

Sub Renumber()
Application.ScreenUpdating = False
Dim Rng As Range, Para As Paragraph, i As Long
Dim r As Range
i = 1
On Error GoTo ErrExit
Set r = ActiveDocument.Range(Start:=Selection.Start, _
End:=ActiveDocument.Range.End)
With r
Set Rng = .Paragraphs.First.Range.Words.First
If Trim(Rng.Text) Like "#" Or Trim(Rng.Text) Like "##" Then
i = Trim(Rng.Text)
End If
i = CLng(InputBox("Please enter the starting number", "Paragraph re-numbering", i))
For Each Para In .Paragraphs
Set Rng = Para.Range.Words.First
Rng.End = Rng.End + 1
If Rng.Text Like "#." Or Rng.Text Like "##." Then
Rng.Text = i & "."
i = i + 1
Else
Exit For
End If
Next
End With
ErrExit:
Application.ScreenUpdating = True
End Sub

If you put the cursor just before the first 1.

Yadda shndlsjldsdfs
1. first
2. second
3. sdsdhsfdhs
7. sjdshshfdsf
9. sjfljsljfsd
adjalnda
dlalfdasfd
2. hdkahdakdsjdslajdl
5. ahsahsak

And execute, you get:

Yadda shndlsjldsdfs
1. first
2. second
3. sdsdhsfdhs
4. sjdshshfdsf
5. sjfljsljfsd
adjalnda
dlalfdasfd
2. hdkahdakdsjdslajdl
5. ahsahsak


But the cursor MUST be at the start of the "list". Remember though...it is NOT a list as far as Word is concerned. It is only plain text.

hlucsa
12-09-2012, 05:17 PM
Thanks guys, both codes work perfect. I'm going to share this with the other transcriptionists on MTstars that use word/editscript, they'll love it!:clap::bow:

macropod
12-09-2012, 05:23 PM
Do be aware that there is a possibly significant difference between fumei's macro and mine:
1. Fumei's macro exits at the first un-numbered paragraph, whether or not it is empty
2. My macro skips over un-numbered paragraphs that contain text and exits at the first empty paragraph.

fumei
12-09-2012, 06:15 PM
I did it as #1 because of "until it hits the end of the list and then terminate." There was no mention of un-numbered, but textual paragraphs...so I terminated it as both (or more accurately, either).

But hey, all in the realm of clearly defined requirements.

hlucsa
12-09-2012, 06:58 PM
Both will work great. I'm telling the transcriptionists to just make sure there's a number and period befor each item of the list before they run the macro. So either one will work, I tried them both. Thanks again... :)