+ Reply to Thread
Results 1 to 13 of 13

Column width based on Heading Value

Hybrid View

  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,481

    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,336

    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...
    Sub J3v16()
    Dim Hdr, Chk, i As Long
    Hdr = [{"Category",20,"Emp Code",8, "Employee Name",10,"Beneficiary Name",25}]
    For i = 1 To Cells(8, Columns.Count).End(xlToLeft).Column
        Chk = Application.Match(Cells(8, i), Hdr, 0)
        Columns(i).ColumnWidth = Hdr(Chk + 1)
    Next i
    End Sub
    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

    Columns(i).ColumnWidth = Hdr(Chk + 1)
    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,336

    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 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.

  8. #8
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,246

    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.
    Sub AdjustColumnWidth()
      Dim varrHeaders As Variant
      Dim varrColsWidth As Variant
      Dim lColIdx As Long
      Dim i As Long
      
      varrHeaders = Split("Category,Emp Code,Employee Name,Beneficiary Name,Bank Account Number,SWIFT Code,Country Code,Net - USD,Net - SAR", ",")
      'Examples of column widths:
      'Category,Emp Code,Employee Name,Beneficiary Name,Bank Account Number,SWIFT Code,Country Code,Net - USD,Net - SAR
      '   20       8          25              25            30                  15          5           12        12
      varrColsWidth = Array(20, 8, 25, 25, 30, 15, 5, 12, 12)
      
      On Error Resume Next
      For i = 0 To UBound(varrHeaders)
        lColIdx = 0
        lColIdx = FindColumnInRange(varrHeaders(i), Rows(8))
        Columns(lColIdx).ColumnWidth = varrColsWidth(i)
      Next i
      
    End Sub
    
    
    Function FindColumnInRange(ByVal columnName As String, ByRef source As Range) As Long
        Dim rngHeaderData As Range
        Dim rngFind     As Range
    
        Set rngHeaderData = source.Resize(1)
    
        Set rngFind = rngHeaderData.Find(What:=columnName, After:=rngHeaderData.Cells(rngHeaderData.Count), _
                                         LookIn:=xlValues, LookAt:=xlWhole, _
                                         SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                                         MatchCase:=False, SearchFormat:=False)
    
        If Not rngFind Is Nothing Then
            FindColumnInRange = rngFind.Column
        Else
            Err.Raise 11110, , "Can't find column """ & columnName & """  in source "
        End If
    
    End Function
    Artik
    Last edited by Artik; 12-30-2021 at 07:12 AM.

  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

    @Artik,

    Thanks, Excellent. Its working.

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

    Re: Column width based on Heading Value

    Add like this...By the way your Net Headers have leading spaces...
    Sub J3v16()
    Dim Hdr, Chk, i As Long
    Hdr = [{"Category",20,"Emp Code",8, "Employee Name",10,"Beneficiary Name",25,"Bank Account Number",30,"SWIFT Code",5,"Country Code",12,"Net - USD",50,"Net - SAR",21}]
    For i = 1 To Cells(8, Columns.Count).End(xlToLeft).Column
        Chk = Application.Match(Cells(8, i), Hdr, 0)
        Columns(i).ColumnWidth = Hdr(Chk + 1)
    Next i
    End Sub
    Attached Files Attached Files

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

    Re: Column width based on Heading Value

    Glad I could contribute...Tx for rep +

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

    Re: Column width based on Heading Value

    As per Artik ...
    If other columns appear, their width will not be changed.
    Code amendment...
    If Not IsError(Chk) Then Columns(i).ColumnWidth = Hdr(Chk + 1)

  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. [SOLVED] 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