Solved

Resizing Columns

Posted on 2011-02-23
8
1,219 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

803 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