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.
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.
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?
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
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.
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.
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.
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:
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:
'Option Explicit' Private Function m_DoFind(Data As Range) As Long ' Find all occurances and store addresses in array' Dim RngFind As Range, Cls As Range, Rng As Range, Rg0 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.Value, LookIn:=xlValues, LookAt:=xlPart, _ SearchOrder:=xlByColumns, SearchDirection:=xlNext) 'Tf the item is found the user is informed and the textbox cleared' If Not RngFind Is Nothing Then Set Rng = Range(RngFind, Data(Data.Cells.Count)) For Each Cls In Rng If Cls.Offset(0, 6) = Depot Then If Rg0 Is Nothing Then Set Rg0 = Cls Else Set Rg0 = Union(Rg0, Cls) End If End If Next Cls End If End With m_DoFind = Rg0.Cells.Count End Function
Bookmarks