+ Reply to Thread
Results 1 to 4 of 4

Autofit columns in a range

Hybrid View

Harlort Autofit columns in a range 08-22-2011, 09:34 AM
romperstomper Re: Autofit columns in a range 08-22-2011, 09:48 AM
Harlort Re: Autofit columns in a range 08-22-2011, 09:52 AM
romperstomper Re: Autofit columns in a range 08-22-2011, 09:58 AM
  1. #1
    Registered User
    Join Date
    09-12-2010
    Location
    Stockholm, Sweden
    MS-Off Ver
    Excel 2003
    Posts
    41

    Autofit columns in a range

    I've done this simple code. But I want it to execute automatically?
    Sub autofit()
    
        Range("B4:B18").Columns.autofit
        Range("C4:C18").Columns.autofit
        Range("D4:D18").Columns.autofit
        Range("E4:E18").Columns.autofit
    
    End Sub
    It's in the VBA code of a specific sheet.

    Any suggestions (including improving existing code...I'm a new at this :-) )?

    Regards,
    Niklas
    Last edited by romperstomper; 08-22-2011 at 09:47 AM. Reason: code tags

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,974

    Re: Autofit columns in a range

    What do you mean by automatically? When do you want it to run?
    You can shorten it to:
    
    Sub autofit()
    
    Range("B4:E18").Columns.autofit
    
    End Sub
    Note: I have added code tags to your post, but you must learn to use them in future when posting code.
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Registered User
    Join Date
    09-12-2010
    Location
    Stockholm, Sweden
    MS-Off Ver
    Excel 2003
    Posts
    41

    Re: Autofit columns in a range

    Thanks for cleaning up my code.

    I want the column/columns to autofit as soon as data has been entered into a cell. Is it possible...?

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,974

    Re: Autofit columns in a range

    That should happen anyway, but you can put this into the worksheet code module (right-click the sheet tab and choose View Code, then paste it in)
    
    Private Sub Worksheet_Change(ByVal Target As Range)
       If Not Intersect(Target, Range("B4:E18")) Is Nothing Then
          Range("B4:E18").Columns.autofit
       End If
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1