Search code examples
excelexcel-2007vba

How can I update text in a merged cell with VBA?


Suppose I have 2 cells that are merged, lets say Sheet1!$A$1:$B$1.

And lets say there's a piece of text that's centered across both cells. How do I programmatically update that value with VBA? I have a simple user form with a button, on button click, I want the cell to update.

I've tried grabbing the Range, but that doesn't seem to work.


Solution

  • If you have merged the cells, then you can only edit the value of the merged cells by referencing the upper left most cell.

    So, if A1 and B1 are merged, the only way to change the date is:

    Range("A1").value = "data"

    Or

    Cells(1,1).Value = "data"

    Calling Range("B1").Value = "data" will not change the visible text.