[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Excel Top and Bottom Cell Padding

Posted on 2012-09-05
7
Medium Priority
?
7,182 Views
Last Modified: 2012-09-07
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?
0
Comment
Question by:nplanek
  • 4
  • 3
7 Comments
 
LVL 17

Expert Comment

by:andrewssd3
ID: 38370149
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
 
LVL 17

Expert Comment

by:andrewssd3
ID: 38370163
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
 

Author Comment

by:nplanek
ID: 38372207
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 17

Accepted Solution

by:
andrewssd3 earned 1500 total points
ID: 38374052
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
 

Author Closing Comment

by:nplanek
ID: 38374146
I can work with this....thanks!
0
 
LVL 17

Expert Comment

by:andrewssd3
ID: 38375038
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
 

Author Comment

by:nplanek
ID: 38376215
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

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question