Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
  HOME    TRACKER    Visual Basic

Merging Cells Using VB

I want to merge two cells together but not really sure of the procedure I could use.

The problem is the fact that the two cells could be any cells in the spreadsheet and this is causing the coding problems. If it were two cells with a definite range then that would not be a problem?

Any helpers?


View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Merging Cells
how do i merge cells only in the first row ?

Merging Cells

I have a situation here..

I want to merge cells in column A

This cells might be separated by 2 or 3 or more than that before the next value appears

Maybe i looks this way

cells(1,1) = has some value
Cells(2,1) = Empty
cells(3,1) = Empty
cells(4,1) = has some value

so how do i actuall search and merge

maybe the example above, i should merge cells(1,1) to cells(3,1)

then continue checking for the next cells that has some value in it..

Any idea..

Thank you very much..

Merging Two Cells Thru VBA
I need to merge two cells programmatically ie e5 and e6. How do I do it ?


Cells Merging In VBA
Dear Sir,

my coding as follows,

With ActiveWorkbook.Worksheets(sheetname)
.Range(.Cells(i + 13, 2), .Cells(i + 13, 3)).Merge
.Cells(i + 13, 9) = a(i, 8)
End With
How to merge cells(i + 13, 9) & cells(i + 13, 10) & cells(i + 13, 11)colum into 1? Please advice

Thanks & regards,

Edit by Moderator:
Please post Excel questions, in the Excel forum.

Please use the [vb][/vb] tags when you post your code. Edit or reply to this post to see how.

Thank you.

Merging Cells
I am trying to merg two col on my fix row on a flexgrid. Here is my code that does not work. Can anyone tell me what I have wrong? Thanks

PaymentGrid.Row = 0
PaymentGrid.MergeCol(1) = True

Merging Cells
Hello all,

I've been implementing some macros at work and I'm gonna need your help. Basically I'm copying some text from a word document and pasting it in an Excel sheet (all of this is done programmatically). Now because the text is longer than the width of its cell is spans over several cells. Now what I need to do is figure out how far it spans out and from that fact figure out over how many cells it spans out. If I know this I can select the cells and merge them and apply some styling. Resizing the column where the text is contained is not an option since it will mess up other parts of the sheet which I have not authority to change. So I'm pretty much looking for a way of figuring out how many pixels a piece of text is using. If I know this I can make some calculations and figure out the cells it spans over. Any comments/suggestions/hints are greatly appreciated .


Macro For Merging Cells
Excel does not allow to merge cells keeping both its values,

can it be done so using a macro ?

if yes how ?


Merging Cells In Code
I am transferring the data from a dao recordset to an excel sheet and I am working on formatting the sheet in (VB not VBA) code. All is going well except I don't know how to merge cells in code. Any help?


It's a thankless job, but I've got a lot of Karma to burn off

Merging Two Cells (append)
I have a value in =IF(+Data!E200="","",Data!E200)
and a value for =IF(+Data!B200="2004","2004","2003")

or maybe that stuff doesn't even matter.

I want to take the value from cell 1(March), and cell 2(2003).
Put them into one cell("E8") so the value will be (March 2003)

How do I do this?

Msflexgrid Merging Cells?
Hi...I want to merge columns for the fixed row on my msflexgrid. I'm not quite sure on how to use the mergecol
property...any suggestions?


'Pass the coffee over...'

Merging Cells In Flexgrid

I have a column of my flexgrid with the same text in all rows. I would like to merge only two rows. When I set the column with MergeCol to true, all data is merged.

How can I specify the correct rows to merge?


Merging Multiple Cells
How would I merge multiple cells without a text matrix statement for all rows. In the example below row 4-7 on column 2 will merge. Right?

MSFlexGrid1.TextMatrix(4, 2) = "How do I make text from a textbox and combobox fill in here"
MSFlexGrid1.TextMatrix(5, 2) = "and here"
MSFlexGrid1.TextMatrix(6, 2) = "and here"
MSFlexGrid1.TextMatrix(7, 2) = "and here"

I believe all cells have to have same content to merge. How would I do this..

MSFlexGrid..merging Cells
Hey everyone, I have searched this forum and have found that this topic has never really been answerd.

I am wondering how I can merge 2 cells (rows) in MSFlexGrid. The thing is, the two cells I want to merge both have different text. I dont know if this is possible. If there is a way to make it so one row has two lines of text, that would work as well.


Instead of this:

| 2/23/05
| Appointment

I want this:

| 2/23/05
| Appointment

So if anyone has any suggestions, that would be great! Thank you =)

Merging Cells In MSFlexgrid
I'm using the following to merge cells in a MSFlexgrid. For some reason, "Softwood" is centered, but not "Hardwood". Why is this?
VB Code:
With mfgInput        .TextMatrix(0, 0) = "Batch"        .TextMatrix(0, 1) = "Softwood"        .TextMatrix(0, 2) = "Softwood"        .TextMatrix(0, 3) = "Softwood"        .TextMatrix(0, 4) = "Hardwood"        .TextMatrix(0, 5) = "Hardwood"        .TextMatrix(0, 6) = "Hardwood"                'Merge cells            .Row = 0            .MergeCells = flexMergeRestrictRows            .MergeRow(0) = True            .CellAlignment = 4 'centered    End With

Merging Cells Problem?
I can't get this damn flexgrid to merge the cells. I hope someone knows how to do this. Here is my code:

Grid1.MergeCells = 1
Grid1.MergeCol(0) = True
Grid1.MergeCol(1) = True

It doesn't even show anyting in row 0, columns 0 and 1. What am I doing wrong?


Having Trouble Merging Cells
I am using the MSFlexGrid and I am trying to merge rows 0 & 1, column 0. How do you do that? I can't get it to work. Here is my code:

Grid1.MergeCol(0) = True
Grid1.MergeRow(0) = True
Grid1.MergeRow(1) = True
Grid1.MergeCells = flexMergeFree

Thanks for your help,

Merging Cells In Flexgrid
I need to merge cells in a flexgrid HORIZONTALLY. The first cell has some text. The other 2 cells do not have any text. Is it possible ?


Merging Cells In MSFlexGrid

Here it is:

gridTest.MergeCells = flexMergeFree
Do Until row >= Me.gridTest.Rows - 1
gridTest.MergeRow(row) = True

Do Until col >= Me.gridTest.Cols - 1
gridTest.MergeCol(col) = True
col = col + 1
row = row + 1

is it possible to simulatinously merge rows and columns with the same content in MSFlexGrid.

Look on the last row of the image (Dima column). Simultanious merge occurs in this column, but as you see it does not merge properly.

I know that you can merge only rows or only columns.

Can you please help.

Merging Cells In MSFelxGrid

Here it is:

gridTest.MergeCells = flexMergeFree
Do Until row >= Me.gridTest.Rows - 1
gridTest.MergeRow(row) = True

Do Until col >= Me.gridTest.Cols - 1
gridTest.MergeCol(col) = True
col = col + 1
row = row + 1

is it possible to simulatinously merge rows and columns with the same content in MSFlexGrid.

Look on the last row of the image (Dima column). Simultanious merge occurs in this column, but as you see it does not merge properly.

I know that you can merge only rows or only columns.

Can you please help.

Merging Cells With MSHFlexGrid...
Hy everyone,
using MSHFlexGrid object i need to merge only selected cells in a row.
I know using MSHFlexGrid1.MergeCells = 2, merging is made for any adjacent cell in a row, but i only merge few cells not all the cell of the row

the grid is like this

COL: |1 | 2 | 3 | 4 | 5 | 6 |
......................................... ROW n
A | B | C | C | C | F |... ROW n+1
.................................... .... ROW n+2

i want to merge only cells of ROW n + 1 and colums 3-4,
even if i have column 5 with the same content "C" of 3 and 4 i do not want to merge it...
so i would like to obtain:

COL: |1 | 2 | 3 | 4 | 5 | 6 |
......................................... ROW n
A | B | C | C | F |... ROW n+1
.................................... .... ROW n+2

setting MSHFlexGrid1.MergeRow(ROW n+1) = True, all cells whith "C" (3,4,5) are merged togheter ...

Is there a way to stop merging cells, in a row, to a wished column?

Thank a lot

Merging Cells In MSHFlexGrid
Hi all,

I've got a heirarchical flexgrid which has it's recordset property set to an ADODB recordset.  Is there any way to merge cells in a specific band when it's connected to a recordset?  

I've tried all the methods that would work if you manually populated the the grid but nothing happens.

I'd like the parent band to extend across all columns and repeating information and then merging is the only way I can think of doing this.  But if merge doesn't work that's a waste of time.

Any ideas?


Merging Alternate Cells In Excel Using VBA
Hi Everybody,

I am struck at Merging rows and column inexcel using VBA.

I had made first two rows as fixed rows.
With Grid1
.MergeCells = 2
.MergeRow(0) = True
.MergeRow(1) = True
End With

Then, I have set of Field Names in column "A" to "F". I merged them row-wise using the following command.

Grid1.TextMatrix(0, 0) = "S NO"
Grid1.TextMatrix(1, 0) = "S NO"

Grid1.TextMatrix(0, 1) = "ZONE"
Grid1.TextMatrix(1, 1) = "ZONE"

Grid1.TextMatrix(0, 2) = "NO OF GRIDS"
Grid1.TextMatrix(1, 2) = "NO OF GRIDS"
Grid1.WordWrap = True

Grid1.TextMatrix(0, 3) = "NO OF SCENE"
Grid1.TextMatrix(1, 3) = "NO OF SCENE"
Grid1.WordWrap = True

Grid1.TextMatrix(0, 4) = "PRINTING STATUS"
Grid1.TextMatrix(1, 4) = "PRINTING STATUS"
Grid1.WordWrap = True

Grid1.TextMatrix(0, 5) = "GIVEN FOR SURVEY"
Grid1.TextMatrix(1, 5) = "GIVEN FOR SURVEY"
Grid1.WordWrap = True

Now, I have to merge 3 columns of the first row in excel.
Grid1.TextMatrix(0, 6) = "DELIVERED"
Grid1.TextMatrix(1, 6) = "ANNEXURES"

Grid1.TextMatrix(0, 7) = "DELIVERED"
Grid1.TextMatrix(1, 7) = "MDB FILE"

Grid1.TextMatrix(0, 8) = "DELIVERED"
Grid1.TextMatrix(1, 8) = "A3 MAP"

And, after that, I have some more fields, which needs to be merged colum wise. I am not able to merge below columns. I have tried using many options, but all in vain. Please help on this one.

Grid1.TextMatrix(0, 9) = "SURVEY STATUS"
Grid1.TextMatrix(1, 9) = "SURVEY STATUS"
Grid1.WordWrap = True
Grid1.MergeCol(9) = True

Grid1.TextMatrix(0, 10) = "SLUM AREA"
Grid1.TextMatrix(1, 10) = "SLUM AREA"
Grid1.WordWrap = True

Grid1.TextMatrix(0, 11) = "SERVICE ID"
Grid1.TextMatrix(1, 11) = "SERVICE ID"
Grid1.WordWrap = True

Grid1.TextMatrix(0, 12) = "SP QC STATUS"
Grid1.TextMatrix(1, 12) = "SP QC STATUS"
Grid1.WordWrap = True

Grid1.TextMatrix(0, 13) = "SP ERRORFIX STATUS"
Grid1.TextMatrix(1, 13) = "SP ERRORFIX STATUS"
Grid1.WordWrap = True

Grid1.TextMatrix(0, 14) = "SP DIG STATUS"
Grid1.TextMatrix(1, 14) = "SP DIG STATUS"
Grid1.WordWrap = True

Grid1.TextMatrix(0, 15) = "DATA LINKING STATUS"
Grid1.TextMatrix(1, 15) = "DATA LINKING STATUS"
Grid1.WordWrap = True

Grid1.TextMatrix(0, 16) = "DATA MIGRATION STATUS"
Grid1.TextMatrix(1, 16) = "DATA MIGRATION STATUS"
Grid1.WordWrap = True

Grid1.TextMatrix(0, 17) = "FINAL STATUS"
Grid1.TextMatrix(1, 17) = "FINAL STATUS"
Grid1.WordWrap = True

Merging Cells In A Table In Word With VB6 (not VBA)
Alright, so I've nearly finished up a small program that takes information from an excel spreadsheet and copies the relevant information into a word table that is created on the fly. However, I've having a real difficulty with merging cells in word. Basically, I have situations where for a single column, up to four rows of information need to be merged, and within the same column there are unmerged cells. (for example, if X are merged cells and Y are unmerged, something like this)



I've created a 2D array that has numerical values in it to match which columns in my table need to be merged, but its just the merge command that I can't get to work.

This is what I'm using to actually merge cells.

With wdTable
.Cell(currRow, nIndex).Merge (.Cell(currRow + currDepth, nIndex))
End With

But I get an incompatible type error. What am I doing wrong?

About Merging Values Of 2 Cells Into A Listbox
Hi there.
Can someone here please advise me on how to display values of two cell in one listbox so that user can select the values and do something about them, say, select and delete them.

e.g. I got two data types '4' and '15/4/2004' in cell b1 and c1 and want to have them displayed in a listbox, looking like '4 (15/4/204)'. Can I do that?


Merging Cells In MS FlexGrid Control
I could not able to merge cells of a row in MS FlexGrid control. Please help me to solve this issue.

*Visually* Merging MSFlexgrid Cells
I would like to format an MSFlexgrid so that, for example, it contains a heading row where the cells span the width of 3 normal cells below it. Can that be done?

Problems Merging Cells For MSFlexGrid
I am trying to merge several columns of an MSFlexGrid and cannot seem to accomplish this. Here is my code:

TG.Row = 0

TG.MergeCells = flexMergeFree
TG.MergeCol(0) = True
TG.MergeCol(1) = True
TG.MergeCol(2) = True
TG.MergeCol(3) = True
TG.MergeCol(4) = True
TG.MergeCol(5) = True
TG.MergeCol(6) = True

Any Ideas???


Merging Cells In Datagrid Control
Hi Can anyone tell me how to merge cells in the datagrid control? I could do it with MSHFlexGrid but i need to use the datagrid for other i need to know how to merge cells in datagrid.


Can I Merge Cells In MSFlexGrid Without Merging Cols Below???
I want to merge som cells in my top row, in a flexgrid. How do I do that, without merging the columns below?


Have a nice weekend!


MSFlex Grid Cells Merging Using Some Condition
can we merge cells in MSFgrid control, by using some condition.

For example, I have created a MSFgrid, using 3 colums and 6 rows, in this, i need to merge 3rd column cells, by cheking condition in first column, suppose if I have 3 cells of same data in column one then i would like to merge respective 3 cells of third column.

Normally what happend is, when we say merge particular column, it is merging all the cells of that particular coulmn, upto same data.

so plz help me in this,

thanks in advance


** RESOLVED ** Merging Cells In Word Table
I know strictly speaking this is VBA rather than VB, but as we don't have a VBA forum ........

Anyone know how to merge 3 cells in a row, on a table with 6 columns, e.g. merge current cell with next 2 cells to the right ?

Every time I turn on the recorder it stops me selecting the 3 cells.
If I turn the recorder off it lets me select them !

Stoopid Word .... grrrr !

Any ideas anyone ?


Merging Cells In Word Document Table
Can any one tell me how to merge cells in a Word document table. I have been searching but i have not been able to find ant info on it

Does Merging Cells Affect Flexgrid Properties?
Hi. Do the MouseRow/MouseCol, Row/Col properties of the flexgrid return the row and column coordinates of merged cells where one clicks? I have a flexgrid with the first 3 columns (Col 0,1,2) as fixedcols and the first row (Row 0) as fixedrow. The cells in my first column are merged/grouped every 3 rows. That means, rows 1,2,3 of the first column are merged and so do rows 4,5,6; 7,8,9; and so on. When I click in the first column, of any row, the returned coordinates as well as the cell's text content are incorrect. In my other non-merged cells, the returned vales are correct. Does merging affect the above mentioned properties?

Merging Data Table Cells To A Master Document
This may be well beyond what is possible in Word but since the documents were created there and the input is by basic users I would like to stay in Word. I have a master document created with several tables that match separate corresponding Word documents. These separate files are filed out in the field with information. I would like to take all of these separate documents to a summary report specifically to corresponding tables and cells in the master. A twist would be to eliminate the rows were no information is typed. Is this possible?

MSFlexGrid - Merging Cells Vs. Multiple Column/row Selection
I have an interesting question that I am wondering if anyone can help me solve.

I am working with an MSFlexGrid ActiveX control, and I want to be able to merge cells in the control
as long as the cells contain identical data. No problem. I also want to be able to allow the user to
select multiple rows and columns in the flexgrid. Here comes the problem:

According to the Microsoft Help Guide, when the MergeCells property of the flexgrid is set to anything other than
0 (Never), selection highlighting is automatically turned off.

Although this isn't that major of a deal (I would rather have cell merging as opposed to multiple selection), I was
wondering if anyone out there knew of a way to get around this problem? Is there a way to allow cells to merge
and allowing multiple row/column selection at the same time?

Mshflexgrid: Get Values Of Cells Or Row Numbers That Relate To Merged Cells
I am using a mshflexgrid and the first 2 columns are merged. So I have 8 rows all with a value for a material code and a description that is identical and for those 8 rows I have different percentages for all my different sizes. Now when a user clicks on a row I want to get all the percentages and sizes for the material. Is there any way to get the start and end rows for a merged cells so that I can get the adjacent values that apply to the current material.
Any help is (as always) appreciated...

"The answer is out there, Neo, it’s looking for you. And it will find you, if you want it to."--Trinity to Neo

Filling Cells With Formulae That Refer To Data In Other Cells
I'm trying to create a macro that uses a DDE function. I need to fill a row with formulae that take in information from other cells, and combine with command "winros".

For instance, given 4 colums and 3 rows (the following is meant to represent a spreadsheet):

I need to fill out Row 3 with formulae that combine the values in the rows above it. e.g., Row 3, Column A would show:


I need to combine =winros| with the value in cell A1 (two rows above), then with !', then with the value in cell A2 (one row above), then with '

However, I do not know how to do this How do I fill a row of cells with formulae that refer to other cells?

Filling Empty Cells In Column With Former Cells Data
What I am trying to do is shown below


Alpha One
[Blank Cell]
[Blank Cell]
[Blank Cell]
Beta Two
[Blank Cell]
[Blank Cell]
Charlie Three
[Blank Cell]
Delta Four
Epsilon Five
[Blank Cell]

Alpha One
Alpha One
Alpha One
Alpha One
Beta Two
Beta Two
Beta Two
Charlie Three
Charlie Three
Delta Four
Epsilon Five
Epsilon Five

It would be great if I can select the range to be processed for the column so that it doesn't go loopy. Any help would be greatly appreciated.


In Word, Check # Of Cells In Current Row, Then Select First Two Cells In Row
I'm trying to do a little VBA subroutine that will run in Word 2003 (VB 6.5), when insertion point is in a table.
What I want it to do is:
* Confirm the insertion point is in a table
* Go to the first row in current table
* Check if the number of cells in that row >= 5
   * If so, merge the first two cells in that row
   * If not, go to the next row
* Continue checking (and merging, if appropriate) to end of the table

I know how to check if insertion pt in in a table and how to merge two cells once they're selected, but am totally stumped on the middle part -- the real heart of the matter... (I've tried a bunch of things, using VBA Help info as a guide, but to no avail.)

* How do I determine the number of cells in the current row?
* How do I select the first two cells in current row?

Really would appreciate any help/guidance.

Edited by - topaz2 on 1/20/2007 8:44:46 PM

Looping Through Cells For Empty Cells Dynamically - Excel VBA - Need Help
I need to run a SQL like 'Select * from xyz where xyz.column1 in {all entries from cell A1: A10}
The criteria here - cells in column A would be loaded dynamically and I would not know how many columns are there (it could be say, A1 to A10 or A1 to A 40). So basically I need to start at A1 , increment column by 1 and look for empty cells. When an empty cell is found, all values from A1 to the one before the empty cell would be included in the where query.
i would like to achieve this and would need your help in getting this done.

Move Non-formula Cells In A Range Up 3 Cells

I want to write a macro to move a range (D34:L44) of cells up by three rows in a excel sheet if
a) they do not contain a formula
b) if they are non-zeros and numeric
I need to repeat this for all worksheets (about 30) that start with the letter 'A' , 'C', or 'P'
I would like to be able to execute this by macro, saving a lot of manual boredom!!
Any idea's ?
Thanks in advance

Combining Text In Cells Based On Other Cells
I have the following problem that I need to solve and VB seems the only way to do it.

I have 3 columns of information in Excel, I need to combine the text in column C based on if the information is the same in column A and then the same again in column B.

example: if a has 3 cells with the number 1 in them and column B has 2 cells with # 5 in them then Column C for the 2 matches needs to combine. and I need to add a comma after each string of text.

Thanks for your help with this.


Merge Cells Depending On Cells' Contents
I have two columns like

column D E

line 5 ABC 1
line 6 [empty] 2
line 7 [empty] 3
line 8 [empty] 4
line 9 ABC 1
line 10 [empty] 2
line 11 [empty] 3
line 60 ABC 1
line 61 [empty] 2
line 62 [empty] 3
line 63 [empty] [empty]
line 64 [empty] [empty]

I need to merge each cell containg text (ABC) with the empty cells below. My goal is to have some big cell with (ABC) in column D and all the cells with (1), (2), (3) in column E ... so that when I filter on column E, I don't loose the info on clumn D. (i.e. if I filter looking for (2) on column E, I will not be able to see the corresponding text (ABC) for each (2) value found by the filter)

Some facts:

* The number of empty cells below each text cell (ABC) is not constant - can be 0 as well as 100.
* The number of total text cells in column D is not constant.

My problem is that I do not know visual basic at all...
What I was thinking of in terms of algorithm is:


variable CELL var1
variable CELL var2

var1 = D5
var2 = D6

while(var1 is not empty)

while(var2 is empty)
end while


var1 = var2

end while

Can it be translated into VB???


Difference Between Cells.value And Cells.formula?
Hello, Everyone,
I used cells.value and cells.formula interchangely. would someone be kind to let me know the difference? and which is faster process? What's needed to notice?
Thank you very much!

How Could I Create Cells Like Excel Cells
how could i create cells like excel cells

is there any tool do this , cause i want to make in my form a table like excel spread sheats

Merging PNG's
Is there a way to first load a couple of different pngs, render them on eachother and then save them as pngs again with alpha channels still working. I've tried to copy them on to a picture box and save them with GDI+, but as they travel to the picture box they seem to become bitmaps and lose their alpha channel properities. Is there a way to merge (layer them on eachother) a couple of png images and then save again through VB either through DirecX or something else.

Is it possible to render them on a picturebox/form through dx8 and then save the renderd screen as png somehow?

Help's really apriciated. Thanks

Merging RTF?
How would you merge two RichTextBoxes??

Also, If anyone know of a good detailed description on how RTF works that would be great.


hi guys...

can u help me.. idont know what to do..
im begineer in vb6, i want to create a project wherein 2 csv file will merge and will have an output in excel or in access...

can u gave me an example so that i can figure it out what commands will i going to used..

any help will be very appreciated...

thank you...

hi guru's in vb...

can u help me.. idont know what to do..
im begineer in vb6, i want to create a project wherein 2 csv file will merge and will have an output in excel...

can u gave me an example so that i can figure it out what commands will i going to used..

any help will be very appreciated...

thank you...

Copyright 2005-08, All rights reserved