+ Reply to Thread
Results 1 to 7 of 7

Autofit Rows Using Macro

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-02-2007
    Location
    Leeds, England
    MS-Off Ver
    MS Office 2007
    Posts
    134

    Autofit Rows Using Macro

    Hi Guys,

    I'm trying to Autofit a number of Rows within a macro.

    I need to autofit Rows 8 to 80 as the content of these rows will have been updated by lines earlier in the macro. Some rows will now contain more than 1 line of text whereas others will now be blank.

    I initially tried to use the code:

    Rows("8:80").EntireRow.AutoFit
    but this didn't work properly. Some rows were not as expanded as they needed to be.

    I then tried to write it like this:

    Sub Macro2()
        Rows("8:8").EntireRow.AutoFit
        Rows("9:9").EntireRow.AutoFit
        Rows("10:10").EntireRow.AutoFit
        Rows("11:11").EntireRow.AutoFit
        Rows("12:12").EntireRow.AutoFit
    End Sub
    however this would take me ages to write out and would also be a pain to edit if i needed to change the rows etc.

    Can anyone show me a solution to this problem and suggest why my first code did not work?

    Thanks
    dvent
    Last edited by dvent; 11-19-2008 at 07:32 AM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Try this
        Rows("8:80").Rows.AutoFit
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Contributor
    Join Date
    11-02-2007
    Location
    Leeds, England
    MS-Off Ver
    MS Office 2007
    Posts
    134
    Quote Originally Posted by royUK View Post
    Try this
        Rows("8:80").Rows.AutoFit
    With this I get:

    Run Time Error '1004'
    Method 'Rows' of object '_Global' failed.

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Remove the second Rows

     Rows("8:80").AutoFit

  5. #5
    Forum Contributor
    Join Date
    11-02-2007
    Location
    Leeds, England
    MS-Off Ver
    MS Office 2007
    Posts
    134
    cheers Roy!

  6. #6
    Registered User
    Join Date
    11-06-2012
    Location
    manila, philippines
    MS-Off Ver
    Excel 2007
    Posts
    1

    Re: Autofit Rows Using Macro

    i have the same problem. my question is where should i put the code?

  7. #7
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Autofit Rows Using Macro

    Hi, winoverme,

    welcome to the Forum.

    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.


    Regarding your question:
    Copy the Excel VBA code
    Select the workbook in which you want to store the Excel VBA code
    Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
    Choose Insert | Module
    Where the cursor is flashing, choose Edit | Paste

    To run the Excel VBA code:
    Choose Tools | Macro | Macros
    Select a macro in the list, and click the Run button
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

+ 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