« Add new sheet with automatically generated name: VBA Excel | Main
Wednesday
Jan312007

Find largest column width, and set all columns to greatest column width: VBA Excel

This is a recursive function:

          

Sub Main()

Call AdjustCols(Range("B2"), 1)

'B2 represents the first cell to be used, 1 represents the next column, C2.   

'$H$2 represents the last cell in your comparison   

end sub

Function AdjustCols(Currentcell As Range, nextCell As Integer)


    If Currentcell(1, 1).Offset(0, nextCell).Address= "$H$2" Then
        Cells.ColumnWidth = Currentcell.Cells(1, 1).ColumnWidth
        Exit Function


    ElseIf Currentcell.Cells(1, 1).ColumnWidth < Currentcell.Cells(1, 1)._

        Offset(0, nextCell).ColumnWidth Then
        Call AdjustCols(Currentcell.Cells(1, 1).Offset(0, nextCell), 1)


    Else
        nextCell = nextCell + 1
        Call AdjustCols(Currentcell.Cells(1, 1), nextCell)
    End If


End Function

Reader Comments (1)

Thank for your time to share us this discuss, i have some opinion on the <a href="http://www.dressaler.com/" title="Formal Dresses">Formal Dresses</a>, I feel so stronl about your article, and learning more on this topic, then it is so good, Do you have time visit us <a href="http://www.dressaler.com/wedding-party-dress/bridesmaid-dresses.html" title="Wedding Bridesmaid Dresses">Wedding Bridesmaid Dresses</a>.I will bookmark your blog and have my kids check up here frequently <a href="http://www.dressaler.com/wedding-party-dress/junior-bridesmaid-dresses.html" title="Junior Bridesmaid Dresses">Junior Bridesmaid Dresses</a>. I’m very certain they will understand lots of new stuff here than anybody else. I do appreciate that youve added relevant and intelligent commentary here though. Thank you!

January 5, 2012 | Unregistered CommenterFormal Dresses

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Textile formatting is allowed.