SOLVED
Home

Excel highlight numbers in range by input

%3CLINGO-SUB%20id%3D%22lingo-sub-307296%22%20slang%3D%22en-US%22%3EExcel%20highlight%20numbers%20in%20range%20by%20input%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-307296%22%20slang%3D%22en-US%22%3E%3CP%3EI%20pretty%20much%20forgot%20about%20VBA%2C%20cause%20i%20was%20learning%20it%20in%20school.%3CBR%20%2F%3EBut%20i%20like%20to%20do%20something%20in%20Excel.%3CBR%20%2F%3ELet's%20take%20a%20look%20at%20my%20tables%20from%20beginning.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F64326i2A3B20A4DF584FFD%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%221a.PNG%22%20title%3D%221a.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EThis%20all%20is%20just%20for%20my%20lottery%20Statistics.%3C%2FP%3E%3CP%3EIn%20table%20A%20i'm%20writing%20in%20numbers%2C%20which%20i%20have%20chosen%20in%20lottery%20tickets.%20(5%20X%2020%20numbers%20)%3CBR%20%2F%3EIn%20table%20B%20i'm%20writing%20those%20number%20which%20was%20drawn%20in%20TV%20lottery.%20(20%2B%20NUMBERS)%3C%2FP%3E%3CP%3ESo%20I%20want%20to%20make%20a%20VBA%20code%20to%20highlight%20cells%20in%20table%20A%20when%20the%20same%20number%20is%20written%20%2F%20inputted%20in%20table%20B.%20But%20i%20don't%20highlight%20any%20blank%20values%2C%20cause%20not%20always%20I'm%20playing%20all%205%20variants%20in%20game%2C%20and%20not%20all%20N's%20are%20numbers%20in%20table%20B%3CBR%20%2F%3EFor%20example%2C%20i%20input%20number%2047%20in%20table%20B%2C%20and%20it%20highlights%20all%20cells%20with%20number%2047%20in%20table%20A%20with%20green%20fill%20colour.%3CBR%20%2F%3EI've%20already%20made%20CountCColor%20in%20VBA%2C%20but%20i%20need%20to%20make%20VBA%20for%20automatically%20highlighting%20numbers%20inputted%20by%20me%20in%20B%20to%20A.%3CBR%20%2F%3EConditional%20formatting%20is%20not%20good%20for%20this%20%3AD%3C%2FP%3E%3CP%3EFor%20example%20Tables%202.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F64328i7125C51A3F80D173%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%221ab.PNG%22%20title%3D%221ab.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20bought%201%20lottery%20ticket%20from%20possible%205.%20Have%20chosen%20my%2020%20numbers%20TableA.%20And%20numbers%20in%20TableB%20were%20drawn%20in%20lottery.%20I%20used%20Kutools%20trial%20there%20but%2C%20is%20it%20possible%20somehow%20to%20do%20that%20with%20built%20in%20Excel%20options%20or%20VBA%3F%3CBR%20%2F%3EI%20just%20need%20highlight%20numbers%20(green)%20in%20B4%3AF23%20which%20will%20be%20or%20was%20inputted%20in%20H4%3AL23.%3C%2FP%3E%3CP%3EThanks%20in%20advance%20%3A)%3CBR%20%2F%3EHappy%20holidays%20%3A)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-307296%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ETraining%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-363036%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20highlight%20numbers%20in%20range%20by%20input%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-363036%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20%3A)%26nbsp%3B%3CBR%20%2F%3ESorry%20for%20my%20very%20late%20response!%3CBR%20%2F%3E%3CBR%20%2F%3EI%20was%20working%20on%20some%20projects%20and%20new%20things%2C%20so%20i%20didn't%20managed%20to%20watch%20Your%20last%20attached%20file%20%3A(%3CBR%20%2F%3E%3CBR%20%2F%3EBut%20i%20opened%20it%20couplde%20days%20ago%20%3A)%3CBR%20%2F%3EAnd%20You%20know%20what%3F%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3EYour%20code%20learned%20me%20a%20lot%20and%20i%20used%20(what%20i%20learned%20from%20Your%20code)%20in%20other%20my%20projects%20and%20works%2C%26nbsp%3B%3C%2FP%3E%3CP%3EI%20learned%20and%20used%20the%20principe%20of%20your%20code.%3CBR%20%2F%3EIn%20completely%20different%20topics.%3C%2FP%3E%3CP%3EWhat%20I%20learned%20from%20your%20code%20helped%20me%20a%20lot%20and%20made%20me%20understand%20many%20things%20that%20I%20did%20not%20understand%20before.%3CBR%20%2F%3E%3CBR%20%2F%3EThanks%20%3A)%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-307662%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20highlight%20numbers%20in%20range%20by%20input%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-307662%22%20slang%3D%22en-US%22%3E%3CP%3EGood%20Morning%2C%20Here%20is%20my%20proposal%20for%20a%20rewrite%20for%20your%20count%20color%20function%20not%20relying%20on%20cell%20color.%20As%20I%20mentioned%20before%20using%20cell%20color%20is%20not%20a%20preferred%20way%20of%20doing%20this%20(Tend%20to%20run%20into%20issues%2C%20such%20as%20the%20one%20we%20had%20before%20where%20you%20had%20to%20manually%20recalculate%20these%20functions%2C%20Excel%20does%20not%20recognize%20a%20change%20in%20a%20cell%20color%20as%20a%20needed%20trigger%20to%20recalculate%20functions.)%20If%20you%20there%20is%20a%20logic%20as%20to%20why%20cells%20on%20a%20worksheet%20are%20colored%20always%20try%20to%20go%20back%20to%20using%20that%20logic%20to%20create%20your%20function%2Fsub.%20Arguments%20are%20now%20the%20range%20of%20cells%20for%20that%20ticket%20and%20the%20range%20of%20cells%20for%20the%20called%20numbers.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EOption%20Explicit%3CBR%20%2F%3E%3CBR%20%2F%3EFunction%20CountCcolor(TicketRange%20As%20Range%2C%20DrawRange%20As%20Range)%20As%20Long%3CBR%20%2F%3E%3CBR%20%2F%3E%20Dim%20Cell%20As%20Range%3CBR%20%2F%3E%20Dim%20CheckRange%20As%20Range%3CBR%20%2F%3E%3CBR%20%2F%3E%20For%20Each%20Cell%20In%20TicketRange%3CBR%20%2F%3E%3CBR%20%2F%3E%20Set%20CheckRange%20%3D%20DrawRange.Find(What%3A%3DCell.Value%2C%20LookIn%3A%3DxlValues%2C%20LookAt%3A%3DxlWhole%2C%20MatchCase%3A%3DFalse)%3CBR%20%2F%3E%3CBR%20%2F%3E%20If%20Not%20CheckRange%20Is%20Nothing%20And%20Cell.Value%20%26lt%3B%26gt%3B%20%22%22%20Then%3CBR%20%2F%3E%3CBR%20%2F%3E%20CountCcolor%20%3D%20CountCcolor%20%2B%201%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20End%20If%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Next%20Cell%3CBR%20%2F%3E%3CBR%20%2F%3EEnd%20Function%3C%2FPRE%3E%3CP%3E%26nbsp%3BI%20redid%20the%20macros%20as%20well%2C%20what%20I%20had%20sent%20you%20before%20had%20the%20macro%20called%20every%20time%20any%20cell%20value%20was%20changed%2C%20added%20a%20few%20lines%20to%20check%20that%20the%20cell%20value%20that%20was%20changed%20is%20within%20the%20range%20of%20the%20ticket%20numbers%20or%20the%20draw%20numbers%20before%20calling%20the%20macro.%20Probably%20not%20all%20that%20critical%20on%20your%20workbook%20but%20this%20will%20speed%20things%20up%20if%20you%20are%20doing%20anything%20else%20on%20worksheets%20in%20this%20workbook.%20Sample%20attached.%20Thank%20you.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EOption%20Explicit%3CBR%20%2F%3E%3CBR%20%2F%3EPrivate%20Sub%20Workbook_SheetChange(ByVal%20Sh%20As%20Object%2C%20ByVal%20Target%20As%20Range)%3CBR%20%2F%3E%3CBR%20%2F%3E%20Dim%20TicketRange%20As%20Range%3CBR%20%2F%3E%20Dim%20DrawRange%20As%20Range%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Set%20TicketRange%20%3D%20Sh.Range(%22B4%3AF23%22)%3CBR%20%2F%3E%20Set%20DrawRange%20%3D%20Sh.Range(%22H4%3AL23%22)%3CBR%20%2F%3E%3CBR%20%2F%3E%20If%20InRange(Target%2C%20TicketRange)%20Or%20InRange(Target%2C%20DrawRange)%20Then%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Call%20ColorCells(Sh)%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20End%20If%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20End%20Sub%3CBR%20%2F%3E%3CBR%20%2F%3EFunction%20InRange(Range1%20As%20Range%2C%20Range2%20As%20Range)%20As%20Boolean%3CBR%20%2F%3E%3CBR%20%2F%3E%20InRange%20%3D%20Not%20(Application.Intersect(Range1%2C%20Range2)%20Is%20Nothing)%3CBR%20%2F%3E%20%3CBR%20%2F%3EEnd%20Function%3CBR%20%2F%3E%3CBR%20%2F%3EPrivate%20Sub%20ColorCells(ByVal%20Sh%20As%20Object)%3CBR%20%2F%3E%3CBR%20%2F%3E%20Dim%20TicketRange%20As%20Range%3CBR%20%2F%3E%20Dim%20DrawRange%20As%20Range%3CBR%20%2F%3E%20Dim%20Cell%20As%20Range%3CBR%20%2F%3E%20Dim%20CheckRange%20As%20Range%3CBR%20%2F%3E%3CBR%20%2F%3E%20Set%20TicketRange%20%3D%20Sh.Range(%22B4%3AF23%22)%3CBR%20%2F%3E%20Set%20DrawRange%20%3D%20Sh.Range(%22H4%3AL23%22)%3CBR%20%2F%3E%3CBR%20%2F%3E%20For%20Each%20Cell%20In%20TicketRange%3CBR%20%2F%3E%3CBR%20%2F%3E%20Set%20CheckRange%20%3D%20DrawRange.Find(What%3A%3DCell.Value%2C%20LookIn%3A%3DxlValues%2C%20LookAt%3A%3DxlWhole%2C%20MatchCase%3A%3DFalse)%3CBR%20%2F%3E%3CBR%20%2F%3E%20If%20Not%20CheckRange%20Is%20Nothing%20And%20Cell.Value%20%26lt%3B%26gt%3B%20%22%22%20Then%3CBR%20%2F%3E%3CBR%20%2F%3E%20Cell.Interior.ColorIndex%20%3D%204%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Else%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Cell.Interior.ColorIndex%20%3D%200%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20End%20If%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Next%20Cell%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20End%20Sub%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-307368%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20highlight%20numbers%20in%20range%20by%20input%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-307368%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20again%20%3A)%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-307367%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20highlight%20numbers%20in%20range%20by%20input%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-307367%22%20slang%3D%22en-US%22%3EYour%20welcome.%20Happy%20Holidays%20to%20you%20as%20well.%3CBR%20%2F%3E%3CBR%20%2F%3EYou%20should%20change%20your%20count%20function%20to%20not%20key%20off%20colour%20though%2C%20I%20won%E2%80%99t%20say%20I%E2%80%99ve%20never%20done%20it%20but%20it%E2%80%99s%20not%20really%20good%20practice.%20Should%20check%20for%20matches%20the%20same%20way%20the%20colouring%20macro%20does.%20If%20I%20get%20some%20time%20I%20will%20try%20to%20write%20an%20alternative%20for%20you.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-307364%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20highlight%20numbers%20in%20range%20by%20input%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-307364%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20%3A)%3CBR%20%2F%3EYou've%20helped%20me%20a%20lot%20%3A)%26nbsp%3B%3CBR%20%2F%3EHappy%20Holidays%20%3A)%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-307362%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20highlight%20numbers%20in%20range%20by%20input%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-307362%22%20slang%3D%22en-US%22%3E%3CP%3EYou%20can%20force%20recalculation%20of%20the%20workbook%20(Ie.%20your%20function%20which%20is%20not%20auto%20recalculating)%20by%20adding%20this%20line%20to%20the%20end%20of%20my%20sub%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EApplication.Calculate%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20can%20maybe%20come%20up%20with%20something%20a%20bit%20more%20elegant%20but%20I've%20got%20to%20take%20off%20now.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-307361%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20highlight%20numbers%20in%20range%20by%20input%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-307361%22%20slang%3D%22en-US%22%3E%3CP%3EOh%20thanks%20%3A)%3CBR%20%2F%3EIt's%20working%20%3A)%3CBR%20%2F%3E%3CBR%20%2F%3EI'll%20learn%20from%20it%2C%20I%20promise%20%3A)%3CBR%20%2F%3EBut%20is%20it%20possible%20to%20make%20that%20the%20count%20of%20coloured%20numbers%20shows%20automatically%20when%20it's%20matching%20number%20in%20lottery%20range%20from%20draw%20range%3F%3CBR%20%2F%3EBecause%20when%20i%20use%20Your%20VBA%20code%2C%20it's%20working%20good.%20But%20when%20i%20enter%20in%20draw%20range%2C%20i%20need%20to%20P7%20-%20P11%20manually%20and%20hit%20enter.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-307360%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20highlight%20numbers%20in%20range%20by%20input%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-307360%22%20slang%3D%22en-US%22%3E%3CP%3ESorry%2C%20I%20didn't%20realize%20you%20had%20this%20on%20multiple%20worksheets%20in%20the%20workbook%2C%20changed%20to%20a%20workbook%20change%20event.%20So%20the%20code%20goes%20in%20ThisWorkbook.%20Added%20to%20what%20you%20had%20posted%20and%20attached.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EPrivate%20Sub%20Workbook_SheetChange(ByVal%20Sh%20As%20Object%2C%20ByVal%20Target%20As%20Range)%3CBR%20%2F%3E%3CBR%20%2F%3E%20Dim%20TicketRange%20As%20Range%3CBR%20%2F%3E%20Dim%20DrawRange%20As%20Range%3CBR%20%2F%3E%20Dim%20Cell%20As%20Range%3CBR%20%2F%3E%20Dim%20CheckRange%20As%20Range%3CBR%20%2F%3E%3CBR%20%2F%3E%20Set%20TicketRange%20%3D%20Range(%22B4%3AF23%22)%3CBR%20%2F%3E%20Set%20DrawRange%20%3D%20Range(%22H4%3AL23%22)%3CBR%20%2F%3E%3CBR%20%2F%3E%20For%20Each%20Cell%20In%20TicketRange%3CBR%20%2F%3E%3CBR%20%2F%3E%20Set%20CheckRange%20%3D%20Range(%22H4%3AL23%22).Find(What%3A%3DCell.Value%2C%20LookIn%3A%3DxlValues%2C%20LookAt%3A%3DxlWhole%2C%20MatchCase%3A%3DFalse)%3CBR%20%2F%3E%3CBR%20%2F%3E%20If%20Not%20CheckRange%20Is%20Nothing%20And%20Cell.Value%20%26lt%3B%26gt%3B%20%22%22%20Then%3CBR%20%2F%3E%3CBR%20%2F%3E%20Cell.Interior.ColorIndex%20%3D%204%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Else%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Cell.Interior.ColorIndex%20%3D%200%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20End%20If%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Next%20Cell%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20End%20Sub%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-307357%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20highlight%20numbers%20in%20range%20by%20input%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-307357%22%20slang%3D%22en-US%22%3E%3CP%3EFor%20me%20is%20working%20only%20that%20Macro%20which%20i%20need%20to%20run%20manually.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-307351%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20highlight%20numbers%20in%20range%20by%20input%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-307351%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20for%20trying%20to%20help.%3CBR%20%2F%3EBut%20nothing%20works%20for%20me.%3CBR%20%2F%3E%3CBR%20%2F%3EMaybe%20i%20can%20send%20You%20a%20copy%20of%20my%20worksheet%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-307318%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20highlight%20numbers%20in%20range%20by%20input%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-307318%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%20Here%20are%20a%20couple%20of%20options%2C%20If%20you%20want%20to%20have%20the%20coloring%20happen%20only%20when%20you%20manually%20run%20the%20code%20stick%20this%20in%20a%20module%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3ESub%20Color()%3CBR%20%2F%3E%3CBR%20%2F%3E%20Dim%20TicketRange%20As%20Range%3CBR%20%2F%3E%20Dim%20DrawRange%20As%20Range%3CBR%20%2F%3E%20Dim%20Cell%20As%20Range%3CBR%20%2F%3E%20Dim%20CheckRange%20As%20Range%3CBR%20%2F%3E%3CBR%20%2F%3E%20Set%20TicketRange%20%3D%20Range(%22B4%3AF23%22)%3CBR%20%2F%3E%20Set%20DrawRange%20%3D%20Range(%22H4%3AL23%22)%3CBR%20%2F%3E%3CBR%20%2F%3E%20For%20Each%20Cell%20In%20TicketRange%3CBR%20%2F%3E%3CBR%20%2F%3E%20Set%20CheckRange%20%3D%20Range(%22H4%3AL23%22).Find(What%3A%3DCell.Value%2C%20LookIn%3A%3DxlValues%2C%20LookAt%3A%3DxlWhole%2C%20MatchCase%3A%3DFalse)%3CBR%20%2F%3E%3CBR%20%2F%3E%20If%20Not%20CheckRange%20Is%20Nothing%20And%20Cell.Value%20%26lt%3B%26gt%3B%20%22%22%20Then%3CBR%20%2F%3E%3CBR%20%2F%3E%20Cell.Interior.ColorIndex%20%3D%204%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20End%20If%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Next%20Cell%3CBR%20%2F%3E%3CBR%20%2F%3EEnd%20Sub%3CBR%20%2F%3E%3CBR%20%2F%3ESub%20UnColor()%3CBR%20%2F%3E%3CBR%20%2F%3E%20Dim%20TicketRange%20As%20Range%3CBR%20%2F%3E%20Dim%20Cell%20As%20Range%3CBR%20%2F%3E%3CBR%20%2F%3E%20Set%20TicketRange%20%3D%20Range(%22B4%3AF23%22)%3CBR%20%2F%3E%3CBR%20%2F%3E%20For%20Each%20Cell%20In%20TicketRange%3CBR%20%2F%3E%3CBR%20%2F%3E%20Cell.Interior.ColorIndex%20%3D%200%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Next%20Cell%3CBR%20%2F%3E%3CBR%20%2F%3EEnd%20Sub%3C%2FPRE%3E%3CP%3E%26nbsp%3BIf%20you%20want%20the%20code%20to%20run%20every%20time%20you%20change%20a%20cells%20value%20on%20the%20worksheet%20than%20stick%20this%20in%20the%20VBA%20for%20your%20worksheet%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EPrivate%20Sub%20Worksheet_Change(ByVal%20Target%20As%20Range)%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Dim%20TicketRange%20As%20Range%3CBR%20%2F%3E%20Dim%20DrawRange%20As%20Range%3CBR%20%2F%3E%20Dim%20Cell%20As%20Range%3CBR%20%2F%3E%20Dim%20CheckRange%20As%20Range%3CBR%20%2F%3E%3CBR%20%2F%3E%20Set%20TicketRange%20%3D%20Range(%22B4%3AF23%22)%3CBR%20%2F%3E%20Set%20DrawRange%20%3D%20Range(%22H4%3AL23%22)%3CBR%20%2F%3E%3CBR%20%2F%3E%20For%20Each%20Cell%20In%20TicketRange%3CBR%20%2F%3E%3CBR%20%2F%3E%20Set%20CheckRange%20%3D%20Range(%22H4%3AL23%22).Find(What%3A%3DCell.Value%2C%20LookIn%3A%3DxlValues%2C%20LookAt%3A%3DxlWhole%2C%20MatchCase%3A%3DFalse)%3CBR%20%2F%3E%3CBR%20%2F%3E%20If%20Not%20CheckRange%20Is%20Nothing%20And%20Cell.Value%20%26lt%3B%26gt%3B%20%22%22%20Then%3CBR%20%2F%3E%3CBR%20%2F%3E%20Cell.Interior.ColorIndex%20%3D%204%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Else%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Cell.Interior.ColorIndex%20%3D%200%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20End%20If%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Next%20Cell%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20End%20Sub%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
NaurisLatvia28
Occasional Contributor

I pretty much forgot about VBA, cause i was learning it in school.
But i like to do something in Excel.
Let's take a look at my tables from beginning.

 

1a.PNG


This all is just for my lottery Statistics.

In table A i'm writing in numbers, which i have chosen in lottery tickets. (5 X 20 numbers )
In table B i'm writing those number which was drawn in TV lottery. (20+ NUMBERS)

So I want to make a VBA code to highlight cells in table A when the same number is written / inputted in table B. But i don't highlight any blank values, cause not always I'm playing all 5 variants in game, and not all N's are numbers in table B
For example, i input number 47 in table B, and it highlights all cells with number 47 in table A with green fill colour.
I've already made CountCColor in VBA, but i need to make VBA for automatically highlighting numbers inputted by me in B to A.
Conditional formatting is not good for this :D

For example Tables 2.

1ab.PNG

 

I bought 1 lottery ticket from possible 5. Have chosen my 20 numbers TableA. And numbers in TableB were drawn in lottery. I used Kutools trial there but, is it possible somehow to do that with built in Excel options or VBA?
I just need highlight numbers (green) in B4:F23 which will be or was inputted in H4:L23.

Thanks in advance :)
Happy holidays :)

 



11 Replies

Hi, Here are a couple of options, If you want to have the coloring happen only when you manually run the code stick this in a module:

 

Sub Color()

Dim TicketRange As Range
Dim DrawRange As Range
Dim Cell As Range
Dim CheckRange As Range

Set TicketRange = Range("B4:F23")
Set DrawRange = Range("H4:L23")

For Each Cell In TicketRange

Set CheckRange = Range("H4:L23").Find(What:=Cell.Value, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)

If Not CheckRange Is Nothing And Cell.Value <> "" Then

Cell.Interior.ColorIndex = 4

End If

Next Cell

End Sub

Sub UnColor()

Dim TicketRange As Range
Dim Cell As Range

Set TicketRange = Range("B4:F23")

For Each Cell In TicketRange

Cell.Interior.ColorIndex = 0

Next Cell

End Sub

 If you want the code to run every time you change a cells value on the worksheet than stick this in the VBA for your worksheet:

 

Private Sub Worksheet_Change(ByVal Target As Range)

Dim TicketRange As Range
Dim DrawRange As Range
Dim Cell As Range
Dim CheckRange As Range

Set TicketRange = Range("B4:F23")
Set DrawRange = Range("H4:L23")

For Each Cell In TicketRange

Set CheckRange = Range("H4:L23").Find(What:=Cell.Value, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)

If Not CheckRange Is Nothing And Cell.Value <> "" Then

Cell.Interior.ColorIndex = 4

Else

Cell.Interior.ColorIndex = 0

End If

Next Cell

End Sub

Thanks for trying to help.
But nothing works for me.

Maybe i can send You a copy of my worksheet?

For me is working only that Macro which i need to run manually.

Solution

Sorry, I didn't realize you had this on multiple worksheets in the workbook, changed to a workbook change event. So the code goes in ThisWorkbook. Added to what you had posted and attached. 

 

 

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

Dim TicketRange As Range
Dim DrawRange As Range
Dim Cell As Range
Dim CheckRange As Range

Set TicketRange = Range("B4:F23")
Set DrawRange = Range("H4:L23")

For Each Cell In TicketRange

Set CheckRange = Range("H4:L23").Find(What:=Cell.Value, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)

If Not CheckRange Is Nothing And Cell.Value <> "" Then

Cell.Interior.ColorIndex = 4

Else

Cell.Interior.ColorIndex = 0

End If

Next Cell

End Sub

 

Oh thanks :)
It's working :)

I'll learn from it, I promise :)
But is it possible to make that the count of coloured numbers shows automatically when it's matching number in lottery range from draw range?
Because when i use Your VBA code, it's working good. But when i enter in draw range, i need to P7 - P11 manually and hit enter.

You can force recalculation of the workbook (Ie. your function which is not auto recalculating) by adding this line to the end of my sub:

 

Application.Calculate

 

I can maybe come up with something a bit more elegant but I've got to take off now. 

Thanks :)
You've helped me a lot :) 
Happy Holidays :)

Your welcome. Happy Holidays to you as well.

You should change your count function to not key off colour though, I won’t say I’ve never done it but it’s not really good practice. Should check for matches the same way the colouring macro does. If I get some time I will try to write an alternative for you.

Thanks again :)

Good Morning, Here is my proposal for a rewrite for your count color function not relying on cell color. As I mentioned before using cell color is not a preferred way of doing this (Tend to run into issues, such as the one we had before where you had to manually recalculate these functions, Excel does not recognize a change in a cell color as a needed trigger to recalculate functions.) If you there is a logic as to why cells on a worksheet are colored always try to go back to using that logic to create your function/sub. Arguments are now the range of cells for that ticket and the range of cells for the called numbers. 

 

 

Option Explicit

Function CountCcolor(TicketRange As Range, DrawRange As Range) As Long

Dim Cell As Range
Dim CheckRange As Range

For Each Cell In TicketRange

Set CheckRange = DrawRange.Find(What:=Cell.Value, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)

If Not CheckRange Is Nothing And Cell.Value <> "" Then

CountCcolor = CountCcolor + 1

End If

Next Cell

End Function

 I redid the macros as well, what I had sent you before had the macro called every time any cell value was changed, added a few lines to check that the cell value that was changed is within the range of the ticket numbers or the draw numbers before calling the macro. Probably not all that critical on your workbook but this will speed things up if you are doing anything else on worksheets in this workbook. Sample attached. Thank you. 

 

Option Explicit

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

Dim TicketRange As Range
Dim DrawRange As Range

Set TicketRange = Sh.Range("B4:F23")
Set DrawRange = Sh.Range("H4:L23")

If InRange(Target, TicketRange) Or InRange(Target, DrawRange) Then

Call ColorCells(Sh)

End If

End Sub

Function InRange(Range1 As Range, Range2 As Range) As Boolean

InRange = Not (Application.Intersect(Range1, Range2) Is Nothing)

End Function

Private Sub ColorCells(ByVal Sh As Object)

Dim TicketRange As Range
Dim DrawRange As Range
Dim Cell As Range
Dim CheckRange As Range

Set TicketRange = Sh.Range("B4:F23")
Set DrawRange = Sh.Range("H4:L23")

For Each Cell In TicketRange

Set CheckRange = DrawRange.Find(What:=Cell.Value, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)

If Not CheckRange Is Nothing And Cell.Value <> "" Then

Cell.Interior.ColorIndex = 4

Else

Cell.Interior.ColorIndex = 0

End If

Next Cell

End Sub

 

 

Hi :) 
Sorry for my very late response!

I was working on some projects and new things, so i didn't managed to watch Your last attached file :(

But i opened it couplde days ago :)
And You know what? 

Your code learned me a lot and i used (what i learned from Your code) in other my projects and works, 

I learned and used the principe of your code.
In completely different topics.

What I learned from your code helped me a lot and made me understand many things that I did not understand before.

Thanks :) 

Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
16 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
flashing a white screen while open new tab
cntvertex in Discussions on
11 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
28 Replies