+ Reply to Thread
Results 1 to 12 of 12

Autofitting of columns not working as expected

  1. #1
    Registered User
    Join Date
    02-25-2014
    Location
    United States
    MS-Off Ver
    Excel 2013
    Posts
    46

    Autofitting of columns not working as expected

    I have a worksheet that is used to generate on-demand reports. Once a report is generated, I would like to auto-fit the columns so that all of the data is visible to the user. Currently, I have the following code written to do this:

    Please Login or Register  to view this content.
    However, when I run a report, the columns are not autofitted as I expected they would be. I do have a merged cell across the top of the report, but I removed it temporarily to test if that was the issue and the result is still the same. I have also tried only autofitting one of the wider columns (e.g. .Columns("C:C").EntireColumn.AutoFit) with no success. My understanding of the .AutoFit function is that it behaves in the same manner as double-clicking on a column positioner, but that does not seem to be the case.
    Last edited by Ezzard; 03-11-2015 at 09:01 AM.

  2. #2
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Autofitting of columns not working as expected

    You should be able to incorporate this into your macro, but I like to use...

    Please Login or Register  to view this content.
    HTH
    Regards, Jeff

  3. #3
    Registered User
    Join Date
    02-25-2014
    Location
    United States
    MS-Off Ver
    Excel 2013
    Posts
    46

    Re: Autofitting of columns not working as expected

    Jeff,

    I have just tried it with your suggested method as so:

    Please Login or Register  to view this content.
    ...but am still not achieving a look where all of the columns are fitted properly. I went through each of the columns and it seems as though most of them are auto-fitted and three of them (including the widest one) are not. Double-clicking on the columns that aren't fitted still works fine, so I'm stumped as to why the VBA auto-fitting is not working.

  4. #4
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Autofitting of columns not working as expected

    Without seeing a workbook sample, I'm not sure either. What if you did...

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    02-25-2014
    Location
    United States
    MS-Off Ver
    Excel 2013
    Posts
    46

    Re: Autofitting of columns not working as expected

    Jeff,

    Unfortunately, the workbook contains a considerable amount of sensitive information so I am not able to upload a sample at the moment. I did try running it using the two separate With..End With statements but achieved the same lack of results. I found a small number of other threads that had users with similar problems, but the only quasi-solution that I found (setting all column widths to a large value then auto-fitting) did not work either.

    I also tried making a separate subroutine as follows and including it in the same module as the subroutine that creates the report:

    Please Login or Register  to view this content.
    If I run the above macro on its own, everything works perfectly, but if I call the subroutine in my main macro then it still does not work properly.

  6. #6
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Autofitting of columns not working as expected

    Hi Ezzard,

    Sorry to hear you have not found a solution.

    Maybe I can place a call to the "real" VBA folks and they can offer a solution.

  7. #7
    Registered User
    Join Date
    02-25-2014
    Location
    United States
    MS-Off Ver
    Excel 2013
    Posts
    46

    Re: Autofitting of columns not working as expected

    Jeff,

    Any additional help would be greatly appreciated. If it helps, here is the code I currently have at the end of my macro after the report is generated (which shouldn't have any impact on whether or not auto-fitting works I would think) as well as the declarations I have for the variables included in that code:

    Please Login or Register  to view this content.
    There are no merged cells or wrapped text (both things that I have seen mentioned as preventing proper auto-fitting) anywhere on the report sheet and if I run the following macro with the sheet with the report active, the auto-fitting works exactly as it should.

    Please Login or Register  to view this content.

  8. #8
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Autofitting of columns not working as expected

    Hi Ezzard,

    Thank you for the additional code. I've run your code as is on a test sheet and it works as expected. All rows and columns autofitted with no problem.

    On those columns you mentioned are not autofitting correctly, and you sure there are no blank spaces at the end of the text which make the cell larger than it should be?

    I know you ran the autofit independenly and it worked, but have to ask.

    Also, have you setup an entirely new workbook with a small sample to test? What results do you get?

    We need to try an eliminate a bug from your existing sheet or is it the code. Again, the code as is ran fine for me on a test sheet.

  9. #9
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Autofitting of columns not working as expected

    It seems that something this simple should do the job:
    Please Login or Register  to view this content.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  10. #10
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Autofitting of columns not working as expected

    This also helps:

    Please Login or Register  to view this content.



  11. #11
    Registered User
    Join Date
    02-25-2014
    Location
    United States
    MS-Off Ver
    Excel 2013
    Posts
    46

    Re: Autofitting of columns not working as expected

    It appears as though the issue was having screen updating disabled when the code attempted to auto-fit the columns. If the auto-fit code is ran after screen updating is enabled, it works as intended. This would also explain why running a separate set of code to auto-fit the columns worked independently.

    Thanks Jeff and everyone else for the assistance.

  12. #12
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Autofitting of columns not working as expected

    You are very welcome. We are glad to have helped.

    Please mark your thread solved if your query has been satistied.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] CountIfS not working as expected
    By rschoenb in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 11-12-2014, 11:45 PM
  2. [SOLVED] VLOOKUP not working as expected.
    By mithandir1 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 01-17-2014, 06:44 PM
  3. Autofitting Columns
    By SQChevy in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 09-03-2008, 11:24 AM
  4. =NETWORKDAYS not working as expected and as it once did
    By USAOz in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-13-2006, 10:05 AM
  5. [SOLVED] For Each Statement not working as expected
    By Jacqui in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-02-2005, 06:17 PM

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