Solved

Resizing Columns

Posted on 2011-02-23
8
1,216 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 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

11 Experts available now in Live!

Get 1:1 Help Now