+ Reply to Thread
Results 1 to 14 of 14

Compile Error. Sub of Function not Defined.

Hybrid View

momo123 Compile Error. Sub of... 07-14-2009, 11:56 PM
rylo Re: Compile Error. Sub of... 07-15-2009, 12:10 AM
D_Rennie Re: Compile Error. Sub of... 07-15-2009, 12:41 AM
momo123 Re: Compile Error. Sub of... 07-15-2009, 02:38 AM
romperstomper Re: Compile Error. Sub of... 07-15-2009, 03:07 AM
momo123 Re: Compile Error. Sub of... 07-15-2009, 03:18 AM
  1. #1
    Registered User
    Join Date
    07-14-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    32

    Compile Error. Sub of Function not Defined.

    Hi, Im really new to VBA Macro and i would appreciate all the help I can get from anyone.
    I have a range of amounts in Sheet 1 from F7:Q13 and im using the find method to search for the active and non active values in the cell. Which means that if there's a value in the cell it will transfer the value in Sheet 2, if nothing is found in the cell the cells in Sheet 2 will return as nothing or null.

    I think the problem lies on the FindWhat variable. Im getting a compiled error which im not sure what is it.

    I've attached the spreadsheet so you get a better idea of the problem that i encountered. Thanks soo much in advance!


    Sub TestFindAll()
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' TestFindAll
    ' This is a test procedure for FindAll.
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        
        Dim SearchRange As Range
        Dim FoundCells As Range
        Dim FoundCell As Range
        Dim FindWhat As Variant
        Dim MatchCase As Boolean
        Dim LookIn As XlFindLookIn
        Dim LookAt As XlLookAt
        Dim SearchOrder As XlSearchOrder
        
        ''''''''''''''''''''''''''
        ' Set the variables to the
        ' appropriate values.
        ''''''''''''''''''''''''''
        Set SearchRange = ThisWorkbook.Worksheets("Sheet 1").Range("F7:Q13")
      
        LookIn = xlValues
        LookAt = xlPart
        SearchOrder = xlByRows
        MatchCase = False
        
        '''''''''''''''''''
        ' Search the range.
        '''''''''''''''''''
        Set FoundCells = FindAll(SearchRange:=SearchRange, FindWhat:=FindWhat, _
            LookIn:=LookIn, LookAt:=LookAt, SearchOrder:=SearchOrder, MatchCase:=MatchCase)
        
        ''''''''''''''''''''''
        ' Display the results.
        ''''''''''''''''''''''
        If FoundCells Is Nothing Then
            Debug.Print "No cells found."
        Else
            For Each FoundCell In FoundCells.Cells
                Debug.Print FoundCell.Address, FoundCell.Text
            Next FoundCell
        End If
        
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Compile Error. Sub of Function not Defined.

    Hi

    You don't have any code for the function FindAll in this workbook. What is the code for this function?

    rylo

  3. #3
    Valued Forum Contributor
    Join Date
    05-14-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    843

    Re: Compile Error. Sub of Function not Defined.

    Try http://www.cpearson.com/excel/RangeFind.htm It has all the function examples for what you are doing.

  4. #4
    Registered User
    Join Date
    07-14-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    32

    Re: Compile Error. Sub of Function not Defined.

    Thanks for the replies.

    I've read the link that you attached but im still confuse as to how i can find the values in the cells. Im trying to use the Dim Array now but there's still error. Can you help me check the codes cos i don't know where have i gone wrong.

    Sub FindValuesOtherSheet()
        
        Dim SearchRange As Range
        Dim FoundCells As Range
        Dim FoundCell As Range
        Dim Rng As Range
        Dim MyArr As Variant
        Dim I As Long
        Dim MatchCase As Boolean
        Dim LookIn As XlFindLookIn
        Dim LookAt As XlLookAt
        Dim SearchOrder As XlSearchOrder
        
        'Fill in the search Value
        MyArr = Array("Current", "Balance")
        
        
        ''''''''''''''''''''''''''
        ' Set the variables to the
        ' appropriate values.
        ''''''''''''''''''''''''''
        
        Set SearchRange = ThisWorkbook.Worksheets("Sheet 1").Range("F7:Q13")
        
        Rcount = 0
    
            For I = LBound(MyArr) To UBound(MyArr)
    
        LookIn = xlValues
        LookAt = xlPart
        SearchOrder = xlByRows
        MatchCase = False
        
        '''''''''''''''''''
        ' Search the range.
        '''''''''''''''''''
     Set FoundCells = .Find(What:=MyArr(I), _
                                After:=.Cells(.Cells.Count), _
                                LookIn:=xlFormulas, _
                                LookAt:=xlPart, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False)
                                
                                
        ''''''''''''''''''''''
        ' Display the results.
        ''''''''''''''''''''''
              If FoundCells Is Nothing Then
                   Debug.Print "No cells found."
        
                    Do
                        Rcount = Rcount + 1
    
                        FoundCells.Copy NewSh.Range("Numbers" & Rcount)
                    Else
                          For Each FoundCell In FoundCells.Cells
                Debug.Print FoundCell.Address, FoundCell.Text
            Next FoundCell
        End If
        
       
    
        
    End Sub

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,009

    Re: Compile Error. Sub of Function not Defined.

    Firstly, if you get an error, it would help if you specified what error and on what line.
    The most obvious thing is that you use .Find but you don't have a With ...End With block. Change this:
     Set FoundCells = .Find(What:=MyArr(I), _
                                After:=.Cells(.Cells.Count), _
                                LookIn:=xlFormulas, _
                                LookAt:=xlPart, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False)
    to this:
     Set FoundCells = SearchRange.Find(What:=MyArr(I), _
                                After:=.Cells(.Cells.Count), _
                                LookIn:=xlFormulas, _
                                LookAt:=xlPart, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False)
    and see if that helps.
    Everyone who confuses correlation and causation ends up dead.

  6. #6
    Registered User
    Join Date
    07-14-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    32

    Re: Compile Error. Sub of Function not Defined.

    I've changed the code but the problem still lies on this line

    After:=.Cells(.Cells.Count), _

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,009

    Re: Compile Error. Sub of Function not Defined.

    Sorry, not enough coffee yet. Try this:
    Sub FindValuesOtherSheet()
        
        Dim SearchRange As Range
        Dim FoundCells As Range
        Dim FoundCell As Range
        Dim Rng As Range
        Dim MyArr As Variant
        Dim I As Long
        Dim MatchCase As Boolean
        Dim LookIn As XlFindLookIn
        Dim LookAt As XlLookAt
        Dim SearchOrder As XlSearchOrder
        
        'Fill in the search Value
        MyArr = Array("Current", "Balance")
        
        
        ''''''''''''''''''''''''''
        ' Set the variables to the
        ' appropriate values.
        ''''''''''''''''''''''''''
        
        Set SearchRange = ThisWorkbook.Worksheets("Sheet 1").Range("F7:Q13")
        
        Rcount = 0
    
       For I = LBound(MyArr) To UBound(MyArr)
    
        LookIn = xlValues
        LookAt = xlPart
        SearchOrder = xlByRows
        MatchCase = False
        
        '''''''''''''''''''
        ' Search the range.
        '''''''''''''''''''
       With SearchRange
          Set FoundCells = .Find(What:=MyArr(I), _
                                     After:=.Cells(.Cells.Count), _
                                     LookIn:=xlFormulas, _
                                     LookAt:=xlPart, _
                                     SearchOrder:=xlByRows, _
                                     SearchDirection:=xlNext, _
                                     MatchCase:=False)
                                     
       End With
        ''''''''''''''''''''''
        ' Display the results.
        ''''''''''''''''''''''
              If FoundCells Is Nothing Then
                   Debug.Print "No cells found."
             Else
                        Rcount = Rcount + 1
    
                        FoundCells.Copy NewSh.Range("Numbers" & Rcount)
                      For Each FoundCell In FoundCells.Cells
                Debug.Print FoundCell.Address, FoundCell.Text
            Next FoundCell
        End If
        
       Next I
    
        
    End Sub
    Note that the loop through FoundCells is pointless really since you will only ever have found one cell in each loop with this code.
    Last edited by romperstomper; 07-15-2009 at 03:33 AM.

+ 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