Solved

Resizing Columns

Posted on 2011-02-23
8
1,217 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
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
 
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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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 400 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 41

Assisted Solution

by:dlmille
dlmille earned 100 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now