+ Reply to Thread
Results 1 to 22 of 22

VB Code to expand column width

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    01-18-2007
    Location
    Georgia
    MS-Off Ver
    2010
    Posts
    4,434

    VB Code to expand column width

    Hello:

    Please refer to attached file.
    I need VB Code to expand the column width as needed.
    In example, i have data from column A thru R.
    As you can see i have column D, H ,K and R which needs to be expanded by VB Code.
    Let me know if you have any questions.
    Thanks.
    Riz
    Attached Files Attached Files

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: VB Code to expand column width

    How about just Ctrl+A, Format > Autofit column width. You could record a macro to do that, riz.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VB Code to expand column width

    If you want a code.

    Option Explicit
    
    Sub expandcolumns()
     Columns.AutoFit
    End Sub
    Or if you want to expand columns to a certain size, say 20

    Columns.ColumnWidth = 20
    Last edited by AB33; 07-30-2016 at 04:03 PM.

  4. #4
    Valued Forum Contributor
    Join Date
    01-18-2007
    Location
    Georgia
    MS-Off Ver
    2010
    Posts
    4,434

    Re: VB Code to expand column width

    Hello AB33:

    Using above code will reduce the other column width which is not desirable for me.
    Need to come up with logic to check the width of each column and see the ones which needs to be expanded.
    Please let me know if you have any questions.
    Thanks.

    Riz

  5. #5
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VB Code to expand column width

    Columns.AutoFit will adjust each column as needed.
    Need to come up with logic to check the width of each column and see the ones which needs to be expanded.
    which column/s and what size?

    Do you mean

    Option Explicit
    Sub expandcolumns()
    Application.ScreenUpdating = 0
    Dim i As Long
    
    With Columns("A:R")
        For i = 1 To .Columns.Count
            If .Columns(i).ColumnWidth < 8.43 Then
              .Columns(i).ColumnWidth = 8.43
            End If
         Next
     End With
     Application.ScreenUpdating = True
    End Sub
    ?
    I assumed the normal column size 8.43 and if I column is less than 8.43, the column will be adjusted to 8.43. You need to change the size as per your requirements.
    Last edited by AB33; 07-30-2016 at 05:22 PM.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: VB Code to expand column width

    Do you mean widen the columns that need it and leave the others alone?

    Sub riz()
      Dim rCol          As Range
      Dim dWid          As Double
    
      For Each rCol In ActiveSheet.UsedRange.EntireColumn
        dWid = rCol.ColumnWidth
        rCol.AutoFit
        If rCol.ColumnWidth < dWid Then rCol.ColumnWidth = dWid
      Next rCol
    End Sub

  7. #7
    Valued Forum Contributor
    Join Date
    01-18-2007
    Location
    Georgia
    MS-Off Ver
    2010
    Posts
    4,434

    Re: VB Code to expand column width

    Hello shg:

    Works great, thanks a lot.
    Would it be possible to add code in the above to change all columns of active sheet to certain column range of active sheeet.
    Example :

    Column D:K

    Please let me know if you have any questions.
    Thanks.



    Riz
    Last edited by rizmomin; 07-31-2016 at 09:26 AM.

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: VB Code to expand column width

    Quote Originally Posted by rizmomin View Post
    Would it be possible to add code in the above to change all columns of active sheet to certain column range of active sheeet.
    Example :

    Column D:K
    I don't know what that means, riz.

  9. #9
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: VB Code to expand column width

    Hello rizmomin,

    Add this line to the bottom of the code supplied by shg, just before End Sub:

    Columns("D:K").ColumnWidth = 25   'Change 25 to suit
    Regards.
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  10. #10
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: VB Code to expand column width

    @shg,

    Could it be possible that maybe Riz wants it as below?

    Option Explicit
    Sub riz()
      Dim rCol          As Range
      Dim dWid          As Double
    
      For Each rCol In ActiveSheet.Columns("D:K").EntireColumn
        rCol.AutoFit
        If rCol.ColumnWidth < dWid Then rCol.ColumnWidth = dWid
      Next rCol
      
    End Sub
    Kind Regards.

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: VB Code to expand column width

    You haven't initialized dWid, Winon.

  12. #12
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: VB Code to expand column width

    I did that on purpose shg, and testing the Code works fine on my side, even with the Workbook saved as Excel 2003.

    Please try it: for example,
    Attached Images Attached Images

  13. #13
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: VB Code to expand column width

    If you don't initialize it, it's zero, and a column's width is never less than zero, so the test doesn't do anything.

  14. #14
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: VB Code to expand column width

    Please try the attached sample Workbook.

    Best Regards.
    Attached Files Attached Files

  15. #15
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: VB Code to expand column width

    @ shg,

    If you copy and pastespecial formats from an unaffected Column into the Range D:K, and run the Macro it works on my side

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

    Re: VB Code to expand column width

    Just use shg's code on D:K
    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

  17. #17
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: VB Code to expand column width

    I tried, it Winon. Your code is equivalent to

      Columns("D:K").AutoFit
    ... is it not?

  18. #18
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: VB Code to expand column width

    @ xladept,

    It would appear to me that rizmomin wants to Zoom in on Range D:K only, and it is with that assumption, that if that is true, we don't need a loop in the Code.

    I guess we will have to wait for confirmation from Riz.

    Regards.

  19. #19
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: VB Code to expand column width

    @ shg,

    Yes, you are correct, and we therefore don't need a loop, if the assumption is correct.

    Riz stated;
    Would it be possible to add code in the above to change all columns of active sheet to certain column range of active sheeet.
    Example :

    Column D:K
    and you observation;

    If you don't initialize it, it's zero, and a column's width is never less than zero
    This is already been taken care of by Excels default "Controls", so why bother?

    The Loop free and "Reset" code would then be:

    Option Explicit
    Sub riz()
      
      ActiveSheet.Columns("D:K").EntireColumn.AutoFit
      
    End Sub
    Sub Reset()
    
    Columns("D:K").ColumnWidth = 8.34   'Change 8.34 to suit
    
    End Sub

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

    Re: VB Code to expand column width

    The autofit could make the column narrower - that's why I think shg's code is the way to go

  21. #21
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: VB Code to expand column width

    @ shg,

    ... it's zero, and a column's width is never less than zero
    Sorry shg, I neglected to mention that, if a column's width is zero, that Column will be hidden by default. in which case one would have to add Columns.Hidden=False to the "Reset Code.

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

    Re: VB Code to expand column width

    Hi Winon old friend,

    I'm taking a back seat now in this one - I hadn't thought of the possibility of zero width

+ 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] Let dropdown list width expand beyond width of column so people can read answer choices?
    By xdrenched in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 11-15-2023, 02:05 AM
  2. VBA code for grouping table column variable to collapse/expand?
    By xdrenched in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-08-2016, 10:48 AM
  3. VB Code to draw rectangle per data size
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-30-2016, 11:52 AM
  4. [SOLVED] Auto Expand or Contract Column Width as Data is Entered...
    By DeanExcel in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 02-13-2015, 07:10 AM
  5. [SOLVED] VB Code to delete column and change column Width
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-28-2014, 06:25 PM
  6. Replies: 2
    Last Post: 12-03-2013, 12:32 AM
  7. How to expand width of name/reference area in formula bar
    By Chris Kinata in forum Excel General
    Replies: 3
    Last Post: 08-12-2005, 07:05 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