Resize cell by VBA code

H

heenchi

I want to make a table with different columns widths on different row
using VBA code. The width of the whole table should also be fixed.

I've tried using the following code:

Selection.Cells.PreferredWidth = CentimetersToPoints(4.7)

It turns out that if I try to make the cell narrower nothing happen
and if I set it to be wider the whole table width will grow larger...

What I want to do is simple to do by using the mouse, then its just t
grab on to the cell border and drag it. The column width will the
resize and the whole table width will stay the same.

But how to do this in VBA code
 
D

Doug Robbins - Word MVP

If you have two cells selected in the one row, the following will increase
the width of the column in which the first cell is located by 20mm and
reduce the width of the column in which the second cell is located by the
same amount so that the oveall width of the table remains unchanged.

Selection.Columns(1).Width = Selection.Columns(1).Width +
MillimetersToPoints(20)
Selection.Columns(2).Width = Selection.Columns(2).Width -
MillimetersToPoints(20)

That's what in effect happens when you click on a border between to cells
and drag it with the mouse.

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
H

heenchi

Thanks for the help!

I use a different number of cells in different rows. Your suggestio
therefore generates an error saying that it isn't possible to addres
specific columns, since I use diffrent number of cells..

Maybe there is a similar way to do it, but I'm a novice, so if you hav
another solution I would gladly accept it!

/Henri
 
H

heenchi

Thanks for the help!

I use a different number of cells in different rows. Your suggestio
therefore generates an error saying that it isn't possible to addres
specific columns, since I use diffrent number of cells..

Maybe there is a similar way to do it, but I'm a novice, so if you hav
another solution I would gladly accept it!

/Henri
 
D

Doug Robbins - Word MVP

In that case use

Selection.Cells(1).Width = Selection.Cells(1).Width +
MillimetersToPoints(20)
Selection.Cells(2).Width = Selection.Cells(2).Width -
MillimetersToPoints(20)


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top