merging cells using VB

  • Thread starter Carrie_Loos via OfficeKB.com
  • Start date
C

Carrie_Loos via OfficeKB.com

I have a row of 1's and 0's. I would like to merge all the cells with 1's
that are grouped in a row and not merge the 0's.
Example:

1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1
1

So that the cells with 1's in them merge and the cells with 0's in them do
not merge.

Is is possible?

Thanks in advance
Carrie
 
H

Helmut Weber

Hi Carrie,

beware of line breaks by the news reader.

Sub Test0101x()
Dim oTbl As Table
Dim oRow As Row
Dim ocll As Cell
Set oTbl = ActiveDocument.Tables(1)
For Each oRow In oTbl.Rows
For Each ocll In oRow.Cells
' ocll.Select ' for testing
While Left(ocll, 1) = "1" And _
Left(ocll.Next, 1) = "1" And _
ocll.RowIndex = ocll.Next.RowIndex
ocll.Merge mergeto:=ocll.Next
ocll.Range.Text = Replace(ocll.Range.Text, Chr(13), "")
If ocll.Range =
oTbl.Range.Cells(oTbl.Range.Cells.Count).Range Then Exit Sub
Wend
Next
Next
End Sub


--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
C

Carrie_Loos via OfficeKB.com

Thank you, I can't wait to try this out!

Helmut said:
Hi Carrie,

beware of line breaks by the news reader.

Sub Test0101x()
Dim oTbl As Table
Dim oRow As Row
Dim ocll As Cell
Set oTbl = ActiveDocument.Tables(1)
For Each oRow In oTbl.Rows
For Each ocll In oRow.Cells
' ocll.Select ' for testing
While Left(ocll, 1) = "1" And _
Left(ocll.Next, 1) = "1" And _
ocll.RowIndex = ocll.Next.RowIndex
ocll.Merge mergeto:=ocll.Next
ocll.Range.Text = Replace(ocll.Range.Text, Chr(13), "")
If ocll.Range =
oTbl.Range.Cells(oTbl.Range.Cells.Count).Range Then Exit Sub
Wend
Next
Next
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
H

Helmut Weber

Hi Carrie,

after one night's sleep there is a solution
without error trapping:

Sub Test0101xx()
Dim oTbl As Table
Dim oRow As Row
Dim ocll As Cell

Set oTbl = ActiveDocument.Tables(1)
For Each oRow In oTbl.Rows
For Each ocll In oRow.Cells
While Left(ocll, 1) = "1" And _
Left(ocll.Next, 1) = "1" And _
ocll.RowIndex = ocll.Next.RowIndex
ocll.Merge mergeto:=ocll.Next
ocll.Range.Text = Replace(ocll.Range.Text, Chr(13), "")
If ocll.Next Is Nothing Then Exit Sub
Wend
Next
Next
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
C

Carrie_Loos via OfficeKB.com

Hi Helmut-

I am now back and working with this after the holidays and I am having
problems getting this to work. I keep getting object or type errors. I have
changed the variables to variants and unsucessfully typed the variables. Any
ideas?

Thanks
Carrie
 
D

Doug Robbins - Word MVP

Helmut's code works fine for me.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
H

Helmut Weber

Hi Carrie,

are you using Word?
Are you trying to control Word from another application?

At second thought your question looks like an Excel-question.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
C

Carrie_Loos via OfficeKB.com

Yes, it is an Excel Question. When I run the macro, I get the portion of the
declared variabe "oTbl As Table" highlighted and the Microsoft Visual Error
Message of "Compile Error: User-defined type not defined"

Why do you suppose the other person can run it without incidence and I get
this message?

Thanks
Carrie
 
C

Carrie_Loos via OfficeKB.com

I am going to have the IT folks 're-install' Excell as a full install and see
if it will update and expand the libraries and see if I can get rid of these
errors. What do you think?

Carrie_Loos said:
Yes, it is an Excel Question. When I run the macro, I get the portion of the
declared variabe "oTbl As Table" highlighted and the Microsoft Visual Error
Message of "Compile Error: User-defined type not defined"

Why do you suppose the other person can run it without incidence and I get
this message?

Thanks
Carrie
Hi Carrie,
[quoted text clipped - 10 lines]
Vista Small Business, Office XP
 
H

Helmut Weber

Hi Carrie,
I am going to have the IT folks 're-install' Excell as a full install and see
if it will update and expand the libraries and see if I can get rid of these
errors. What do you think?

don't! It is waisted efforts.

The code I gave you is Word-Code,
which Doug Robbins took for granted
and checked it, using a Word-table,
as you were asking in a group for Word-programming.

It isn't supposed to be used in Excel.

Ask in ...public.excel.programming,
experts there will like your question.
And don't forget to mention your Excel-version.

:-(
Its a shame, it was a fine piece of code I gave you,
but don't worry, things like that happen.
After all, I'm posting here just for fun.

Have a nice day. :)

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
C

Carrie_Loos via OfficeKB.com

Darn, well thank you and I am sure there is somebody out there who is
struggling with this in Word and will be really excited to see this thread. I
thought the code was pretty darn good myself.
 

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