Uniform first line indents

purnima

New Member
Joined
Sep 1, 2005
Messages
46
Hi,

I am trying to code a macro which will determine the first line indents of all paragraphs in a document, and use the indent value that occurs the most often. All the first line indents should be set to this indent value.

I have declared an integer array to store all the first line indents. Now I have to determine the indent value which occurs the maximum in this array. But I just cannot figure out how to do this. Sorting the array won't help, because it will give me the highest indent value and not the value which occurs maximum no. of times.

I'm not sure if this logic is the best. And even if it is, I don't know how to move further with the macro.

Can anyone please direct me?
Any help will be appreciated.

Purnima.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
First of all I don't know how you structured this array - I'm guessing it's a simple list? Personally I'd make it 2-dimensional:

1st dimension = indent size
2nd dimension = counter

So for each paragraph, the code sequence will be:

- Determine size of indent
- If this indent is already in my array then add 1 to the counter
- Otherwise add it to my array and set the counter to 1

This will be held within some kind of looping through paragraphs code, maybe a For..Next or a Do..Loop. Then all you have to do is loop through the resulting array and pick the indent where counter is the highest. Here's my example:
Code:
Sub paras()
    Dim p As Integer 'notional paragraph counter
    Dim u As Integer 'tracks size of array
    Dim i As Integer 'generic counter
    
    Dim CurrentIndent 'size of current indent
    Dim IndentArray 'array of indents and counters (ReDim-med later)
    
    Dim MaxCount As Integer 'highest counter in IndentArray
    Dim ChosenIndent 'indent corresponding to MaxCount
    
    For p = 1 To 10
        CurrentIndent = GetIndentSize(p) 'change this!!
        
        If IsEmpty(IndentArray) Then
            
            u = 0
            ReDim IndentArray(1, 0)
        
        Else
            
            For i = LBound(IndentArray, 2) To u
                If IndentArray(0, i) = CurrentIndent Then
                    'Increment counter
                    IndentArray(1, i) = IndentArray(1, i) + 1
                    GoTo NextPara
                End If
            Next
            
            'If we've got this far then the current indent is not listed
            u = u + 1
            ReDim Preserve IndentArray(1, u)
            IndentArray(0, u) = CurrentIndent
            IndentArray(1, u) = 1
        
        End If
NextPara:
    Next
    
    'Now find the maximum
    MaxCount = 0
    For i = LBound(IndentArray, 2) To UBound(IndentArray, 2)
        If IndentArray(1, i) > MaxCount Then
            MaxCount = IndentArray(1, i)
            ChosenIndent = IndentArray(0, i)
        End If
    Next
    
    'Show the result
    MsgBox "The chosen indent is: " & ChosenIndent & vbNewLine & _
        "It occurred " & MaxCount & " times."
End Sub
Excuse the lazy declarations, but you can change those as required. Also I've notionally used 'p' as a loop to represent paragraphs. You'll need to change the calculation of CurrentIndent to suit you.

Hope it helps.
 
Upvote 0
Oh wow!! This really looks helpful. I should be able to modify the code according to our requirements.

Thank you so very much!!

Purnima.
 
Upvote 0

Forum statistics

Threads
1,215,430
Messages
6,124,846
Members
449,194
Latest member
HellScout

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top