SOLVED

SOLVED - Check based on whatever cell the User-Defined Formula (Function) is in - Set Target

%3CLINGO-SUB%20id%3D%22lingo-sub-2769077%22%20slang%3D%22en-US%22%3ESOLVED%20-%20Check%20based%20on%20whatever%20cell%20the%20User-Defined%20Formula%20(Function)%20is%20in%20-%20Set%20Target%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2769077%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3EUpdate%3A%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20figured%20it%20out.%20Working%3A%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Working.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F311692i172BECD20308F173%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Working.png%22%20alt%3D%22Working.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Working1.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F311693i3CC75289EEAAE9F9%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Working1.png%22%20alt%3D%22Working1.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Untitled2.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F311691i4943F005C20003E7%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Untitled2.png%22%20alt%3D%22Untitled2.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETo%20make%20the%20user-defined%20formula%20be%20dynamic%20to%20whatever%20cell%20the%20formula%20is%20in%3A%3C%2FP%3E%3CP%3E1)%20I%20needed%20to%20%3CU%3Enot%3C%2FU%3E%20declare%20a%20target%2C%20range%2C%20selection%2C%20etc.%20at%20all.%3C%2FP%3E%3CP%3E2)%20Define%20the%20function%20within%20%22With%20Application.Caller%26nbsp%3B%20%26nbsp%3B*CODE*%26nbsp%3B%20%26nbsp%3BEnd%20With%22%3C%2FP%3E%3CP%3E3)%20Make%20it%20behave%20as%20regular%20formulas%20would%20regarding%20calculation%20using%20%22Application.Volatile%22%3C%2FP%3E%3CP%3ESo%20basically%2C%20the%20%3CSTRONG%3Esolution%20is%20no%20definition%20of%20Target%20and%20.Caller%3C%2FSTRONG%3E.%3C%2FP%3E%3CP%3ECode%3A%3C%2FP%3E%3CP%3E(Of%20course%2C%20I%20also%20put%20in%20all%20the%20checks%20(IFs)%20specific%20to%20my%20need.)%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3EPublic%20Function%20PreviousYear_LastKnown_VALUE()%0A%0ADim%20FirstYear_Val%20As%20Integer%3A%20FirstYear_Val%20%3D%20Sheet4.Name%0AApplication.Volatile%20'This%20is%20to%20calculate%20this%20User-Defined%20formula%20automatically%20with%20the%20rest%20of%20the%20formulas%0AWith%20Application.Caller%20'This%20is%20to%20refer%20dynamically%20to%20the%20cell%20the%20formula%20is%20in%20(allow%20for%20%22.row%22)%0A%0AIf%20Not%20Worksheets(%22Troop%20to%20Task%20-%20Tracker%22).Range(%22D2%22).Value%20-%201%20%26lt%3B%20FirstYear_Val%20Then%20'Check%20if%20greater%20than%20or%20equal%20to%20first%20generated%20year%0A%0AIf%20Application.WorksheetFunction.CountIf(ThisWorkbook.Sheets(%22%22%20%26amp%3B%20Worksheets(%22Troop%20to%20Task%20-%20Tracker%22).%20_%0ARange(%22D2%22).Value%20-%201%20%26amp%3B%20%22%22).Cells%2C%20Worksheets(%22Troop%20to%20Task%20-%20Tracker%22).Range(%22E%22%20%26amp%3B%20.Row).%20_%0AOffset(0%2C%20Worksheets(%22Formula%20%26amp%3B%20Code%20Data%22).Range(%22C16%22).Value%20%2B%204).Formula2)%20%26gt%3B%200%20Then%20'Check%20if%20person%20exists%0A%0APrevYear_Rng%20%3D%20ThisWorkbook.Sheets(%22%22%20%26amp%3B%20Worksheets(%22Troop%20to%20Task%20-%20Tracker%22).Range(%22D2%22).Value%20-%201%20%26amp%3B%20%22%22).%20_%0ACells.Find(What%3A%3DWorksheets(%22Troop%20to%20Task%20-%20Tracker%22).Range(%22E%22%20%26amp%3B%20.Row%20%26amp%3B%20%22%3AE%22%20%26amp%3B%20.Row).%20_%0AOffset(0%2C%20Worksheets(%22Formula%20%26amp%3B%20Code%20Data%22).Range(%22C16%22).Value%20%2B%204).Formula2).Offset(-1%2C%20-1).Address%0A%0AIf%20ThisWorkbook.Sheets(%22%22%20%26amp%3B%20Worksheets(%22Troop%20to%20Task%20-%20Tracker%22).Range(%22D2%22).Value%20-%201%20%26amp%3B%20%22%22).Range(%20_%0APrevYear_Rng).Value%20%3D%20%22Staff%22%20Or%20ThisWorkbook.Sheets(%22%22%20%26amp%3B%20Worksheets(%22Troop%20to%20Task%20-%20Tracker%22).Range%20_%0A(%22D2%22).Value%20-%201%20%26amp%3B%20%22%22).Range(PrevYear_Rng).Value%20%3D%20%22CQ%22%20Then%20'Check%20if%20duty%20is%20last%20day%20of%20previous%20year%0A%0APreviousYear_LastKnown_VALUE%20%3D%20%22This'll%20be%201%20by%20default%22%20'1%0AElse%0APreviousYear_LastKnown_VALUE%20%3D%20_%0AThisWorkbook.Sheets(%22%22%20%26amp%3B%20Worksheets(%22Troop%20to%20Task%20-%20Tracker%22).Range(%22D2%22).Value%20-%201%20%26amp%3B%20%22%22).Range(PrevYear_Rng).Value%20%2B%201%0AEnd%20If%0AElse%0APreviousYear_LastKnown_VALUE%20%3D%20%22This'll%20be%201%20by%20default%22%20'1%0AEnd%20If%0AElse%0APreviousYear_LastKnown_VALUE%20%3D%20%22This'll%20be%201%20by%20default%22%20'1%0AEnd%20If%0AEnd%20With%0A%0A'CODE%20WITHOUT%20THE%20IFS%3A%0A'WS_MINUS_REF%20%3D%20ThisWorkbook.Sheets(%22%22%20%26amp%3B%20Worksheets(%22Troop%20to%20Task%20-%20Tracker%22).Range(%22D2%22).Value%20-%201%20%26amp%3B%20%22%22).%20_%0ACells.Find(What%3A%3DWorksheets(%22Troop%20to%20Task%20-%20Tracker%22).Range(%22E%22%20%26amp%3B%20.Row%20%26amp%3B%20%22%3AE%22%20%26amp%3B%20.Row).%20_%0AOffset(0%2C%20Worksheets(%22Formula%20%26amp%3B%20Code%20Data%22).Range(%22C16%22).Value%20%2B%204).Formula2).Offset(-1%2C%20-1).Value%0A%0AEnd%20Function%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2768849%22%20slang%3D%22en-US%22%3ESOLVED%20-%20Check%20based%20on%20whatever%20cell%20the%20User-Defined%20Formula%20(Function)%20is%20in%20-%20Set%20Target%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2768849%22%20slang%3D%22en-US%22%3E%3CP%3EGreetings!%20Can%20y'all%20help%20me%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BI%20have%20a%20function%20that%20acts%20as%20a%20cell%20formula.%20What's%20happening%20is%3A%20the%20little%20grey%20numbers%20represent%20the%20days%20since%20the%20Soldier%20went%20without%20a%20duty%20(CQ%20or%20Staff)%2C%20I%20have%20a%20sheet%20that%20acts%20as%20a%20database%20for%202021%20(2022%20currently%20displayed)%2C%20and%20I%20want%20the%20first%20cell%20in%20range%20%22E%22%20(1%2F1%2FYEAR)%20to%20dynamically%20reference%20the%20database's%20number%20based%20on%20the%20Solder's%20tag%20(Tag_PersonPos_2)%20to%20continue%20values.%20Since%20I%20named%20the%20database%20literally%20just%20%222021%22%2C%20I%20have%20code%20using%20.Find%20to%20get%20the%20cell%20address%20I%20want%20to%20reference%20dynamically%2C%20but%20I%20don't%20know%20%3CSTRONG%3Ehow%20to%20make%20it%20check%20based%20on%20whatever%20cell%20the%20User-Defined%20formula%20is%20in%3C%2FSTRONG%3E%2C%20as%20opposed%20to%20a%20defined%20range%20or%20selection.%20%3CU%3EWhat%20do%20I%20declare%20Target%20as%3F%3C%2FU%3E%26nbsp%3BPlease%20see%20the%20code%20below%20(and%20pictures%20for%20a%20visual%20of%20what%20I%20said)%20and%20I'd%20much%20appreciate%20any%20help.%20Thank%20you!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EActive%20Sheet%3A%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Formua.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F311668i86ACB65B37BF4AD6%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Formua.png%22%20alt%3D%22Formua.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPrevious%20Year%20Database%20-%20Ref%20%26amp%3B%20Tag%3A%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22PreviousYearRef.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F311669i40721D8C7346C4EB%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22PreviousYearRef.png%22%20alt%3D%22PreviousYearRef.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EActive%20Sheet%20Tag%3A%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22ActiveYearRef.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F311670iDF67885BDF90F55F%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22ActiveYearRef.png%22%20alt%3D%22ActiveYearRef.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECode%3A%E2%80%83%E2%80%83%E2%80%83%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3EPublic%20Function%20WS_MINUS_REF(ByVal%20Target%20As%20Range)%0A%0A'Target%20%3D%20%3F%3F%3F%0A%0AWS_MINUS_REF%20%3D%20ThisWorkbook.Sheets(%22%22%20%26amp%3B%20Worksheets(%22Troop%20to%20Task%20-%20Tracker%22).Range(%22D2%22).Value%20-%201%20%26amp%3B%20%22%22).%20_%0ACells.Find(What%3A%3DWorksheets(%22Troop%20to%20Task%20-%20Tracker%22).Range(%22E%22%20%26amp%3B%20Target.Row%20%26amp%3B%20%22%3AE%22%20%26amp%3B%20Target.Row).%20_%0AOffset(0%2C%20Worksheets(%22Formula%20%26amp%3B%20Code%20Data%22).Range(%22C16%22).Value%20%2B%204).Formula2).Offset(0%2C%20-1).Address%0A%0A'REFER%20TO%20CELL%20FORMULA%20IS%20IN%20DYNAMICALLY%3F%3F%3F!!!%0A%20%20%20%20%20%20%20%20%0AEnd%20Function%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2768849%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2769383%22%20slang%3D%22en-US%22%3ERe%3A%20SOLVED%20-%20Check%20based%20on%20whatever%20cell%20the%20User-Defined%20Formula%20(Function)%20is%20in%20-%20Set%20Target%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2769383%22%20slang%3D%22en-US%22%3E%3CP%3E(My%20completed%20code%2C%20to%20whoever%20it%20may%20help.)%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3EPublic%20Function%20PreviousYear_LastKnown_VALUE()%0A%0ADim%20FirstYear_Val%20As%20Integer%3A%20FirstYear_Val%20%3D%20Sheet4.Name%0AApplication.Volatile%20'This%20is%20to%20calculate%20this%20User-Defined%20formula%20automatically%20with%20the%20rest%20of%20the%20formulas%0AWith%20Application.Caller%20'This%20is%20to%20refer%20dynamically%20to%20the%20cell%20the%20formula%20is%20in%20(allow%20for%20%22.row%22)%0AIf%20Worksheets(%22Formula%20%26amp%3B%20Code%20Data%22).Range(%22I10%22).Value%20%3D%20%22No%22%20Then%20GoTo%20Calculate_NA%20'Check%20if%20not%20applicable%20(for%20macro%20enhancement)%0AOn%20Error%20GoTo%20Calculate_NA%0A%0AIf%20.Parent.Name%20%3D%20Sheet1.Name%20Then%20'Check%20if%20formula's%20in%20active%20year%0A%20%20%20%20If%20Not%20Worksheets(%22Troop%20to%20Task%20-%20Tracker%22).Range(%22D2%22).Value%20-%201%20%26lt%3B%20_%0A%20%20%20%20FirstYear_Val%20Then%20'Check%20if%20greater%20than%20or%20equal%20to%20first%20generated%20year%0A%20%20%20%20If%20Application.WorksheetFunction.CountIf(ThisWorkbook.Sheets(%22%22%20%26amp%3B%20Worksheets(%22Troop%20to%20Task%20-%20Tracker%22).%20_%0A%20%20%20%20Range(%22D2%22).Value%20-%201%20%26amp%3B%20%22%22).Cells%2C%20Worksheets(%22Troop%20to%20Task%20-%20Tracker%22).Range(%22E%22%20%26amp%3B%20.Row).%20_%0A%20%20%20%20Offset(0%2C%20Worksheets(%22Formula%20%26amp%3B%20Code%20Data%22).Range(%22C16%22).Value%20%2B%204).Formula2)%20%26gt%3B%200%20Then%20'Check%20if%20person%20exists%0A%20%20%20%20PrevYear_Rng%20%3D%20ThisWorkbook.Sheets(%22%22%20%26amp%3B%20Worksheets(%22Troop%20to%20Task%20-%20Tracker%22).Range(%22D2%22).Value%20-%201%20%26amp%3B%20%22%22).%20_%0A%20%20%20%20Cells.Find(What%3A%3DWorksheets(%22Troop%20to%20Task%20-%20Tracker%22).Range(%22E%22%20%26amp%3B%20.Row%20%26amp%3B%20%22%3AE%22%20%26amp%3B%20.Row).%20_%0A%20%20%20%20Offset(0%2C%20Worksheets(%22Formula%20%26amp%3B%20Code%20Data%22).Range(%22C16%22).Value%20%2B%204).Formula2).Offset(-1%2C%20-1).Address%0A%20%20%20%20If%20ThisWorkbook.Sheets(%22%22%20%26amp%3B%20Worksheets(%22Troop%20to%20Task%20-%20Tracker%22).Range(%22D2%22).Value%20-%201%20%26amp%3B%20%22%22).Range(%20_%0A%20%20%20%20PrevYear_Rng).Value%20%3D%20%22Staff%22%20Or%20ThisWorkbook.Sheets(%22%22%20%26amp%3B%20Worksheets(%22Troop%20to%20Task%20-%20Tracker%22).Range%20_%0A%20%20%20%20(%22D2%22).Value%20-%201%20%26amp%3B%20%22%22).Range(PrevYear_Rng).Value%20%3D%20%22CQ%22%20Then%20'Check%20if%20duty%20is%20last%20day%20of%20previous%20year%0A%20%20%20%20PreviousYear_LastKnown_VALUE%20%3D%201%0A%20%20%20%20Else%0A%20%20%20%20PreviousYear_LastKnown_VALUE%20%3D%20_%0A%20%20%20%20ThisWorkbook.Sheets(%22%22%20%26amp%3B%20Worksheets(%22Troop%20to%20Task%20-%20Tracker%22).Range(%22D2%22).Value%20-%201%20%26amp%3B%20%22%22).Range(PrevYear_Rng).Value%20%2B%201%0A%20%20%20%20End%20If%0A%20%20%20%20Else%3A%20PreviousYear_LastKnown_VALUE%20%3D%201%3A%20End%20If%0A%20%20%20%20Else%3A%20PreviousYear_LastKnown_VALUE%20%3D%201%3A%20End%20If%0AElseIf%20.Parent.Name%20%3D%20Sheet4.Name%20Then%20'Check%20if%20formula's%20in%20first%20generated%20year%0A%20%20%20%20'MAYBE%20ADD%20CODE%20FOR%20INPORTED%20DATABASE%20HERE%3F%3F%3F!!!%0A%20%20%20%20PreviousYear_LastKnown_VALUE%20%3D%201%0AElse%20'Formula's%20in%20second%2Fthird%2Fforth%20generated%20year%0A%20%20%20%20Dim%20NonActiveYear_SheetRef%20As%20Integer%3A%20NonActiveYear_SheetRef%20%3D%20.Parent.Name%0A%20%20%20%20If%20Application.WorksheetFunction.CountIf(ThisWorkbook.Sheets(%22%22%20%26amp%3B%20NonActiveYear_SheetRef%20-%201%20%26amp%3B%20%22%22).Cells%2C%20_%0A%20%20%20%20.Offset(1%2C%20ThisWorkbook.Sheets(%22%22%20%26amp%3B%20NonActiveYear_SheetRef%20%26amp%3B%20%22%22).Range(%22B1%22).Value).Formula2)%20%26gt%3B%200%20Then%20'Check%20if%20person%20exists%0A%20%20%20%20PrevYear_Rng_Other%20%3D%20ThisWorkbook.Sheets(%22%22%20%26amp%3B%20NonActiveYear_SheetRef%20-%201%20%26amp%3B%20%22%22).Cells.Find(What%3A%3D.Offset(1%2C%20_%0A%20%20%20%20ThisWorkbook.Sheets(%22%22%20%26amp%3B%20NonActiveYear_SheetRef%20%26amp%3B%20%22%22).Range(%22B1%22).Value).Formula2).Offset(-1%2C%20-1).Address%0A%20%20%20%20If%20ThisWorkbook.Sheets(%22%22%20%26amp%3B%20NonActiveYear_SheetRef%20-%201%20%26amp%3B%20%22%22).Range(%20_%0A%20%20%20%20PrevYear_Rng_Other).Value%20%3D%20%22Staff%22%20Or%20ThisWorkbook.Sheets(%22%22%20%26amp%3B%20NonActiveYear_SheetRef%20-%201%20%26amp%3B%20%22%22)%20_%0A%20%20%20%20.Range(PrevYear_Rng_Other).Value%20%3D%20%22CQ%22%20Then%20'Check%20if%20duty%20is%20last%20day%20of%20previous%20year%0A%20%20%20%20PreviousYear_LastKnown_VALUE%20%3D%201%0A%20%20%20%20Else%0A%20%20%20%20PreviousYear_LastKnown_VALUE%20%3D%20ThisWorkbook.Sheets(%22%22%20%26amp%3B%20NonActiveYear_SheetRef%20-%201%20%26amp%3B%20%22%22).Range(PrevYear_Rng_Other).Value%20%2B%201%0A%20%20%20%20End%20If%0A%20%20%20%20Else%3A%20PreviousYear_LastKnown_VALUE%20%3D%201%3A%20End%20If%0AEnd%20If%0A%0A'Error%20Handler%3A%0ACalculate_NA%3A%20If%20PreviousYear_LastKnown_VALUE%20%3D%200%20Or%20_%0APreviousYear_LastKnown_VALUE%20%3D%20%22%22%20Then%0APreviousYear_LastKnown_VALUE%20%3D%201%3A%20End%20If%3A%20End%20With%0A%0A'CODE%20WITHOUT%20THE%20IFS%3A%0A'PreviousYear_LastKnown_VALUE%20%3D%20ThisWorkbook.Sheets(%22%22%20%26amp%3B%20Worksheets(%22Troop%20to%20Task%20-%20Tracker%22).Range(%22D2%22).Value%20-%201%20%26amp%3B%20%22%22).%20_%0ACells.Find(What%3A%3DWorksheets(%22Troop%20to%20Task%20-%20Tracker%22).Range(%22E%22%20%26amp%3B%20.Row%20%26amp%3B%20%22%3AE%22%20%26amp%3B%20.Row).%20_%0AOffset(0%2C%20Worksheets(%22Formula%20%26amp%3B%20Code%20Data%22).Range(%22C16%22).Value%20%2B%204).Formula2).Offset(-1%2C%20-1).Value%0A%0AEnd%20Function%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Greetings! Can y'all help me?

 

     I have a function that acts as a cell formula. What's happening is: the little grey numbers represent the days since the Soldier went without a duty (CQ or Staff), I have a sheet that acts as a database for 2021 (2022 currently displayed), and I want the first cell in range "E" (1/1/YEAR) to dynamically reference the database's number based on the Solder's tag (Tag_PersonPos_2) to continue values. Since I named the database literally just "2021", I have code using .Find to get the cell address I want to reference dynamically, but I don't know how to make it check based on whatever cell the User-Defined formula is in, as opposed to a defined range or selection. What do I declare Target as? Please see the code below (and pictures for a visual of what I said) and I'd much appreciate any help. Thank you!

 

Active Sheet:

Formua.png

 

Previous Year Database - Ref & Tag:

PreviousYearRef.png

 

Active Sheet Tag:

ActiveYearRef.png

 

Code:   

 

 

Public Function WS_MINUS_REF(ByVal Target As Range)

'Target = ???

WS_MINUS_REF = ThisWorkbook.Sheets("" & Worksheets("Troop to Task - Tracker").Range("D2").Value - 1 & ""). _
Cells.Find(What:=Worksheets("Troop to Task - Tracker").Range("E" & Target.Row & ":E" & Target.Row). _
Offset(0, Worksheets("Formula & Code Data").Range("C16").Value + 4).Formula2).Offset(0, -1).Address

'REFER TO CELL FORMULA IS IN DYNAMICALLY???!!!
        
End Function

 

 

 

2 Replies
best response confirmed by Kendethar (Occasional Contributor)
Solution

Update:

 

I figured it out. Working:

Working.png

Working1.png

Untitled2.png

 

To make the user-defined formula be dynamic to whatever cell the formula is in:

1) I needed to not declare a target, range, selection, etc. at all.

2) Define the function within "With Application.Caller   *CODE*   End With"

3) Make it behave as regular formulas would regarding calculation using "Application.Volatile"

So basically, instead of Target.Row do just .row, and the solution is no definition of Target and use .Caller.

 

Code:

(Of course, I also put in all the checks (IFs) specific to my need.)

 

 

Public Function PreviousYear_LastKnown_VALUE()

Dim FirstYear_Val As Integer: FirstYear_Val = Sheet4.Name
Application.Volatile 'This is to calculate this User-Defined formula automatically with the rest of the formulas
With Application.Caller 'This is to refer dynamically to the cell the formula is in (allow for ".row")

If Not Worksheets("Troop to Task - Tracker").Range("D2").Value - 1 < FirstYear_Val Then 'Check if greater than or equal to first generated year

If Application.WorksheetFunction.CountIf(ThisWorkbook.Sheets("" & Worksheets("Troop to Task - Tracker"). _
Range("D2").Value - 1 & "").Cells, Worksheets("Troop to Task - Tracker").Range("E" & .Row). _
Offset(0, Worksheets("Formula & Code Data").Range("C16").Value + 4).Formula2) > 0 Then 'Check if person exists

PrevYear_Rng = ThisWorkbook.Sheets("" & Worksheets("Troop to Task - Tracker").Range("D2").Value - 1 & ""). _
Cells.Find(What:=Worksheets("Troop to Task - Tracker").Range("E" & .Row & ":E" & .Row). _
Offset(0, Worksheets("Formula & Code Data").Range("C16").Value + 4).Formula2).Offset(-1, -1).Address

If ThisWorkbook.Sheets("" & Worksheets("Troop to Task - Tracker").Range("D2").Value - 1 & "").Range( _
PrevYear_Rng).Value = "Staff" Or ThisWorkbook.Sheets("" & Worksheets("Troop to Task - Tracker").Range _
("D2").Value - 1 & "").Range(PrevYear_Rng).Value = "CQ" Then 'Check if duty is last day of previous year

PreviousYear_LastKnown_VALUE = "This'll be 1 by default" '1
Else
PreviousYear_LastKnown_VALUE = _
ThisWorkbook.Sheets("" & Worksheets("Troop to Task - Tracker").Range("D2").Value - 1 & "").Range(PrevYear_Rng).Value + 1
End If
Else
PreviousYear_LastKnown_VALUE = "This'll be 1 by default" '1
End If
Else
PreviousYear_LastKnown_VALUE = "This'll be 1 by default" '1
End If
End With

'CODE WITHOUT THE IFS:
'WS_MINUS_REF = ThisWorkbook.Sheets("" & Worksheets("Troop to Task - Tracker").Range("D2").Value - 1 & ""). _
Cells.Find(What:=Worksheets("Troop to Task - Tracker").Range("E" & .Row & ":E" & .Row). _
Offset(0, Worksheets("Formula & Code Data").Range("C16").Value + 4).Formula2).Offset(-1, -1).Value

End Function

 

 

 

(My completed code, to whoever it may help.)

 

 

Public Function PreviousYear_LastKnown_VALUE()

Dim FirstYear_Val As Integer: FirstYear_Val = Sheet4.Name
Application.Volatile 'This is to calculate this User-Defined formula automatically with the rest of the formulas
With Application.Caller 'This is to refer dynamically to the cell the formula is in (allow for ".row")
If Worksheets("Formula & Code Data").Range("I10").Value = "No" Then GoTo Calculate_NA 'Check if not applicable (for macro enhancement)
On Error GoTo Calculate_NA

If .Parent.Name = Sheet1.Name Then 'Check if formula's in active year
    If Not Worksheets("Troop to Task - Tracker").Range("D2").Value - 1 < _
    FirstYear_Val Then 'Check if greater than or equal to first generated year
    If Application.WorksheetFunction.CountIf(ThisWorkbook.Sheets("" & Worksheets("Troop to Task - Tracker"). _
    Range("D2").Value - 1 & "").Cells, Worksheets("Troop to Task - Tracker").Range("E" & .Row). _
    Offset(0, Worksheets("Formula & Code Data").Range("C16").Value + 4).Formula2) > 0 Then 'Check if person exists
    PrevYear_Rng = ThisWorkbook.Sheets("" & Worksheets("Troop to Task - Tracker").Range("D2").Value - 1 & ""). _
    Cells.Find(What:=Worksheets("Troop to Task - Tracker").Range("E" & .Row & ":E" & .Row). _
    Offset(0, Worksheets("Formula & Code Data").Range("C16").Value + 4).Formula2, Lookat:=xlWhole).Offset(-1, -1).Address
    'Note: "Lookat:=xlWhole" is necessary to get an exact match (I.e. Tag_PersonPos2 not matched/found with Tag_PersonPos29)
    If ThisWorkbook.Sheets("" & Worksheets("Troop to Task - Tracker").Range("D2").Value - 1 & "").Range( _
    PrevYear_Rng).Value = "Staff" Or ThisWorkbook.Sheets("" & Worksheets("Troop to Task - Tracker").Range _
    ("D2").Value - 1 & "").Range(PrevYear_Rng).Value = "CQ" Then 'Check if duty is last day of previous year
    PreviousYear_LastKnown_VALUE = 1
    Else
    PreviousYear_LastKnown_VALUE = _
    ThisWorkbook.Sheets("" & Worksheets("Troop to Task - Tracker").Range("D2").Value - 1 & "").Range(PrevYear_Rng).Value + 1
    End If
    Else: PreviousYear_LastKnown_VALUE = 1: End If
    Else: PreviousYear_LastKnown_VALUE = 1: End If
ElseIf .Parent.Name = Sheet4.Name Then 'Check if formula's in first generated year
    'MAYBE ADD CODE FOR INPORTED DATABASE HERE???!!!
    PreviousYear_LastKnown_VALUE = 1
Else 'Formula's in second/third/forth generated year
    Dim NonActiveYear_SheetRef As Integer: NonActiveYear_SheetRef = .Parent.Name
    If Application.WorksheetFunction.CountIf(ThisWorkbook.Sheets("" & NonActiveYear_SheetRef - 1 & "").Cells, _
    .Offset(1, ThisWorkbook.Sheets("" & NonActiveYear_SheetRef & "").Range("B1").Value).Formula2) > 0 Then 'Check if person exists
    PrevYear_Rng_Other = ThisWorkbook.Sheets("" & NonActiveYear_SheetRef - 1 & "").Cells.Find(What:=.Offset(1, _
    ThisWorkbook.Sheets("" & NonActiveYear_SheetRef & "").Range("B1").Value).Formula2, Lookat:=xlWhole).Offset(-1, -1).Address
    'Note: "Lookat:=xlWhole" is necessary to get an exact match (I.e. Tag_PersonPos2 not matched/found with Tag_PersonPos29)
    If ThisWorkbook.Sheets("" & NonActiveYear_SheetRef - 1 & "").Range( _
    PrevYear_Rng_Other).Value = "Staff" Or ThisWorkbook.Sheets("" & NonActiveYear_SheetRef - 1 & "") _
    .Range(PrevYear_Rng_Other).Value = "CQ" Then 'Check if duty is last day of previous year
    PreviousYear_LastKnown_VALUE = 1
    Else
    PreviousYear_LastKnown_VALUE = ThisWorkbook.Sheets("" & NonActiveYear_SheetRef - 1 & "").Range(PrevYear_Rng_Other).Value + 1
    End If
    Else: PreviousYear_LastKnown_VALUE = 1: End If
End If

'Error Handler:
Calculate_NA: If PreviousYear_LastKnown_VALUE = 0 Or _
PreviousYear_LastKnown_VALUE = "" Then
PreviousYear_LastKnown_VALUE = 1: End If: End With

End Function