+ Reply to Thread
Results 1 to 5 of 5

Insert row with max length found in Column

  1. #1
    Registered User
    Join Date
    11-17-2006
    Posts
    5

    Insert row with max length found in Column

    Hi,

    I have a bunch of CSV or XLS files. I need to find the max length of what is in each column and insert it as a column header for each file. The number of columns per file vary as do the number of rows. This will be either added as the top CSV row or to XLS that will be converted to CSV.

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Try this macro
    It inserts a row at row 1
    places entry equal to number of rows & the word row. example a1 = 59 rows

    Sub AddHeader()
    Dim iCol As Integer
    Dim lRow As Long
    Dim iForCol As Integer

    Rows("1:1").Insert Shift:=xlDown
    iCol = .Cells.Find(what:="*", searchorder:=xlByColumns, _
    searchdirection:=xlPrevious).Column
    For iForCol = 1 To iCol Step 1
    lRow = Cells(Rows.Count, iCol).End(xlUp).Row
    Cells(1, iCol).Value = lRow & " Rows"
    Next iForCol
    End Sub

  3. #3
    Registered User
    Join Date
    11-17-2006
    Posts
    5
    Thank you. Your code did help get the ball rolling. This is what I have. Wondering if there is a more efficient way than looping through all the rows, brute force.

    ' Insert the row
    Rows("1:1").Insert Shift:=xlDown

    ' Get the max
    MaxCol = ActiveSheet.UsedRange.Columns.Count
    MaxRow = ActiveSheet.UsedRange.Rows.Count

    For ForCol = 1 To MaxCol Step 1
    MaxLen = 0

    For ForRow = 3 To MaxRow Step 1
    If Len(Cells(ForRow, ForCol).Value) > MaxLen Then
    MaxLen = Len(Cells(ForRow, ForCol))
    End If

    Next ForRow

    'WorksheetFunction.Range()
    'MaxLen = WorksheetFunction.Max( Cell(ForCol, 2)

    Cells(1, ForCol).Value = MaxLen

    Next ForCol

  4. #4
    Forum Contributor
    Join Date
    07-05-2006
    Location
    Canada
    MS-Off Ver
    2003, 2007
    Posts
    581
    Nope. You have to go through every cell that has information in it.

    This might give you a bit of a speed increase:

    Please Login or Register  to view this content.
    Scott

  5. #5
    Registered User
    Join Date
    11-17-2006
    Posts
    5
    Looping through the rows took way too long. I have modified the code to this. Which runs in a blink of an eye compared to before.

    Is there a way to specify Range() instead of me building it?


    MaxCol = objWorkSheet.UsedRange.Columns.Count
    MaxRow = objWorkSheet.UsedRange.Rows.Count

    For ForCol = 1 To MaxCol Step 1
    MaxLen = 0
    ColLetter = ConvertColumnNumberToLetter(ForCol)

    objWorkSheet.Cells(1, ForCol).FormulaArray = "=MAX(LEN(" + ColLetter + "3:" + ColLetter + CStr(MaxRow) + "))"

    Next ForCol

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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