Link to home
Start Free TrialLog in
Avatar of nplanek
nplanekFlag for United States of America

asked on

Excel Top and Bottom Cell Padding

I'm trying to add cell padding on the top and bottom of all cells in an Excel 2007 worksheet.  I found a solution on Experts Exchange from Akoster, but it doesn't run for me:

Range([...]).EntireRow.AutoFit
Range([...]).RowHeight = Range([...]).RowHeight + 12
Range([...]).VerticalAlignment = xlCenter

The error is...Run-time error '1004'
Method 'Range' of object' _Global' failed.

What should I change?
Avatar of andrewssd3
andrewssd3
Flag of United Kingdom of Great Britain and Northern Ireland image

The solution looks fine - that error will be because the Range you specify is in some way invalid, either because it's a bad cell reference or name or because you don't have the right active sheet.  What is your code actually saying?  It should be something like
Range("A1:A34").EntireRow.AutoFit
Range("A1:A34").RowHeight = Range("A1:A34").RowHeight + 12
Range("A1:A34").VerticalAlignment = xlCenter

Open in new window

Or better,
Sub PadRows()

Dim rngPad As Range

    Set rngPad = Application.Range("A1:A34")

    With rngPad
        .EntireRow.AutoFit
        .RowHeight = Range("A1:A34").RowHeight + 12
        .VerticalAlignment = xlCenter
    End With

End Sub

Open in new window

Sorry missed one reference there:
Sub PadRows()

Dim rngPad As Range

    Set rngPad = Application.Range("A1:A34")

    With rngPad
        .EntireRow.AutoFit
        .RowHeight = .RowHeight + 12
        .VerticalAlignment = xlCenter
    End With

End Sub

Open in new window

The A1:A34 could be any range that includes all the rows you want.
Avatar of nplanek

ASKER

I have cells that have one, two or three lines.  The code seems to center the text vertically, because I can see the text in the one and two line cells move; I see no difference in the three line cell (it shows no padding).  Ideally I would like the text aligned at the bottom of the cell with padding at the top and bottom.  Possible?
ASKER CERTIFIED SOLUTION
Avatar of andrewssd3
andrewssd3
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nplanek

ASKER

I can work with this....thanks!
Thanks for the points.  Can you just say why you did not award an A here?  The grading guidelines http://www.experts-exchange.com/help/viewHelpPage.jsp?helpPageID=26 say you should award an A unless the answer is deficient in some way.  If it is I'd be happy to help you.
Avatar of nplanek

ASKER

I'm sure your solution was the best available, but didn't meet my needs perfectly.  I just needed the text aligned at the bottom, with padding on the top and bottom.