+ Reply to Thread
Results 1 to 2 of 2

Unable to get countif property of the worksheet function class

Hybrid View

  1. #1
    Registered User
    Join Date
    04-17-2008
    Location
    Yorkshire
    Posts
    79

    Unable to get countif property of the worksheet function class

    I keep getting the following error, when I run my macro:

    Runtime error '1004'
    Unable to get countif property of the worksheetfunction class

    Sub LinkList()
        Dim SearchRange As Range
        Dim FoundCells As Range
        Dim FoundCell As Range
        Dim CompareLikeString As String
        Dim SearchOrder As XlSearchOrder
        Dim MatchCase As Boolean
        Dim OutRng As Range
        Dim Orisht As Worksheet
        Dim stringform As String
        Dim stringform2 As String
        Dim sform3 As String
        Dim rscut As String
        Dim Rsf As String
    
    
    Set Orisht = Worksheets("LinkList")
    Set OutRng = Orisht.Range("A65536")
    
        For Each Worksheet In Worksheets
            With Worksheet
            Select Case Worksheet.Name
            
            Case "LinkList"
            GoTo nxtwks
                 
            End Select
            End With
        
    'search in this range in search worksheet
        Set SearchRange = Worksheet.Range("A1:BM3000")
        CompareLikeString = "*[[]*[]]*"
        SearchOrder = xlByRows
        MatchCase = True
        
    'find matching cells
        Set FoundCells = WildCardMatchCells(SearchRange:=SearchRange, CompareLikeString:=CompareLikeString, _
            SearchOrder:=SearchOrder, MatchCase:=MatchCase)
    
        If FoundCells Is Nothing Then
            'MsgBox "No cells in this worksheet" --Not in use
        Else
            For Each FoundCell In FoundCells
                
    stringform = FoundCell.Formula
    
    
    
    'trim stringform of cell references
        For x = 1 To 6
        Rsf = Right(stringform, x)
        rscut = Left(Rsf, 1)
        If rscut <> "!" Then GoTo Nxt:
        stringform2 = (Left(stringform, Len(stringform) - (x + 1)))
    Nxt:
        Next x
    
     'time off the  = so not recorded as a formula
     sform3 = Right(stringform2, Len(stringform2) - 2)
    
    'if the link has already been listed dont record again 
    If Application.WorksheetFunction.CountIf(Worksheets("LinkList").Range("A1:A65536"), sform3) > 0 Then GoTo skip
                
            OutRng.End(xlUp).Offset(1, 0).Value = _
            sform3
                
    skip:
            Next FoundCell
        End If
        
    nxtwks:
     Next Worksheet
    
    End Sub
    The macro searches all sheets for external links, and records unique entries the linklist worksheet. (original function from an online example I cant recall where).

    It works great where there is only 1 link in a cell. However it trips up when there are for instance link+link in the cell formula. It dies at the countif line with the above error.

    Does anyone have any idea why? While I'd prefer to leave the countif line in to remove duplicate entries, there wont be too many of these types of values in the workbooks anyway, so I tried to have it just skip the 'check if already listed' part if this error occured and just add the link to the list anyway (to be sorted out manually later) but i cant seem to make an error handler that works either.

    Ideas experts?!
    Last edited by mikeyfear; 05-23-2008 at 08:39 AM.

  2. #2
    Registered User
    Join Date
    04-17-2008
    Location
    Yorkshire
    Posts
    79
    I found a work around for the problem above by extracting my link from the left hand side of the string instead of the right. I could now extract the 2nd, 3rd, 4th link in a cell quite easily (as long as non of the linked cells are ranges!), by counting where each : is and trunkating the string at that value.

    The problem now is this is becoming a massive proccessor drain with all the loops. I'm lucky enough to be on a top spec core2duo machine, but even so it takess a good 3 mins of locked up pc to generate the results. Thats without extracting the multiple links in the cells.

    Is there anyway to speed this up?:

    Sub LinkList()
        Dim SearchRange As Range
        Dim FoundCells As Range
        Dim FoundCell As Range
        Dim CompareLikeString As String
        Dim SearchOrder As XlSearchOrder
        Dim MatchCase As Boolean
        Dim OutRng As Range
        Dim Orisht As Worksheet
        Dim stringform As String
        Dim stringform2 As String
        Dim sform3 As String
        Dim rscut As String
        Dim Rsf As String
    
    
    Set Orisht = Worksheets("LinkList")
    Set OutRng = Orisht.Range("A65536")
    
        For Each Worksheet In Worksheets
            With Worksheet
            Select Case Worksheet.Name
            
            Case "LinkList"
            GoTo nxtwks
            
             Case "RefList"
            GoTo nxtwks
            
            End Select
            End With
        
    'search in this range in search worksheet
        Set SearchRange = Worksheet.Range("A1:BM3000")
        CompareLikeString = "*[[]*[]]*"
        SearchOrder = xlByRows
        MatchCase = True
        
    'find matching cells
        Set FoundCells = WildCardMatchCells(SearchRange:=SearchRange, CompareLikeString:=CompareLikeString, _
            SearchOrder:=SearchOrder, MatchCase:=MatchCase)
    
        If FoundCells Is Nothing Then
            'MsgBox "No cells in this worksheet" --Not in use
        Else
            For Each FoundCell In FoundCells
                
    stringform = FoundCell.Formula
    coloncount = 0
    
    
    
     'trim off left up to ":" -2
        
        For x = 1 To 20
        Rsf = Left(stringform, x)
        rscut = Right(Rsf, 1)
        If rscut <> ":" Then GoTo Nxt2:
        stringform2 = Right(stringform, Len(stringform) - (x - 2))
        'coloncount = coloncount + 1
    'If coloncount > 1 Then MsgBox coloncount & FoundCell.Address & stringform
    'could be used to extract next occurance of : and next etc colon count > 1 means more than one link
    Nxt2:
    
        Next x
    
    
    
    'trim from right up to "'"
        For x = 1 To 150
        Rsf = Left(stringform2, x)
        rscut = Right(Rsf, 1)
        If rscut <> "!" Then GoTo Nxt:
        sform3 = Left(stringform2, x - 2)
    Nxt:
        Next x
    
    ' MsgBox sform3
    
    
     
     
     
    If Application.WorksheetFunction.CountIf(Worksheets("LinkList").Range("A1:A65536"), sform3) > 0 Then GoTo skip
                
            OutRng.End(xlUp).Offset(1, 0).Value = _
            sform3
                
    skip:
            Next FoundCell
        End If
        
    nxtwks:
     Next Worksheet
    
    End Sub
    Last edited by mikeyfear; 05-23-2008 at 06:57 PM.

+ 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