Search code examples
vb.netexcelmergeoffice-interop

vb.net excel merging cells confusion


Hey all I am trying to wrap my head around how to go about merging 2 cells together though it seems easy, I can't seem to get a grasp of how its calling the cells, rows, columns, etc from my code below:

Dim mergeCell As Excel.Range = xlsSheet.Range("B" & i & ":B" & i)
mergeCell.Range(mergeCell.Cells(0, 0), mergeCell.Cells(1, 1)).Merge()

'i' above would be 4 in this case below. My original sheet looks like this:

enter image description here

Where I want to merge A4/A5 with B4/5. Likewise, I will need to be also able to merge A8 with B8, etc etc.

However, when I run that code it merges A6/A7 with B6/B7:

enter image description here

When I use this code before the merge:

mergeCell.Select()

It seems to outline the .1 cell:

enter image description here

Can someone make since of the mergeCell.Cells(0, 0), mergeCell.Cells(1, 1) code?


Solution

  • Seems that I finally figured it out:

    xlsSheet.Range("A" & i & ":B" & (i + 1)).MergeCells = True
    

    But I would like to see the example from you, @Zaggler.