+ Reply to Thread
Results 1 to 2 of 2

My code to find longest string in column doesn't work properly! Need Help!!

Hybrid View

refree My code to find longest... 06-03-2013, 07:27 AM
refree Re: My code to find longest... 06-03-2013, 08:27 AM
  1. #1
    Registered User
    Join Date
    04-09-2013
    Location
    izmir,turkey
    MS-Off Ver
    excel 2007
    Posts
    4

    Talking My code to find longest string in column doesn't work properly! Need Help!!

    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
    Last edited by refree; 06-03-2013 at 08:28 AM.

  2. #2
    Registered User
    Join Date
    04-09-2013
    Location
    izmir,turkey
    MS-Off Ver
    excel 2007
    Posts
    4

    Re: My code to find longest string in column doesn't work properly! Need Help!!

    i've solved it by myself

    by adding Sheets("xx")

          Sheets("calc").AutoFilterMode = False
          lastRow = .Cells(Rows.Count, "a").End(xlUp).Row
          Sheets("calc").Range("a:a").AutoFilter Field:=1, Criteria1:="<>"
          myCount = 0
          For Each elem In Sheets("calc").Range("a1:a" & lastRow).SpecialCells(xlCellTypeVisible).Areas

+ 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