Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Resizing Columns

Posted on 2011-02-23
8
Medium Priority
?
1,226 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

610 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