Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

merge cells

Status
Not open for further replies.

vadim86

Programmer
Aug 15, 2007
8
CA
Hello,
I need to merge table header cells horizontally and some vertically from my VB6 program. The problem is that after merging first 2 cells indexing of other cells changes. So, after merging cell(1,1) and cell(1,2) the original cell(1,3) changes to cell(1,2). I have some headers with 3-4 rows and about 10 columns with different merging configurations.
I create 2 arrays:
1- mergeFrom[(1,1),(1,4) ...........]
2 -mergeTo[(1,2),(2,4).............]

.Cell(mergeFrom(i).Row, mergeFrom(i).col).Merge .Cell(mergeTo(i).Row, mergeTo(i).col)


What is the best way to keep track of changing cell coordinates after merging cells??

Also, merging cell by cell if very slow. Is the better/faster way to merge cells?


Thank you
 
Hi,

So you're coding in VB6, not VBA?

Even so, these questions can be answered here.

So you only need to keep track of Word Cell Merging while running this code; not after the fact?

I'd set up an array that describes the table and how the horizontal & vertical merging has changed the indexing.
 
Thanks SkipVought

Yes, I'm coding in VB6 , but suggested asking my question on VBA forum instead of VB6

I'm adjusting my array indexes for row and column in my array, but can't get it to work properly. Works fine only for two row and very very slow.
 

I run through my array of cells and subtract 1 from every column that is located to the right of my current merged column only when detected horizontal merge (mergeFrom.row = mergeTo.row)
The same I do for rows.

 
SkipVought,
I think that I also have to check whether whole row was already merged before adjusting row index.

Is there maybe easier and FASTER way to do it?
 
I really can't say. Quite often ideas emerge as the logic developes.

I'm sitting in a doctor's office; not the best place to do design.

Work through it, post your code where you think you have problems. You'll get some help.
 
Have you solved your problem? If not what issues do you have? Post your Word .docm so we can play with your code/table example.
 
Hello SkipVought,
Unfortunately, I still didn't solve the problem. I managed to merge cells for two rows, but still doesn't work for 3 and more.
I attached word document with tables before and after merge.

My code that works for two rows :
I have 2 arrays

1. mergecell[(1,1),(1,4), .. ...
2. mergecellTo[(1,2),(2,4), ......

For i = 1 To UBound(mergecell)

.Cell(mergecell(i).Row, mergecell(i).col).Merge .Cell(mergecellTo(i).Row, mergecellTo(i).col)


If (mergecell(i).Row = mergecellTo(i).Row) Then
'adjust columns of the same row due to reduced number of columns
For j = 1 To UBound(mergecell)
If mergecell(j).Row = mergecell(i).Row And mergecell(j).col > mergecell(i).col Then
mergecell(j).col = mergecell(j).col - 1
End If
If mergecellTo(j).Row = mergecellTo(i).Row And mergecellTo(j).col > mergecellTo(i).col Then
mergecellTo(j).col = mergecellTo(j).col - 1
End If
Next j
End If

If (mergecell(i).col = mergecellTo(i).col) Then
'adjust rows of the same column due to reduced number of rows
For j = 1 To UBound(mergecell)
If mergecell(j).col = mergecell(i).col And mergecell(j).Row - mergecell(i).Row = 1 Then
mergecell(j).Row = mergecell(j).Row - 1
End If
If mergecellTo(j).col = mergecellTo(i).col And mergecellTo(j).Row - mergecellTo(i).Row = 1 Then
mergecellTo(j).Row = mergecellTo(j).Row - 1
End If
Next j
End If
Next i




I really appreciate your help.
 
 http://files.engineering.com/getfile.aspx?folder=ef622e5e-7bfb-4de8-8ca5-ca2bf26da2b1&file=merge_cells.doc
Instead of merging from the top down & left to right, why don't you go in the reverse order (e.g., bottom to top, right to left). That way, you row's won't be deleted with the mergers and you don't need to have any additional code to figure out how many rows to adjust your future merging.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top