Hi everyone,
i had a vba code that finds the longest string in a column and its location contains only "1"s.
It works at the sheet named "datasheet" (first sheet) but at the other sheets like "calc" and "charts" its useless.
What is the problem with this code? Additionally, i was having problem with the activating the sheet, it could be the reason? 
Option Explicit
Sub macro1()
Dim lastRow As Long, elem
Dim myCount As Long, myRow
Dim nrCells As Long
Application.ScreenUpdating = False
With ThisWorkbook.ActiveSheet
.AutoFilterMode = False
lastRow = .Cells(Rows.Count, "a").End(xlUp).Row
.Range("a:a").AutoFilter Field:=1, Criteria1:="<>"
myCount = 0
For Each elem In Range("a1:a" & lastRow).SpecialCells(xlCellTypeVisible).Areas
nrCells = elem.Cells.Count
If nrCells > myCount Then
myCount = nrCells
myRow = elem.Cells(1).Row
End If
Next elem
.AutoFilterMode = False
End With
Application.ScreenUpdating = True
MsgBox "Max number is: " & myCount & " at rows: " & myRow _
& "-" & myRow + myCount - 1
End Sub
here is a example file of it.
example.xlsx
Thanks in advance
Bookmarks