Thanks!
>> You want to avoid Variant declarations (the default) whenever possible as they are inefficient. <<
I tried doing that but just added "As Long" to the first Dim line. E.g.
Dim col, i, line As Long
It seemed I removed it because I was getting some error. Is the syntax wrong to do them at once like I was trying or is it something else? Or should that have worked?
>> And you want to avoid selecting objects like sheets. <<
Are you refering to the line below when you say selecting an object?
Worksheets("Sheet1").Activ
I just wanted to clarify because I wasn't really sure what you meant there, although it makes sense. Or was it not using With as you did to work with the worksheet?
Thanks! Let me look closer and try the changes? Any thoughts about the "side" questions? If I got carried away I can open a new question for them if I need an answer. I was just thinking they may be on topic and a quick answer so I included them.
bol
Main Topics
Browse All Topics





by: zorvekPosted on 2009-10-08 at 16:15:57ID: 25531201
Pretty good start. A few tweaks. I declare every variable on a separate line so I know what each is. You want to avoid Variant declarations (the default) whenever possible as they are inefficient. And you want to avoid selecting objects like sheets.
Sub getMaxLen()
Dim col As Long
Dim i As Long
Dim line As Long
Dim myRange As Range
With Worksheets("Sheet1")
Set myRange = .[A1].CurrentRegion
'MsgBox (myRange.Rows.Count & " - " & myRange.Columns.Count)
'Exit Sub
For col = 1 To myRange.Columns.Count
'MsgBox (Cells(1, col).Value)
'If Cells(1, col).Value = "" Then Exit Sub
i = 0
For line = 1 To myRange.Rows.Count
If Len(Cells(line, col).Value) > i Then i = Len(Cells(line, col).Value)
Next
.Cells((line + 1), col).Value = i
Next
End With
End Sub
Kevin