SOLVED

Adding condition to existing function

%3CLINGO-SUB%20id%3D%22lingo-sub-2400185%22%20slang%3D%22en-US%22%3EAdding%20condition%20to%20existing%20function%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2400185%22%20slang%3D%22en-US%22%3E%3CP%3EI%20hope%20to%20get%20some%20help%20regarding%20the%20function%20from%20experts.%3C%2FP%3E%3CP%3EIn%20the%20below%20function%201%20is%20represent%20to%20Col%221%22%20and%202%20is%20represent%20to%20Col%222%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20been%20using%20this%20function%20and%20its%20working%20fine%20but%20when%20it%20does%20not%20find%20any%20relevant%20strings%20from%20cater%20to%20Col%221%22%20then%20it%20stops.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20trying%20to%20add%20one%20condition%20in%20below%20function%20that%20is%20if%20Col%221%22%20strings%20are%20not%20matched%20with%20cater%20strings%20then%20there%20are%20strings%20with%20the%20name%20of%20%22Permanent%22%20in%20the%20Col%221%22%20so%20the%20function%20will%20go%20for%20the%20%22Permanent%22%20along%20with%20the%20Condit%2C%202.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20string%20matches%20then%20same%20below%20function%20will%20work.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EOriginal%20Function%3C%2FSTRONG%3E%3C%2FP%3E%3CPRE%3E%3CSPAN%20class%3D%22hljs-keyword%22%3EPublic%20%3CSPAN%20class%3D%22hljs-keyword%22%3EFunction%20GetRowNo_ByCaterAndCondit(Cater%20%3CSPAN%20class%3D%22hljs-keyword%22%3EAs%20%3CSPAN%20class%3D%22hljs-type%22%3EString%2C%20Condit%20%3CSPAN%20class%3D%22hljs-keyword%22%3EAs%20%3CSPAN%20class%3D%22hljs-type%22%3EString)%20%3CSPAN%20class%3D%22hljs-keyword%22%3EAs%20%3CSPAN%20class%3D%22hljs-type%22%3ELong%0A%20%20%20%20%0A%20%20%20%20GetRowNo_ByCaterAndCondit%20%3D%20usedfunctions.GetRowNoTwoColumns(%20_%0A%20%20%20%20%20%20%20%20ThisWorkbook.Sheets(%3CSPAN%20class%3D%22hljs-string%22%3E%22Sheet1%22)%2C%20Cater%2C%20%3CSPAN%20class%3D%22hljs-number%22%3E1%2C%20Condit%2C%20%3CSPAN%20class%3D%22hljs-number%22%3E2)%0A%20%20%20%20%20%20%20%20%0A%3CSPAN%20class%3D%22hljs-keyword%22%3EEnd%20%3CSPAN%20class%3D%22hljs-keyword%22%3EFunction%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FPRE%3E%3CP%3EI%20would%20appreciate%20it%20if%20you%20could%20help%20me%20with%20this.%20Thanks%3C%2FP%3E%3CP%3EI%20am%20trying%20but%20its%20not%20seems%20to%20be%20working.%3C%2FP%3E%3CPRE%3E%3CSPAN%20class%3D%22hljs-keyword%22%3EPublic%20%3CSPAN%20class%3D%22hljs-keyword%22%3EFunction%20GetRowNo_ByCaterAndCondit(Permanent%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eas%20%3CSPAN%20class%3D%22hljs-type%22%3Estring%2C%20Cater%20%3CSPAN%20class%3D%22hljs-keyword%22%3EAs%20%3CSPAN%20class%3D%22hljs-type%22%3EString%2C%20Condit%20%3CSPAN%20class%3D%22hljs-keyword%22%3EAs%20%3CSPAN%20class%3D%22hljs-type%22%3EString)%20%3CSPAN%20class%3D%22hljs-keyword%22%3EAs%20%3CSPAN%20class%3D%22hljs-type%22%3ELong%0A%20%20%20%20%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3EIf%20GetRowNo_ByCaterAndCondit%20%3D%20usedfunctions.GetRowNoTwoColumns(%20_%0A%20%20%20%20%20%20%20%20ThisWorkbook.Sheets(%3CSPAN%20class%3D%22hljs-string%22%3E%22Sheet1%22)%2C%20Cater%2C%20%3CSPAN%20class%3D%22hljs-number%22%3E1%2C%20Condit%2C%20%3CSPAN%20class%3D%22hljs-number%22%3E2)%0A%0A%3CSPAN%20class%3D%22hljs-comment%22%3E'if%20not%20macthed%20these%20Cater%2C%201%20then%0A%0A%20%20%20%20GetRowNo_ByCaterAndCondit%20%3D%20usedfunctions.GetRowNoTwoColumns(%20_%0A%20%20%20%20%20%20%20%20ThisWorkbook.Sheets(%3CSPAN%20class%3D%22hljs-string%22%3E%22Sheet1%22)%2C%20permanent%2C%20%3CSPAN%20class%3D%22hljs-number%22%3E1%2C%20Condit%2C%20%3CSPAN%20class%3D%22hljs-number%22%3E2)%0A%3CSPAN%20class%3D%22hljs-keyword%22%3EEnd%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eif%0A%3CSPAN%20class%3D%22hljs-keyword%22%3EEnd%20%3CSPAN%20class%3D%22hljs-keyword%22%3EFunction%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FPRE%3E%3CP%3E%3CSTRONG%3EHere%20is%20the%20GetRowNoTwoColumns%20function%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%3CSPAN%20class%3D%22hljs-keyword%22%3EPublic%20%3CSPAN%20class%3D%22hljs-keyword%22%3EFunction%20GetRowNoSearchTwoColumns(Sht%20%3CSPAN%20class%3D%22hljs-keyword%22%3EAs%20Worksheet%2C%20_%0A%20%20%20%20StringToFind1%20%3CSPAN%20class%3D%22hljs-keyword%22%3EAs%20%3CSPAN%20class%3D%22hljs-type%22%3EString%2C%20ColumnNumber1%20%3CSPAN%20class%3D%22hljs-keyword%22%3EAs%20%3CSPAN%20class%3D%22hljs-type%22%3EInteger%2C%20_%0A%20%20%20%20StringToFind2%20%3CSPAN%20class%3D%22hljs-keyword%22%3EAs%20%3CSPAN%20class%3D%22hljs-type%22%3EString%2C%20ColumnNumber2%20%3CSPAN%20class%3D%22hljs-keyword%22%3EAs%20%3CSPAN%20class%3D%22hljs-type%22%3EInteger)%20%3CSPAN%20class%3D%22hljs-keyword%22%3EAs%20%3CSPAN%20class%3D%22hljs-type%22%3ELong%0A%20%20%20%20%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3EOn%20%3CSPAN%20class%3D%22hljs-keyword%22%3EError%20%3CSPAN%20class%3D%22hljs-keyword%22%3EGoTo%20GetRowNoSearchTwoColumns_CleanUp_ErrorCheck%0A%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3EDim%20OldReferenceStyle%20%3CSPAN%20class%3D%22hljs-keyword%22%3EAs%20XlReferenceStyle%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3EDim%20SheetUsedRange%20%3CSPAN%20class%3D%22hljs-keyword%22%3EAs%20Range%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3EDim%20LastRow%20%3CSPAN%20class%3D%22hljs-keyword%22%3EAs%20%3CSPAN%20class%3D%22hljs-type%22%3ELong%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3EDim%20LastCol%20%3CSPAN%20class%3D%22hljs-keyword%22%3EAs%20%3CSPAN%20class%3D%22hljs-type%22%3EInteger%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3EDim%20CompleteRange%20%3CSPAN%20class%3D%22hljs-keyword%22%3EAs%20Range%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3EDim%20StrFormula%20%3CSPAN%20class%3D%22hljs-keyword%22%3EAs%20%3CSPAN%20class%3D%22hljs-type%22%3EString%0A%20%20%20%20%0A%20%20%20%20OldReferenceStyle%20%3D%20Application.ReferenceStyle%0A%20%20%20%20Application.ReferenceStyle%20%3D%20xlR1C1%0A%20%20%20%20%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3ESet%20SheetUsedRange%20%3D%20Sht.UsedRange%0A%20%20%20%20LastRow%20%3D%20SheetUsedRange.Row%20%2B%20SheetUsedRange.Rows.Count%20-%20%3CSPAN%20class%3D%22hljs-number%22%3E1%0A%20%20%20%20LastCol%20%3D%20SheetUsedRange.Column%20%2B%20SheetUsedRange.Columns.Count%20-%20%3CSPAN%20class%3D%22hljs-number%22%3E1%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3ESet%20CompleteRange%20%3D%20Sht.Range(Sht.Cells(%3CSPAN%20class%3D%22hljs-number%22%3E1%2C%20%3CSPAN%20class%3D%22hljs-number%22%3E1)%2C%20Sht.Cells(LastRow%2C%20LastCol))%0A%20%20%20%20%0A%20%20%20%20StrFormula%20%3D%20%3CSPAN%20class%3D%22hljs-string%22%3E%22%3DMATCH(%22%22%22%20%26amp%3B%20StringToFind1%20%26amp%3B%20%3CSPAN%20class%3D%22hljs-string%22%3E%22%22%22%26amp%3B%22%22%22%20%26amp%3B%20StringToFind2%20%26amp%3B%20%3CSPAN%20class%3D%22hljs-string%22%3E%22%22%22%2C%22%20%26amp%3B%20_%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-string%22%3E%22'%22%20%26amp%3B%20Sht.Name%20%26amp%3B%20%3CSPAN%20class%3D%22hljs-string%22%3E%22'!%22%20%26amp%3B%20CompleteRange.Columns(ColumnNumber1).Address(ReferenceStyle%3A%3DxlR1C1)%20%26amp%3B%20%3CSPAN%20class%3D%22hljs-string%22%3E%22%26amp%3B%22%20%26amp%3B%20_%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-string%22%3E%22'%22%20%26amp%3B%20Sht.Name%20%26amp%3B%20%3CSPAN%20class%3D%22hljs-string%22%3E%22'!%22%20%26amp%3B%20CompleteRange.Columns(ColumnNumber2).Address(ReferenceStyle%3A%3DxlR1C1)%20%26amp%3B%20%3CSPAN%20class%3D%22hljs-string%22%3E%22%2C0)%22%0A%20%20%20%20%0A%20%20%20%20GetRowNoSearchTwoColumns%20%3D%20Application.Evaluate(StrFormula)%0A%20%20%20%20%0A%3CSPAN%20class%3D%22hljs-symbol%22%3EGetRowNoSearchTwoColumns_CleanUp_ErrorCheck%3A%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-comment%22%3E'%20CleanUp%0A%20%20%20%20Application.ReferenceStyle%20%3D%20OldReferenceStyle%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3ESet%20SheetUsedRange%20%3D%20%3CSPAN%20class%3D%22hljs-literal%22%3ENothing%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3ESet%20CompleteRange%20%3D%20%3CSPAN%20class%3D%22hljs-literal%22%3ENothing%0A%20%20%20%20%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3EIf%20Err.Number%20%26lt%3B%26gt%3B%20%3CSPAN%20class%3D%22hljs-number%22%3E0%20%3CSPAN%20class%3D%22hljs-keyword%22%3EThen%0A%20%20%20%20%20%20%20%20GetRowNoSearchTwoColumns%20%3D%20%3CSPAN%20class%3D%22hljs-number%22%3E0%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3EEnd%20%3CSPAN%20class%3D%22hljs-keyword%22%3EIf%0A%3CSPAN%20class%3D%22hljs-keyword%22%3EEnd%20%3CSPAN%20class%3D%22hljs-keyword%22%3EFunction%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2400185%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2401407%22%20slang%3D%22en-US%22%3ERe%3A%20Adding%20condition%20to%20existing%20function%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2401407%22%20slang%3D%22en-US%22%3EWhy%20would%20you%20want%20to%20do%20this%20using%20a%20slow%20UDF%2C%20rather%20than%20placing%20the%20formula%20in%20question%20directly%20in%20the%20cell%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2401505%22%20slang%3D%22en-US%22%3ERe%3A%20Adding%20condition%20to%20existing%20function%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2401505%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F945050%22%20target%3D%22_blank%22%3E%40AHelper%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20would%20second%20the%20question%20asked%20by%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F22322%22%20target%3D%22_blank%22%3E%40Jan%20Karel%20Pieterse%3C%2FA%3E%20%3A%20why%20are%20you%20writing%20a%20routine%20to%20set%20a%20user%20defined%20function.%20The%20great%20likelihood%20is%20that%20the%20same%20result%20could%20be%20achieved--more%20quickly%20and%20reliably--just%20be%20using%20built-in%20functions.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMy%20guess%20is%20that%20you%20have%20a%20programming%20background%20and%20therefore%20find%20writing%20your%20own%20routines%20satisfying.%20In%20reviewing%20the%20posts%20you've%20started%20here%20at%20techcommunity%20I%20do%20note%20that%20the%20majority%20(if%20not%20the%20totality)%20concern%20VBA%20or%20macro%20questions.%20It's%20perfectly%20legitimate%20to%20write%20routines%2C%20of%20course.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBack%20in%20the%201990s%20I%20inherited%20responsibility%20for%20a%20monthly%20report%20that%20went%20to%20the%20top%20management%20of%20my%20corporation%2C%20a%20report%20that%20summarized%20employee%20headcounts%20by%20division%2C%20location%2C%20payclass%2C%20etc.%20When%20I%20inherited%20it%2C%20it%20was%20being%20produced%20(in%20Lotus%201-2-3)%20by%20means%20of%20an%20elaborate%20Basic%20program%20based%20on%20an%20SQL%20extraction%20of%20data.%20I%20took%20the%20raw%20data%20extract%20and%20used%20built-in%20functions%20in%20the%20spreadsheet%20to%20produce%20the%20same%20report.%20Much%20faster--virtually%20instantaneous%2C%20compared%20to%2020%20to%2030%20minutes.%20And%20every%20bit%20as%20reliable.%20The%20IT%20person%20who%20had%20created%20the%20former%20method%20had%20not%20done%20anything%20wrong%3B%20it%20just%20was%26nbsp%3B%20a%20mindset%20that%20came%20in%20to%20the%20task%20thinking%20%22this%20is%20such%20a%20big%20task%20that%20it%20surely%20requires%20a%20program%20to%20be%20written.%22%20I%20came%20into%20it%20thinking%2C%20%22this%20is%20just%20a%20task%20summarizing%20data%2C%20and%20spreadsheets%20are%20made%20for%20that%2C%22%20coupled%20with%20the%20fact%20that%20I%20had%2C%20in%20those%20days%2C%20a%20lot%20of%20fun%20reading%20the%20(printed)%20user%20manuals%20and%20discovering%20the%20wealth%20of%20power%20that%20was%20present%20in%20the%20built-in%20functions.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThat%20power%20has%20extended%20a%20LOT%20in%20the%20intervening%2025%20years--Excel%20contains%20an%20incredible%20array%20of%20functions--so%20my%20default%20of%20seeking%20to%20accomplish%20tasks%20without%20macros%2FVBA%20is%20still%20the%20approach%20I%20recommend.%20Will%20I%20ever%20write%20(or%20record)%20a%20short%20VBA%20routine%20to%20accomplish%20a%20repetitive%20task%3F%20Yes.%20But%20it's%20not%20my%20default.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhether%20this%20has%20any%20relevance%20to%20your%20situation%20is%20up%20to%20you....I%20just%20wanted%20to%20add%20it%20as%20reinforcement%20to%20Jan's%20question.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2401914%22%20slang%3D%22en-US%22%3ERe%3A%20Adding%20condition%20to%20existing%20function%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2401914%22%20slang%3D%22en-US%22%3EYes%20i%20know%20thins%20can%20be%20achieve%20directly%20through%20excel%20formula%20but%20this%20functions%20has%20been%20created%20by%20developer%20who%20was%20working%20for%20the%20organisation%20before%20me%20but%20i%20want%20to%20change%20these%20functions%20to%20single%20code%20or%20two%20code%20solution.%3CBR%20%2F%3E%3CBR%20%2F%3EBecause%20i%20am%20unable%20to%20understand%20his%20writings%20i%20really%20do%20not%20know%20why%20he%20created%20separate%20functions.%3CBR%20%2F%3E%3CBR%20%2F%3EI%20just%20need%20to%20add%20IF%20condition%20to%20as%20i%20stated%20above.%3CBR%20%2F%3E%3CBR%20%2F%3EFormula%20cannot%20be%20used%20because%20sheet%20is%20already%2040MB%20no%20single%20formula%20is%20used.%20File%20has%20just%20codes%2C%20functions%2C%20classes%20and%20user-forms.%3C%2FLINGO-BODY%3E
Contributor

I hope to get some help regarding the function from experts.

In the below function 1 is represent to Col"1" and 2 is represent to Col"2"

 

I have been using this function and its working fine but when it does not find any relevant strings from cater to Col"1" then it stops.

 

I am trying to add one condition in below function that is if Col"1" strings are not matched with cater strings then there are strings with the name of "Permanent" in the Col"1" so the function will go for the "Permanent" along with the Condit, 2.

 

If string matches then same below function will work.

 

Original Function

Public Function GetRowNo_ByCaterAndCondit(Cater As String, Condit As String) As Long
    
    GetRowNo_ByCaterAndCondit = usedfunctions.GetRowNoTwoColumns( _
        ThisWorkbook.Sheets("Sheet1"), Cater, 1, Condit, 2)
        
End Function

I would appreciate it if you could help me with this. Thanks

I am trying but its not seems to be working.

Public Function GetRowNo_ByCaterAndCondit(Permanent as string, Cater As String, Condit As String) As Long
    
    If GetRowNo_ByCaterAndCondit = usedfunctions.GetRowNoTwoColumns( _
        ThisWorkbook.Sheets("Sheet1"), Cater, 1, Condit, 2)

'if not macthed these Cater, 1 then

    GetRowNo_ByCaterAndCondit = usedfunctions.GetRowNoTwoColumns( _
        ThisWorkbook.Sheets("Sheet1"), permanent, 1, Condit, 2)
End if
End Function

Here is the GetRowNoTwoColumns function

 

Public Function GetRowNoSearchTwoColumns(Sht As Worksheet, _
    StringToFind1 As String, ColumnNumber1 As Integer, _
    StringToFind2 As String, ColumnNumber2 As Integer) As Long
    
    On Error GoTo GetRowNoSearchTwoColumns_CleanUp_ErrorCheck

    Dim OldReferenceStyle As XlReferenceStyle
    Dim SheetUsedRange As Range
    Dim LastRow As Long
    Dim LastCol As Integer
    Dim CompleteRange As Range
    Dim StrFormula As String
    
    OldReferenceStyle = Application.ReferenceStyle
    Application.ReferenceStyle = xlR1C1
    
    Set SheetUsedRange = Sht.UsedRange
    LastRow = SheetUsedRange.Row + SheetUsedRange.Rows.Count - 1
    LastCol = SheetUsedRange.Column + SheetUsedRange.Columns.Count - 1
    Set CompleteRange = Sht.Range(Sht.Cells(1, 1), Sht.Cells(LastRow, LastCol))
    
    StrFormula = "=MATCH(""" & StringToFind1 & """&""" & StringToFind2 & """," & _
        "'" & Sht.Name & "'!" & CompleteRange.Columns(ColumnNumber1).Address(ReferenceStyle:=xlR1C1) & "&" & _
        "'" & Sht.Name & "'!" & CompleteRange.Columns(ColumnNumber2).Address(ReferenceStyle:=xlR1C1) & ",0)"
    
    GetRowNoSearchTwoColumns = Application.Evaluate(StrFormula)
    
GetRowNoSearchTwoColumns_CleanUp_ErrorCheck:
    ' CleanUp
    Application.ReferenceStyle = OldReferenceStyle
    Set SheetUsedRange = Nothing
    Set CompleteRange = Nothing
    
    If Err.Number <> 0 Then
        GetRowNoSearchTwoColumns = 0
    End If
End Function

 

4 Replies
Why would you want to do this using a slow UDF, rather than placing the formula in question directly in the cell?

@Valiant 

 

I would second the question asked by @Jan Karel Pieterse : why are you writing a routine to set a user defined function. The great likelihood is that the same result could be achieved--more quickly and reliably--just be using built-in functions.

 

My guess is that you have a programming background and therefore find writing your own routines satisfying. In reviewing the posts you've started here at techcommunity I do note that the majority (if not the totality) concern VBA or macro questions. It's perfectly legitimate to write routines, of course.

 

Back in the 1990s I inherited responsibility for a monthly report that went to the top management of my corporation, a report that summarized employee headcounts by division, location, payclass, etc. When I inherited it, it was being produced (in Lotus 1-2-3) by means of an elaborate Basic program based on an SQL extraction of data. I took the raw data extract and used built-in functions in the spreadsheet to produce the same report. Much faster--virtually instantaneous, compared to 20 to 30 minutes. And every bit as reliable. The IT person who had created the former method had not done anything wrong; it just was  a mindset that came in to the task thinking "this is such a big task that it surely requires a program to be written." I came into it thinking, "this is just a task summarizing data, and spreadsheets are made for that," coupled with the fact that I had, in those days, a lot of fun reading the (printed) user manuals and discovering the wealth of power that was present in the built-in functions.

 

That power has extended a LOT in the intervening 25 years--Excel contains an incredible array of functions--so my default of seeking to accomplish tasks without macros/VBA is still the approach I recommend. Will I ever write (or record) a short VBA routine to accomplish a repetitive task? Yes. But it's not my default.

 

Whether this has any relevance to your situation is up to you....I just wanted to add it as reinforcement to Jan's question.

Yes i know thins can be achieve directly through excel formula but this functions has been created by developer who was working for the organisation before me but i want to change these functions to single code or two code solution.

Because i am unable to understand his writings i really do not know why he created separate functions.

I just need to add IF condition to as i stated above.

Formula cannot be used because sheet is already 40MB no single formula is used. File has just codes, functions, classes and user-forms.
best response confirmed by Valiant (Contributor)
Solution
The normal formula you would need is something like:
=MATCH(A1&B1&C1,Sheet1!$A$2:$A$1000&Sheet1!$B$2:$B$1000&Sheet1!$C$2:$C$1000,0)
Where the three conditions are in A1, B1 and C1 and the columns to look them up are columns A, B and C on Sheet1, rows 2 to 1000.
Your argument that a formula cannot be used sounds strange. A 40 MB workbook isn't that special if most of it is just plain data.