+ Reply to Thread
Results 1 to 13 of 13

Column width based on Heading Value

  1. #1
    Forum Contributor
    Join Date
    11-12-2012
    Location
    Jeddah, Saudi Arabia
    MS-Off Ver
    2010, 2013, 2016, Office 365
    Posts
    507

    Column width based on Heading Value

    hi,

    How can I adjust the column width based on cell value which is in row 8. I have the below value in Column A:Last Column. Header name will be the same but column not fixed

    Category Emp Code Employee Name Beneficiary Name Bank Account Number SWIFT Code Country Code Net - USD Net - SAR Net - USD


    If cell value is "Category" then Column width = 20
    If cell value is "Emp Code" then Column width = 8
    and so on


    Thanks

  2. #2
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Column width based on Heading Value

    Assume there were 8 columns. s is array of 8 width.
    PHP Code: 
    Option Explicit
    Sub test
    ()
    Dim si&
    = Array(208101112131415' assume there were 8 widths of 8 columns
        For i = 0 To UBound(s) - 1
            Cells(1, i + 1).ColumnWidth = s(i)
        Next
    End Sub 
    Quang PT

  3. #3
    Forum Contributor
    Join Date
    11-12-2012
    Location
    Jeddah, Saudi Arabia
    MS-Off Ver
    2010, 2013, 2016, Office 365
    Posts
    507

    Re: Column width based on Heading Value

    Thanks bebo, as I have explained sometimes column header values will be in different columns.

    Example if "Country Code" is in Column G, next time it will be in F or some other columns.

  4. #4
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Column width based on Heading Value

    If this does not work then only way is to see sample sheet...
    Assumes headers in Row 8 and they exist in Array
    Add as many headers as required...Change red numbers to header column width requirement...
    Please Login or Register  to view this content.
    Last edited by sintek; 12-30-2021 at 06:12 AM.
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  5. #5
    Forum Contributor
    Join Date
    11-12-2012
    Location
    Jeddah, Saudi Arabia
    MS-Off Ver
    2010, 2013, 2016, Office 365
    Posts
    507

    Re: Column width based on Heading Value

    @sintek,

    Thanks, I am getting Type mismatch error in the below line

    Please Login or Register  to view this content.
    Attached Files Attached Files

  6. #6
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Column width based on Heading Value

    That's obvious...
    See post 4
    Assumes headers in Row 8 and they exist in Array
    Add as many headers as required...Change red numbers to header column width requirement...
    You did not follow instructions...
    Where is other Headers and their corresponding required column widths...
    Bank Account Number SWIFT Code Country Code Net - USD Net - SAR

  7. #7
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,224

    Re: Column width based on Heading Value

    The following macro will only widen defined columns. If other columns appear, their width will not be changed.
    Please Login or Register  to view this content.
    Artik
    Last edited by Artik; 12-30-2021 at 07:12 AM.

  8. #8
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Column width based on Heading Value

    Add like this...By the way your Net Headers have leading spaces...
    Please Login or Register  to view this content.
    Attached Files Attached Files

  9. #9
    Forum Contributor
    Join Date
    11-12-2012
    Location
    Jeddah, Saudi Arabia
    MS-Off Ver
    2010, 2013, 2016, Office 365
    Posts
    507

    Re: Column width based on Heading Value

    Quote Originally Posted by sintek View Post
    That's obvious...
    See post 4
    Assumes headers in Row 8 and they exist in Array


    You did not follow instructions...
    Where is other Headers and their corresponding required column widths...
    Bank Account Number SWIFT Code Country Code Net - USD Net - SAR
    Sorry, you are right. I have forgot to add the values in Array.

    Thanks again for a excellent solution.

  10. #10
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Column width based on Heading Value

    Glad I could contribute...Tx for rep +

  11. #11
    Forum Contributor
    Join Date
    11-12-2012
    Location
    Jeddah, Saudi Arabia
    MS-Off Ver
    2010, 2013, 2016, Office 365
    Posts
    507

    Re: Column width based on Heading Value

    @Artik,

    Thanks, Excellent. Its working.

  12. #12
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Column width based on Heading Value

    As per Artik ...
    If other columns appear, their width will not be changed.
    Code amendment...
    Please Login or Register  to view this content.

  13. #13
    Forum Contributor
    Join Date
    11-12-2012
    Location
    Jeddah, Saudi Arabia
    MS-Off Ver
    2010, 2013, 2016, Office 365
    Posts
    507

    Re: Column width based on Heading Value

    @ Sintek,

    Thanks for the amendment

+ 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. Replies: 8
    Last Post: 03-29-2020, 01:00 PM
  2. Calculate standard deviation for column based on column heading
    By mattadler22 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-06-2013, 04:25 PM
  3. Adding Column values based on Column Heading
    By sjhanson in forum Excel General
    Replies: 1
    Last Post: 11-17-2011, 02:58 PM
  4. Finding column Heading based on max row value
    By Sandies in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-25-2007, 03:51 AM
  5. [SOLVED] Set Column Width Based On Total Width Of Other Columns
    By rayneraingoaway in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-28-2006, 06:10 PM
  6. Offset based on column heading
    By achidsey in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-30-2005, 09:10 PM
  7. Replies: 1
    Last Post: 11-13-2005, 05:15 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