?
Solved

Resizing Columns

Posted on 2011-02-23
8
Medium Priority
?
1,225 Views
Last Modified: 2012-05-11
Is there a simple way to have particular columns automatically resize themselves (i.e. auto fit) based on data that is moved into a particular column via a macro or formula?  

B.
0
Comment
Question by:Bright01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 19

Expert Comment

by:elimesika
ID: 34960123
0
 
LVL 7

Expert Comment

by:David_Hagerman
ID: 34960236
i also reviewed the url above and there are some flaws when using these macro so if it doesn't work for you then here is a step by step guide to do it manually once the your data has been imported. this is quicker than selecting one column at a time

http://www.ehow.com/how_4445648_autofit-column-width-excel.html
0
 

Author Comment

by:Bright01
ID: 34960300
elimesika,

OK....

Here's what they recommend:

With Cells
    .WrapText = True
    .EntireColumn.AutoFit
    .EntireRow.AutoFit
End With

Do I put that in the spreadsheet code?  Do I identify the columns I want to autofit?

Please advise.

Thank you,

B.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:elimesika
ID: 34960591
HI

You can define a Macro containing this code and run it manually or from a program.
It will do that for all cells in document.

 
0
 

Author Comment

by:Bright01
ID: 35005664
David &/or elimesika,

Can you create a small sample of how this works?  A simple spreadsheet with a macro that when the sheet is opened or changed, it will auto resize the cells?

Thank you!

B.
0
 
LVL 58

Accepted Solution

by:
tigermatt earned 1600 total points
ID: 35043741

Bright01,

What we'll need to do is adapt the code provided by the previous Experts so that it is fired as part of an "event". Event programming is popular in most of the modern programming languages today -- essentially you subscribe an event - in this case, the "Change" event - to an object - in this case, the Excel worksheet. Whenever the action described by the event (a change to a cell) occurs, it fires the code associated with it.

I've performed a little testing and the code below is a very rudimentary way of doing what you are after:
Private Sub Worksheet_Change(ByVal Target As Range)
    MinRow = 3
    MaxRow = 6
    
    MinCol = 6
    MaxCol = 7
    
    For Each cell In Target
    
        ' Changed cell
        ActiveRow = cell.Row
        ActiveCol = cell.Column
        
        If ((ActiveRow >= MinRow And ActiveRow <= MaxRow) And (ActiveCol >= MinCol And ActiveCol <= MaxCol)) Then
        
            With cell
                .WrapText = False
                .EntireRow.AutoFit
                .EntireColumn.AutoFit
            End With
            
        End If
    
    Next
End Sub

Open in new window

The options at the very top of the code allow you to set the start and end rows and start and end columns for which the code should fire. For example, at the moment, the code will only do its autofit when I make changes to the cells in rows 3 to 6 of columns F and G. (The column count starts at column "A" being 1).

You add that code to the worksheet by right-clicking on the sheet tab at the bottom of the program and clicking "View Code". That will load the VBA editor where you can add the code.

I've attached an example spreadsheet which should demonstrate the code in action.
AutoFitOnChangeExample.xls

Please let me know if that helps you at all.

-Matt
0
 
LVL 42

Assisted Solution

by:dlmille
dlmille earned 400 total points
ID: 35044528
Here are a couple posts that do this...  One by Tommy:  http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_26781258.html

And one I solved, which also accounts for MERGED CELLS - at this post http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_26751022.html

I've attached that solution here.  You are prompted for merge approach and it fits all worksheets in the workbook.  PS - you don't have to have merged columns to use this.


Just run the Macro AutoFitAll()

Cheers,

Dave
AutoFit-Column-Merged-v2.xlsm
0
 

Author Comment

by:Bright01
ID: 35044723
Dave and Matt,

Thank you both for responding!  I have looked over both samples.  They are both very different.  Matt, your solution autofits when text is inputted and it auto adapts the column fit.  This is exactly what I was looking for.  Dave, your response is an auto fit for the entire workbook and all the worksheets.  While this is very interesting, it's not what I need.  I really appreciate both of you working on this.  I've accepted the solution.

Thank you,

B.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

765 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