Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Loop through a column to find the longest string

Posted on 2011-03-02
11
Medium Priority
?
1,623 Views
Last Modified: 2012-05-11
I need to loop through a column, check each field and determine its length and when compeleted, return from the function the value for the longest string.  I think I can figure out how to loop, but how to I tell it to return only the longest count of characters?  That is, if cell A15 has data that is 15 characters and cell A16 has 22 characters, I want the function to only return 22.
0
Comment
Question by:ssmith94015
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +3
11 Comments
 
LVL 22

Expert Comment

by:rspahitz
ID: 35020820
You can do this through formulas..in a blank column next to the text, such as B1 put =Len(A1)

then you you put a formula at the bottom e.g.: =Max(B1:B50)
0
 
LVL 8

Expert Comment

by:ragnarok89
ID: 35020832
Before your loop begins, have this:

max = 0

inside your loop, have this:

temp = Len(ActiveCell)
if temp > max then max = temp


Len returns the length of the string
0
 
LVL 24

Expert Comment

by:StephenJR
ID: 35020951
You could use an array formula - enter with ctrl+shift+enter:

=MAX(LEN(A1:A10))
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 33

Expert Comment

by:jppinto
ID: 35020957
On column B I putted a formula to get the lenght of column A value. Then on cell D1 I putted an array formula to get the value that corresponds to the value with the highest lenght on column A. To insert an array formula just type in the formula on the cell and instead of pressing just Enter key, Ctrl-Shift-Enter.

The formula will look like this:

{=INDEX(A1:A6,MATCH(MAX(LEN(A:A)),B1:B6,0))}

jppinto

Book2.xlsx
0
 

Author Comment

by:ssmith94015
ID: 35020982
ISorry,  did not mention, this must be all done in VBA code.  Also, I discovered that one field has over 300 characters, which should not be considered as it is a footnote.  Ragnarok89, I think this is what I am looking for, will try and get back.
0
 

Author Comment

by:ssmith94015
ID: 35021046
I have adapted the code incorporating some of the suggestions, but all the Debug.Print intMax returns is 0.  I think I am missing a point somewhere.
Public Sub LengthCountTest()
Dim rngCell             As Range
Dim intColTextLength    As Integer
Dim intRowCount         As Integer
Dim intColCount         As Integer
Dim aryColCount()       As Integer
Dim intColumns          As Integer
Dim intMax              As Integer
intMax = 0

'First set the print area to a named range
With ActiveSheet
    .Names.Add "ReportArea", _
        "=" & Range("Print_Area").Address
    intRowCount = .Range("ReportArea").Rows.Count
    Debug.Print intRowCount
    intColCount = .Range("ReportArea").Columns.Count
    Debug.Print intColCount
'End With

ReDim aryColCount(1 To intColCount)

intColumns = 1
    Do Until intColumns > intColCount
        With ActiveSheet
            For Each rngCell In Range("A1:A" & intRowCount)
                    intColTextLength = Len(ActiveCell)
                    If intColTextLength > intMax Then
                        intMax = intColTextLength
                    End If
                Debug.Print intMax
            Next
            intColumns = intColumns + 1
        End With
    Loop
End With

End Sub

Open in new window

0
 
LVL 23

Accepted Solution

by:
Michael Fowler earned 2000 total points
ID: 35021055
Here is a vba function that will do what you are after

It takes the Column letter as a string and returns the length of the longest string

Michael
Function maxInCol(col As String)
   Dim lastrow As Long, i As Long
   
   lastrow = Range(col & Rows.Count).End(xlUp).Row
   maxInCol = 0
   
   For i = 1 To lastrow
      If Len(Range(col & i).Value) > maxInCol Then maxInCol = Len(Range(col & i).Value)
   Next

End Function

Open in new window

0
 

Author Comment

by:ssmith94015
ID: 35021059
As a side note, evnetually the Rage("A1:A" & intRowCount) will be replaced by code that will move from one colum to another in the range, but right now I just want to get the first column to return data!
0
 
LVL 23

Expert Comment

by:Michael Fowler
ID: 35021124
Note - You can use the function above by placing it into a module and then calling it from the sheet by using

=maxInCol("A")

which will give the required result for column A

Michael
0
 

Author Closing Comment

by:ssmith94015
ID: 35021138
This is did it.  Thank you all.  I will be posting another question relative to the fact that I need it to ignore the Max value of 300 +.  The footnote is on different rows depending on the report so I cannot simply tell it to ignore a particular row.  Eventually, all the max values for each of the columns will populate an array, but that is about three questions down the road.
0
 
LVL 33

Expert Comment

by:jppinto
ID: 35021522
When asking a question, if you wanted VBA only, you should have mentioned at the beginning! For future reference, please don't forget it...
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question