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?
nplanekAsked:
Who is Participating?
 
andrewssd3Connect With a Mentor Commented:
I'm not quite clear if this is what you want - it does add padding top and bottom, but the text is always vertically centered.  The other code did not work because if the rows in the range have different heights, .RowHeight returns Null, which cannot be used. So this variant takes it one row at a time (I've also changed it to use the Selection rather than a hard-coded range):
Sub PadRows()

Dim rngPad As Range
Dim r As Range

    Set rngPad = Selection

    For Each r In rngPad.Rows
        With r
            .EntireRow.AutoFit
            .RowHeight = .RowHeight + 12
            .VerticalAlignment = xlCenter
        End With
    Next r

End Sub

Open in new window

0
 
andrewssd3Commented:
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

0
 
andrewssd3Commented:
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.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
nplanekAuthor Commented:
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?
0
 
nplanekAuthor Commented:
I can work with this....thanks!
0
 
andrewssd3Commented:
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.
0
 
nplanekAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.