+ Reply to Thread
Results 1 to 14 of 14

Find method loop problem

  1. #1
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Find method loop problem

    Hi

    I have the following code to find the first occurance of a selected date (txtWeekCommencing)then check if that date corresponds to a selected department (Depot) then loop until both criteria are met.

    The code is reading the selected date but not working to check the department.

    What do you think is going wrong?

    Thanks

    Please Login or Register  to view this content.

  2. #2
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: Find method loop problem

    I should have added that there are similar dates for other departments but I want to search for a date AND a department.

    Any advice will be very welcome!

  3. #3
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Find method loop problem

    I want to search for a date AND a department.
    Need to set-up two find statements. If the first find is true then you can set up a second find.
    What is Depot?

    Or put an if condition once the find returns the first match.

  4. #4
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: Find method loop problem

    Thanks for your response AB33. I am new to VBA and have adapted the code unsuccessfully. Could you expand on your suggestion and advise where I should put the other find statement?

    Thanks

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,199

    Re: Find method loop problem

    Hi, newbi004,

    any particular reason you got away from the much faster version of using WorksheetFunction.CountIfs to using a User-Defined Function? BTW: why do you create and redim an array if you donīt use it?

    Iīd divided the first one liner If into 2 Ifs. And since only rngFind and the offset to that cell need to be considered no reason for a second Find.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  6. #6
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: Find method loop problem

    Thanks for that advice, Holger. I will try that out.

  7. #7
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,199

    Re: Find method loop problem

    Hi, newbi004,

    neither Depot nor txtWeekCommencing.Value are known in your Function. If you want to refer to a textbox on a UserForm make sure that the name of that form preceeds the name of the control. And Depot - might be a public Variable.

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Ciao,
    Holger

  8. #8
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: Find method loop problem

    Hi again Holger

    I am still struggling with this. I have a sheet called Bookings Form which holds dates in column A and associated data in columns C, D, E, F, and G. In column G is the department name (variable Depot).

    There could be several rows of data for a date and Depot. Also there could be several rows of data for another Depot for the same date. I was looking for the code to pick up all the data for a specific Depot for the selected date.

    The code I have written picks up the first Depot but not any other selected Depot. It's so frustrating as I am not particularly familiar with arrays so cannot see where I am going wrong.

  9. #9
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,199

    Re: Find method loop problem

    Hi, newbi004,

    it would be very good if you describe what you want to achieve. The function you posted doesnīt return the array but the number of entires that were found. And instead of using an UDF you could rely on an Autofilter - even if it may be difficult to flter for a Date (you must make sure the Date is in the american format and ushould use CLng to convert it to a long) you can filter for the Depot and then run the subset of the visible cells.

    If this wonīt help maybe it would be goof if you attach a workbook with some data and the UserForm to have a look at.

    Ciao,
    Holger

  10. #10
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: Find method loop problem

    Good morning Holger

    Thanks for coming back.

    The Module is very large and contains confidential information so will be difficult to send in open forums.

    The code below preceeds the function call:

    Please Login or Register  to view this content.
    The variable Depot is set here and the date is also set just before this.

    The Depot is set when user clicks a command button with that Depot name. The UDF works for the first time the date and the first named Depot is found. I have slightly changed the original UDF to:

    Please Login or Register  to view this content.
    However when another Depot button is clicked, the UDF finds the date but as the rngfnd is the first Depot found the code goes out at this line:

    Please Login or Register  to view this content.
    The code in the UDF is not finding another Depot.

    I hope this explains my problem?

    Cheers

  11. #11
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: Find method loop problem

    Good morning Holger

    Thanks for coming back.

    The Module is very large and contains confidential information so will be difficult to send in open forums.

    The code below preceeds the function call:

    Please Login or Register  to view this content.
    The variable Depot is set here and the date is also set just before this.

    The Depot is set when user clicks a command button with that Depot name. The UDF works for the first time the date and the first named Depot is found. I have slightly changed the original UDF to:

    Please Login or Register  to view this content.
    However when another Depot button is clicked, the UDF finds the date but as the rngfnd is the first Depot found the code goes out at this line:

    Please Login or Register  to view this content.
    The code in the UDF is not finding another Depot.

    I hope this explains my problem?

    Cheers

  12. #12
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: Find method loop problem

    sorry I clicked the send reply twice!

  13. #13
    Forum Contributor
    Join Date
    03-21-2012
    Location
    Ho Chi Minh city
    MS-Off Ver
    Excel 2003
    Posts
    180

    Re: Find method loop problem

    PHP Code: 
    'Option Explicit'
    Private Function m_DoFind(Data As Range) As Long
    ' Find all occurances and store addresses in array'
     
    Dim RngFind As RangeCls As RangeRng As RangeRg0 As Range
     Dim strFirstAddress 
    As String
     
     m_DoFind 
    0    ' No finds'
     
    ReDim m_strFindAddresses(m_DoFind) As String
     
    'run the search'
     
    With Data
        Set RngFind 
    = .Find(what:=txtWeekCommencing.ValueLookIn:=xlValuesLookAt:=xlPart_
            SearchOrder
    :=xlByColumnsSearchDirection:=xlNext)
        
    'Tf the item is found the user is informed and the textbox cleared'
        
    If Not RngFind Is Nothing Then
            Set Rng 
    Range(RngFindData(Data.Cells.Count))
            For 
    Each Cls In Rng
                
    If Cls.Offset(06) = Depot Then
                    
    If Rg0 Is Nothing Then
                        Set Rg0 
    Cls
                    
    Else
                        
    Set Rg0 Union(Rg0Cls)
                    
    End If
                
    End If
            
    Next Cls
        End 
    If
     
    End With
     m_DoFind 
    Rg0.Cells.Count
    End 
    Function 

  14. #14
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: Find method loop problem

    Thanks Sa DQ. Your code seems to move things forward, but how do I change the output code below to read what your code finds?

    Please Login or Register  to view this content.
    I am getting a Subscript out of range error at line

    Please Login or Register  to view this content.
    Thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Find Method Problem
    By Bob@Sun in forum Excel Programming / VBA / Macros
    Replies: 22
    Last Post: 03-28-2010, 04:11 PM
  2. With Loop + Find Method = Confused
    By hurricanopotamus in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-23-2010, 04:15 PM
  3. Find method loop with ColorIndex
    By yellephant in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-14-2008, 08:02 PM
  4. [SOLVED] Problem with Find Method
    By Edward Ulle in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-22-2005, 01:10 PM
  5. Problem with find method
    By Ralph Heidecke in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-18-2005, 04:06 PM

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