Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2008

Tip: Looking for answers? Try searching our database.

Remove text from a range

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Harold Druss - 03 Mar 2008 10:20 GMT
Hi
I need this macro to merge cells and remove all text.

**********************************************
Sub MergeRangeAndRemoveText()
   Dim rng As Range

   Set rng = ActiveDocument.Range _
   (Start:=ActiveDocument.Tables(1).Cell(5, 3).Range.Start, _
   End:=ActiveDocument.Tables(1).Cell(6, 5).Range.End)

   rng.Cells.Merge

   rng.Text = ""
End Sub
**********************************************

The merge works fine, but the text is not removed.

Thanks
Harold
Stefan Blom - 03 Mar 2008 11:49 GMT
Delete the text first, and then merge the cells:

Sub MergeRangeAndRemoveText()

Dim rng As Range
Dim i As Long, j As Long

For i = 5 To 6
   For j = 3 To 5
       ActiveDocument.Tables(1).Cell(Row:=i, Column:=j). _
           Range.Text = ""
   Next j
Next i

Set rng = ActiveDocument.Range _
   (Start:=ActiveDocument.Tables(1).Cell(5, 3).Range.Start, _
   End:=ActiveDocument.Tables(1).Cell(6, 5).Range.End)

rng.Cells.Merge

End Sub

FWIW, working with ranges and table columns is tricky. The following code,
used on a document that has a table with at least six rows and more than
five columns, illustrates the problem:

Set rng = ActiveDocument.Range _
   (Start:=ActiveDocument.Tables(1).Cell(5, 3).Range.Start, _
   End:=ActiveDocument.Tables(1).Cell(6, 5).Range.End)

rng.Select

rng.Font.Bold = True
Selection.Font.Name = "Courier New"

Note which cells will be bold and which will be in Courier New!

Signature

Stefan Blom
Microsoft Word MVP

> Hi
> I need this macro to merge cells and remove all text.
[quoted text clipped - 17 lines]
> Thanks
> Harold
Tony Jollans - 03 Mar 2008 12:13 GMT
Very interesting. It 'works' in Word 2007, but not in 2003. I didn't know
there had been a change in behaviour.

The Range you define includes cells (5,3), (5,4), (5,5), ..., (5, last) ,
(6,1), (6,2), ..., (6,5)

If you Select it (rng.Select) then just the block (5,3), (5,4), (5,5),
(6,3), (6,4), (6,5) is selected and it is this block that is merged - as you
already know.

After the merge the Range appears to be redefined (in 2003) which is why the
text in the merged cell is not deleted.

If you redefine it yourself you will be able to delete the text.

   Sub MergeRangeAndRemoveText2()
       Dim rng As Range

       Set rng = ActiveDocument.Range _
       (Start:=ActiveDocument.Tables(1).Cell(5, 3).Range.Start, _
       End:=ActiveDocument.Tables(1).Cell(6, 5).Range.End)

       rng.Cells.Merge

       Set rng = ActiveDocument.Range _
       (Start:=ActiveDocument.Tables(1).Cell(5, 3).Range.Start, _
       End:=ActiveDocument.Tables(1).Cell(5, 3).Range.End)

       rng.Text = ""
   End Sub

Signature

Enjoy,
Tony

> Hi
> I need this macro to merge cells and remove all text.
[quoted text clipped - 17 lines]
> Thanks
> Harold
Harold Druss - 03 Mar 2008 19:57 GMT
> Hi
> I need this macro to merge cells and remove all text.
[quoted text clipped - 17 lines]
> Thanks
> Harold

Hi Tony and Stefan
I should have mentioned I'm using Word 2003.

Thank you both for two working solutions.

I appreciate the help.

Harold
Harold Druss - 06 Mar 2008 10:46 GMT
Hi All
I cannot merge a cell at row 1, column 1
with a cell at row 2, column 1 using a range object.
I can only merge those two cells using selection method.

****************************************************
Dim rng As Range

Set rng = ActiveDocument.Range _

(Start:=ActiveDocument.Tables(1).Cell(1, 1).Range.Start, _
End:=ActiveDocument.Tables(1).Cell(2, 1).Range.End)

rng.Cells.Merge
****************************************************
Just curious
Harold

> Hi
> I need this macro to merge cells and remove all text.
[quoted text clipped - 17 lines]
> Thanks
> Harold
Tony Jollans - 06 Mar 2008 16:49 GMT
It's good to be curious.

Actually, the oddity is cells you _can_ merge with a range, rather than the
ones you can't.

When you select a range as you are doing, it (the Range, but not the
Selection - select the two cells in the UI and then look at
selection.cells.count versus selection.range.cells.count to get a flavour of
this) includes all cells from the first one, reading left to right, top to
bottom to the last one. This group of cells cannot (usually) all be merged.
When you get the result you want it is because Word is recognising the
discrepancy and taking special action to do what it thinks you must mean.
Word doesn't always manage it and sometimes you get an error. I don't know
all the circumstances and, as I pointed out before, it seems that some
behaviour has changed in 2007, but in general, merging cells is one of those
rare actions better taken with the selection  than a range.

Signature

Enjoy,
Tony

> Hi All
> I cannot merge a cell at row 1, column 1
[quoted text clipped - 35 lines]
>> Thanks
>> Harold
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.