Need help with "do...loop"

J

Jihemer

Hi,

I'm a newbie on this newsgroup.

What I'm trying to do is to fusion several cells on the same line inside a
big sheet that contains about 12 cells and 1500 lines.
The macro I recorded to fusion the cells on a single line is :

Sub Fusion3()
'
' Fusion3 Macro
' Macro enregistrée le 14/01/2004 par BLSH
'
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveRight Unit:=wdCharacter, Count:=4, Extend:=wdExtend
ActiveWindow.ActivePane.SmallScroll ToRight:=3
Selection.MoveRight Unit:=wdCharacter, Count:=2, Extend:=wdExtend
Selection.Cells.Merge
End Sub


The macro sarts on a cell, then moves the cursor down to the following line,
selects the first cell, then the other ones, and finally fusion them in a
single cell with no loss of data.
And it works like a charm !

Now could someone help me to use the "do...loop" command or any other
command to do the same job on all the lines, because it is a long long job
to start the macro at each line !

Thank you for your answers...

Jihemer
 
M

Martin Seelhofer

Hi Jihemer

You can use code like the following:

Do While Selection.Range.Information(wdWithInTable)
' your code goes here
Loop

This makes your code run until the selection is moved
out of the table. However, you might have to fine-tune
your macro to prevent errors forced by unexpected table-
layout. Or, if you don't want to do that, add a line containing ...

On Error Resume Next

.... before the Do-Loop. This will prevent VBA from
complaining about errors but also (and this is *much*
worse) from handling them. Be careful...


Cheers,
Martin
 
J

Jihemer

Thank you very much, Martin, I'll try this as soon as I'm at my office...
But what does the "wdWithInTable" refer to ? The number of cells I want to
merge ?
And In this case do I have to mention this number within the command line or
not ?
If not I suppose my macro should look like this :

Sub Fusion3()
'
' Fusion3 Macro
' Macro enregistrée le 14/01/2004 par BLSH
'
On Error Resume Next
Do While Selection.Range.Information(wdWithInTable)
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveRight Unit:=wdCharacter, Count:=4, Extend:=wdExtend
ActiveWindow.ActivePane.SmallScroll ToRight:=3
Selection.MoveRight Unit:=wdCharacter, Count:=2, Extend:=wdExtend
Selection.Cells.Merge
Loop
End Sub
?

Anyway I'll let you know if it works properly or not...

Thanx again
Jihemer
 
M

Martin Seelhofer

Hi again
But what does the "wdWithInTable" refer to ?

This is a predefined VBA-constant (which you don't have to
replace with a value yourself), which in conjunction with
Selection.Range.Information(...) forces VBA to return TRUE
if the Selection is situated inside a table or FALSE if it is not.
Together with "Do While ..." this makes the loop run until
the Selection moves out of the table...


Cheers,
Martin
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

Hi Jihemer,

It would be a lot quicker to use something like:

Dim myrange As Range, i As Long
For i = 1 To ActiveDocument.Tables(1).Rows.Count
Set myrange = ActiveDocument.Tables(1).Cell(i, 1).Range
myrange.End = ActiveDocument.Tables(1).Cell(i, 3).Range.End
myrange.Cells.Merge
Next i

Change the numbers in the two Cell(1, #) to grab the actual cells that you
want to merge.

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.
Hope this helps
Doug Robbins - Word MVP
 
J

Jihemer

OK, Martin,

Now the work is done ! And your macro worked perfectly on the condition that
the first selected cell had no more than one line.
When this condition was fulfilled, I could have all the cells I wanted
merged inside the table. This was great help for me !

Cheers,

Jihemer
 
J

Jihemer

Thank you Doug,

As I'll problably have to do the same kind of work several times, be sure i
will consider your macro with interest, try it and tell you the result, if
any...

Sincerely

Jihemer

"Doug Robbins - Word MVP - DELETE UPPERCASE CHARACTERS FROM EMAIL ADDRESS"
 
J

Jihemer

Well, Doug, your macro is just OK !
....And much quicker, and much safer that the previous one.
I have tested on a small file and i can say that the work was done "quickly
and smoothly".
Now I have to do the same job on a 7 000 lines file...
Obviously i couldn't have done it all by myself, doing the same little macro
7000 times.
A proof that visual basic for Word is really worth using.
So thank you again !

Jihemer
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

HI Jihemer,

I am glad that it helped.

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.
Hope this helps
Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top