PDA

View Full Version : Merging cells from a row object



Digilee
06-22-2011, 12:30 PM
I am trying to do a simple merge of two cells in a row as follows


Dim nRow as row
. . .
Set newRow = .Rows.Add ' Need a new table row
nRow.Cells(1).Merge( nRow.Cells(2))

When I execute this, I get a type mismatch error, so I am guessing that row.cells does not return a cell object. I'm not sure why, because it is an index into a cell collection.

(Obviously this is a snippet; I am using a with srcTable statement above all this)
(Help)

Frosty
06-22-2011, 01:10 PM
newRow and nRow are typos?

Do you have Option Explicit at the top of your module (the answer should *always* be yes).

Digilee
06-22-2011, 02:21 PM
Frosty,

Yes, I use explicit. - this was just a typed in example of what I am doing, and yes, newRow == nRow

Digilee
06-22-2011, 02:59 PM
Never mind, I solved it.

I tend to put parenthesis around the arguments of a function no matter what ('C' background), so I forgot that

nRow.Cells(1).Merge( nRow.Cells(2))

is not the same as

nRow.Cells(1).Merge nRow.Cells(2)

oops.

Frosty
06-22-2011, 02:59 PM
Always dangerous to just type the code in. Maybe it's your parentheses? This stubby works, if I have my selection in a table.

Sub xyzzy()
Dim nRow As Row
Dim oTable As Table

Set oTable = Selection.Tables(1)
With oTable
Set nRow = .Rows.Add ' Need a new table row
nRow.Cells(1).Merge nRow.Cells(2)
End With
End Sub

Digilee
06-22-2011, 03:02 PM
Frosty,

I guess our last replies collided. You are right about posting code by just typing it in is dangerous, but I had the parenthesis in my real code too...

Anyway, I'm not sure I will ever get it right when to use the parenthesis and when not to use them

Frosty
06-22-2011, 03:22 PM
Posted at the same time.

Yeah... you can put Call in front of everything and always use the extra parens, but I kind of find that annoying too.

Syntax problems are my least favorite programming problems. You have my sympathies on that kind of annoyance. Glad it's solved.

Frosty
06-22-2011, 03:23 PM
Oh... despite my occasional irritation at what seems inconsistent, there is a logic.

If you're using the function to return a value (as well as whatever else it does), you need the parens. Otherwise you don't. Unless you put Call in front of the statement.

See, easy peasy.

:-D