SOLVED
Home

Color cells of a worksheet by comparing entries with other worksheet.

%3CLINGO-SUB%20id%3D%22lingo-sub-846039%22%20slang%3D%22en-US%22%3EColor%20cells%20of%20a%20worksheet%20by%20comparing%20entries%20with%20other%20worksheet.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-846039%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EIn%20the%20attached%20workbook%2C%20i%20have%20to%20compare%20the%20*COMPETITOR*%20Worksheet%20with%20the%20*PLAN*%20Worksheet.%20The%20comparison%20is%20to%20be%20done%20between%20Plans%20Premium%2C%20Plan_Amount%20and%20Item%201%20Price%20in%20both%20the%20sheets%20and%20conditionally%20format%20the%20Competitor%20worksheets%20entry%20as%20blue%20and%20red%20depending%20on%20the%20values(%20if%20lower%20or%20higher%20than%20the%20values%20in%20Plan%20worksheet).%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EI%20have%20to%20automate%20this%20step%20along%20multiple%20sheet(for%20different%20states).%20Kindly%20help%20me%20out%20here.%20Apologies%20for%20lack%20of%20clarity%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3Ei%20will%20try%20to%20frame%20the%20question%20in%20a%20better%20way%20if%20it's%20not%20clear.%3C%2FSPAN%3E%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-846039%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-846057%22%20slang%3D%22en-US%22%3ERe%3A%20Color%20cells%20of%20a%20worksheet%20by%20comparing%20entries%20with%20other%20worksheet.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-846057%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F406336%22%20target%3D%22_blank%22%3E%40vicasso%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20you%20need%20to%20compare%20the%20values%20for%20the%20said%20plans%20on%20both%20the%20worksheets%20based%20on%20their%20Plan%20ID%2C%20there%20is%20no%20common%20Plan%20ID%20in%20both%20the%20sheets.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETo%20make%20it%20more%20clear%2C%20you%20should%20mock%20up%20the%20desired%20output%20manually%20based%20on%20the%20sample%20data%2C%20add%20comments%20in%20there%20to%20explain%20the%20logic%20behind%20the%20desired%20output%20and%20upload%20the%20sample%20workbook%20again%20so%20that%20we%20can%20visualize%20what%20you%20are%20trying%20to%20achieve.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAlso%2C%20what%20do%20you%20mean%20when%20you%20say%20%22I%20have%20to%20automate%20this%20step%20along%20multiple%20sheet(for%20different%20states)%22%3F%3C%2FP%3E%3CP%3EIt%20would%20be%20better%20if%20you%20include%20that%20scenario%20as%20well%20in%20the%20sample%20workbook%20so%20that%20it%20is%20clear%20what%20would%20be%20the%20layout%20in%20case%20of%20multiple%20states.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-846080%22%20slang%3D%22en-US%22%3ERe%3A%20Color%20cells%20of%20a%20worksheet%20by%20comparing%20entries%20with%20other%20worksheet.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-846080%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F394231%22%20target%3D%22_blank%22%3E%40Subodh_Tiwari_sktneer%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20to%20compare%20the%20plans%20on%20the%20*COMPETITOR*%20worksheet%20with%20the%20Plans%20of%20*PLAN*%20Worksheet.%20Comparison%20is%20to%20be%20done%20between%20%3CSTRONG%3EPlan%20ID%3C%2FSTRONG%3Ein%20Row%201%20and%20%3CSTRONG%3EPlan%20ID%20for%20comparison%26nbsp%3B%3C%2FSTRONG%3Ein%20Row%203.%26nbsp%3B%3C%2FP%3E%3CP%3ERow%203%20values%20can%20be%20found%20in%20*PLAN*%20Worksheet.%26nbsp%3BI%20have%20added%20one%20more%20worksheet%20*FINAL%20RESULT*%20to%20show%20how%20the%20output%20is%20supposed%20to%20look.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20to%20write%20a%20VBA%20code%20that%20i%20can%20use%20in%20multiple%20sheets%20since%20i%20have%20to%20replicate%20the%20same%20process%20for%20different%20worksheets.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-846132%22%20slang%3D%22en-US%22%3ERe%3A%20Color%20cells%20of%20a%20worksheet%20by%20comparing%20entries%20with%20other%20worksheet.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-846132%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F406336%22%20target%3D%22_blank%22%3E%40vicasso%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20find%20the%20attached%20with%20the%20code%20on%20Module1%20and%20a%20button%20called%20%22Compare%20Plans%22%20on%20Competitor%20Sheet.%20You%20may%20click%20this%20button%20to%20run%20the%20code.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhen%20the%20code%20executes%2C%20it%20will%20prompt%20you%20to%20select%20any%20cell%20on%20the%20Competitor%20Sheet.%20To%20do%20so%2C%20if%20the%20Competitor%20Sheet%20is%20not%20currently%20the%20active%20sheet%2C%20activate%20the%20Competitor%20Sheet%20and%20select%20any%20random%20cell%20on%20the%20Competitor%20Sheet%20to%20set%20the%20Competitor%20Sheet.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESame%20way%20you%20will%20get%20a%20prompt%20to%20select%20any%20cell%20on%20the%20Plan%20Sheet%20and%20you%20can%20activate%20the%20Plan%20Sheet%20in%20the%20workbook%20and%20select%20any%20random%20cell%20on%20the%20Plan%20Sheet%20to%20set%20the%20Plan%20Sheet.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOnce%20you%20define%20the%20Competitor%20Sheet%20and%20the%20Plan%20Sheet%2C%20the%20code%20will%20compare%20the%20Plans%20as%20per%20your%20requirement%20and%20apply%20the%20relevant%20background%20color%20to%20the%20cells%20on%20the%20Competitor%20Sheet.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20made%20this%20setup%20so%20that%20you%20can%20run%20this%20code%20for%20any%20State%20Sheet%20and%20choose%20whatever%20Competitor%20Sheet%20you%20wish%20to%20compare%20irrespective%20of%20it's%20name%20with%20the%20whatever%20is%20the%20Plan%20Sheet.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EThe%20Code%20on%20Module1%3A%3C%2FSTRONG%3E%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3ESub%20PlanIDComparison()%0ADim%20wsCompetitor%20%20%20%20%20%20%20%20As%20Worksheet%0ADim%20wsPlan%20%20%20%20%20%20%20%20%20%20%20%20%20%20As%20Worksheet%0ADim%20x%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20As%20Variant%0ADim%20Plan%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20As%20Object%0ADim%20i%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20As%20Long%0ADim%20j%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20As%20Long%0A%0AOn%20Error%20Resume%20Next%0ASet%20wsCompetitor%20%3D%20Application.InputBox(%22Please%20activate%20the%20Competitor%20Sheet%20you%20want%20to%20compare.%22%20%26amp%3B%20vbNewLine%20%26amp%3B%20_%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22And%20select%20any%20cell%20on%20that%20Sheet%20to%20set%20the%20Competitor%20Sheet.%22%2C%20%22Select%20Competitor%20Sheet!%22%2C%20Type%3A%3D8).Parent%0A%0AIf%20wsCompetitor%20Is%20Nothing%20Then%0A%20%20%20%20MsgBox%20%22You%20didn't%20select%20the%20Competitor%20Sheet.%22%2C%20vbExclamation%0A%20%20%20%20Exit%20Sub%0AEnd%20If%0A%0AOn%20Error%20Resume%20Next%0ASet%20wsPlan%20%3D%20Application.InputBox(%22Please%20activate%20the%20Plan%20Sheet%20you%20want%20to%20compare%20the%20Competitor%20Sheet%20with.%22%20%26amp%3B%20vbNewLine%20%26amp%3B%20_%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22And%20select%20any%20cell%20on%20that%20Sheet%20to%20set%20the%20Plan%20Sheet.%22%2C%20%22Select%20Plan%20Sheet!%22%2C%20Type%3A%3D8).Parent%0A%0AIf%20wsPlan%20Is%20Nothing%20Then%0A%20%20%20%20MsgBox%20%22You%20didn't%20select%20the%20Plan%20Sheet.%22%2C%20vbExclamation%0A%20%20%20%20Exit%20Sub%0AEnd%20If%0A%0Ax%20%3D%20wsPlan.Range(%22A1%22).CurrentRegion.Value%0ASet%20Plan%20%3D%20CreateObject(%22Scripting.Dictionary%22)%0A%0AFor%20i%20%3D%203%20To%20UBound(x%2C%201)%0A%20%20%20%20For%20j%20%3D%202%20To%20UBound(x%2C%202)%0A%20%20%20%20%20%20%20%20Plan.Item(x(1%2C%20j)%20%26amp%3B%20x(2%2C%20j)%20%26amp%3B%20x(i%2C%201))%20%3D%20x(i%2C%20j)%0A%20%20%20%20Next%20j%0ANext%20i%0A%0Ax%20%3D%20wsCompetitor.Range(%22A1%22).CurrentRegion.Value%0AwsCompetitor.Range(%22A1%22).CurrentRegion.Offset(%2C%201).Interior.ColorIndex%20%3D%20xlNone%0A%0AFor%20i%20%3D%204%20To%20UBound(x%2C%201)%0A%20%20%20%20For%20j%20%3D%201%20To%20UBound(x%2C%202)%0A%20%20%20%20%20%20%20%20If%20Plan.exists(x(3%2C%20j)%20%26amp%3B%20x(2%2C%20j)%20%26amp%3B%20x(i%2C%201))%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20If%20x(i%2C%20j)%20%26gt%3B%20Plan(x(3%2C%20j)%20%26amp%3B%20x(2%2C%20j)%20%26amp%3B%20x(i%2C%201))%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20wsCompetitor.Cells(i%2C%20j).Interior.Color%20%3D%20vbRed%0A%20%20%20%20%20%20%20%20%20%20%20%20ElseIf%20x(i%2C%20j)%20%26lt%3B%3D%20Plan(x(3%2C%20j)%20%26amp%3B%20x(2%2C%20j)%20%26amp%3B%20x(i%2C%201))%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20wsCompetitor.Cells(i%2C%20j).Interior.Color%20%3D%20RGB(0%2C%20176%2C%2080)%0A%20%20%20%20%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20Next%20j%0ANext%20i%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-848996%22%20slang%3D%22en-US%22%3ERe%3A%20Color%20cells%20of%20a%20worksheet%20by%20comparing%20entries%20with%20other%20worksheet.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-848996%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F394231%22%20target%3D%22_blank%22%3E%40Subodh_Tiwari_sktneer%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ecan%20you%20help%20me%20understand%20the%20working%20functionality%20of%20this%20code%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-849183%22%20slang%3D%22en-US%22%3ERe%3A%20Color%20cells%20of%20a%20worksheet%20by%20comparing%20entries%20with%20other%20worksheet.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-849183%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F406336%22%20target%3D%22_blank%22%3E%40vicasso%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20let%20me%20know%20which%20part%20do%20you%20find%20difficult%20to%20understand.%3C%2FP%3E%3CP%3ETo%20fully%20understand%20the%20code%20you%20must%20have%20an%20idea%20about%20Arrays%20and%20Dictionary.%20If%20you%20are%20not%20comfortable%20with%20them%2C%20please%20read%20about%20them%20first.%20There%20are%20plenty%20of%20study%20material%20available%20online%20even%20videos.%20You%20may%20search%20on%20YouTube%20for%20Excel%20VBA%20arrays%20tutorial%20and%20Dictionary%20tutorial.%3C%2FP%3E%3CP%3ELet%20me%20know%20if%20you%20don't%20find%20one.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-855530%22%20slang%3D%22en-US%22%3ERe%3A%20Color%20cells%20of%20a%20worksheet%20by%20comparing%20entries%20with%20other%20worksheet.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-855530%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F394231%22%20target%3D%22_blank%22%3E%40Subodh_Tiwari_sktneer%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3ECan%20you%20explain%20to%20me%20this%20part%20of%20code.%0A------------------------------------------------------------------------------------------%0Ax%20%3D%20wsPlan.Range(%22A1%22).CurrentRegion.Value%0ASet%20Plan%20%3D%20CreateObject(%22Scripting.Dictionary%22)%0A%0AFor%20i%20%3D%203%20To%20UBound(x%2C%201)%0A%20%20%20%20For%20j%20%3D%202%20To%20UBound(x%2C%202)%0A%20%20%20%20%20%20%20%20Plan.Item(x(1%2C%20j)%20%26amp%3B%20x(2%2C%20j)%20%26amp%3B%20x(i%2C%201))%20%3D%20x(i%2C%20j)%0A%20%20%20%20Next%20j%0ANext%20i%0A----------------------------------------------------------------------------------------%0AThanks%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-855649%22%20slang%3D%22en-US%22%3ERe%3A%20Color%20cells%20of%20a%20worksheet%20by%20comparing%20entries%20with%20other%20worksheet.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-855649%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F406336%22%20target%3D%22_blank%22%3E%40vicasso%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ex%20%3D%20wsPlan.Range(%22A1%22).CurrentRegion.Value%3C%2FP%3E%3CP%3EThe%20Current%20Region%20of%20A1%20is%20the%20range%20A1%3AE5.%20If%20you%20manually%20select%20the%20cell%20A1%20on%20Plan%20Sheet%20and%20hit%20Ctrl%2B*%20it%20will%20select%20the%20range%20A1%3AE5%20and%20this%20is%20the%20current%20region%20for%20cell%20A1%20i.e.%20all%20the%20adjacent%20rows%20and%20columns%20with%20data%20with%20no%20blank%20row%20or%20column%20in%20between.%3C%2FP%3E%3CP%3EThe%20line%20reads%20all%20the%20value%20of%20the%20current%20region%20into%20the%20array%20x.%3C%2FP%3E%3CP%3ESo%20now%20the%20array%20x%20will%20contain%20all%20the%20data%20in%20the%20range%20A1%3AE5.%3C%2FP%3E%3CP%3EArray%20x%20has%20two%20dimensions%2C%20one%20dimension%20is%20for%20rows%20and%20another%20for%20columns.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESet%20Plan%20%3D%20CreateObject(%22Scripting.Dictionary%22)%3C%2FP%3E%3CP%3EHere%20a%20dictionary%20called%20Plan%20is%20being%20created%20to%20store%20the%20actual%20data%20from%20the%20actual%20data%20range%20B3%3AE5%20for%20specific%20criteria%20Plan%20ID%2C%20State%20and%20the%20column%20header.%3C%2FP%3E%3CP%3Ee.g.%20B3%20has%20a%20value%20400%20which%20is%20specific%20for%20Plan%20ID%20Plan%20Premium%2C%20State%20CA%20and%20column%20header%201300.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFor%20i%20%3D%203%20To%20UBound(x%2C%201)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%20For%20j%20%3D%202%20To%20UBound(x%2C%202)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Plan.Item(x(1%2C%20j)%20%26amp%3B%20x(2%2C%20j)%20%26amp%3B%20x(i%2C%201))%20%3D%20x(i%2C%20j)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%20Next%20j%3CBR%20%2F%3ENext%20i%3C%2FP%3E%3CP%3EIn%20the%20above%20lines%2C%20we%20are%20looping%20through%20the%20array%20x%20and%20populating%20the%20dictionary%20Plan%20with%20the%20combination%20of%20Plan%20ID%2C%20State%20and%20Column%20Header%20as%20a%20Key%20and%20the%20actual%20value%20as%20Item%20in%20the%20following%20line...%3C%2FP%3E%3CP%3EPlan.Item(x(1%2C%20j)%20%26amp%3B%20x(2%2C%20j)%20%26amp%3B%20x(i%2C%201))%20%3D%20x(i%2C%20j)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ee.g.%20in%20the%20above%20line%20i%20stands%20for%20Rows%20and%20j%20stands%20for%20Columns%20and%20we%20start%20the%20row%20loop%20with%20i%3D3%20and%20column%20loop%20with%20j%3D2%20i.e.%20Row3%20and%20column%202%20which%20should%20be%20cell%20B3%20on%20the%20Plan%20Sheet.%3C%2FP%3E%3CP%3ESo%20take%20the%20example%20of%20B3%2C%20if%20we%20translate%20the%20above%20line%20of%20code%20into%20values%2C%20it%20is%20like%20this...%3C%2FP%3E%3CP%3EPlan.Item(1300%20%26amp%3B%20CA%20%26amp%3B%20Plan%20Premium)%20%3D%20400%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20the%20dictionary%20Plan%20will%20have%20a%20unique%20key%20%221300CAPlan%20Premium%22%20with%20a%20value%20400%20assigned%20to%20it.%3C%2FP%3E%3CP%3EAnd%20later%20we%20can%20loop%20through%20the%20data%20on%20the%20Competitor%20Sheet%20and%20check%20if%20a%20same%20combination%20is%20found%20there%20and%20if%20yes%2C%20we%20can%20compare%20the%20stored%20value%20in%20the%20dictionary%20for%20that%20key%20with%20the%20value%20on%20the%20Competitor%20Sheet%20and%20highlight%20the%20cells%20accordingly.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHope%20I%20was%20able%20to%20explain%20it.%3C%2FP%3E%3C%2FLINGO-BODY%3E
vicasso
Occasional Contributor

In the attached workbook, i have to compare the *COMPETITOR* Worksheet with the *PLAN* Worksheet. The comparison is to be done between Plans Premium, Plan_Amount and Item 1 Price in both the sheets and conditionally format the Competitor worksheets entry as blue and red depending on the values( if lower or higher than the values in Plan worksheet).

 

I have to automate this step along multiple sheet(for different states). Kindly help me out here. Apologies for lack of clarity,
i will try to frame the question in a better way if it's not clear.

 

 

7 Replies
Highlighted

@vicasso 

If you need to compare the values for the said plans on both the worksheets based on their Plan ID, there is no common Plan ID in both the sheets.

 

To make it more clear, you should mock up the desired output manually based on the sample data, add comments in there to explain the logic behind the desired output and upload the sample workbook again so that we can visualize what you are trying to achieve.

 

Also, what do you mean when you say "I have to automate this step along multiple sheet(for different states)"?

It would be better if you include that scenario as well in the sample workbook so that it is clear what would be the layout in case of multiple states.

Hi @Subodh_Tiwari_sktneer 

 

I have to compare the plans on the *COMPETITOR* worksheet with the Plans of *PLAN* Worksheet. Comparison is to be done between Plan ID in Row 1 and Plan ID for comparison in Row 3. 

Row 3 values can be found in *PLAN* Worksheet. I have added one more worksheet *FINAL RESULT* to show how the output is supposed to look. 

 

I have to write a VBA code that i can use in multiple sheets since i have to replicate the same process for different worksheets. 

Solution

@vicasso 

 

Please find the attached with the code on Module1 and a button called "Compare Plans" on Competitor Sheet. You may click this button to run the code.

 

When the code executes, it will prompt you to select any cell on the Competitor Sheet. To do so, if the Competitor Sheet is not currently the active sheet, activate the Competitor Sheet and select any random cell on the Competitor Sheet to set the Competitor Sheet.

 

Same way you will get a prompt to select any cell on the Plan Sheet and you can activate the Plan Sheet in the workbook and select any random cell on the Plan Sheet to set the Plan Sheet.

 

Once you define the Competitor Sheet and the Plan Sheet, the code will compare the Plans as per your requirement and apply the relevant background color to the cells on the Competitor Sheet.

 

I have made this setup so that you can run this code for any State Sheet and choose whatever Competitor Sheet you wish to compare irrespective of it's name with the whatever is the Plan Sheet.

 

The Code on Module1:

Sub PlanIDComparison()
Dim wsCompetitor        As Worksheet
Dim wsPlan              As Worksheet
Dim x                   As Variant
Dim Plan                As Object
Dim i                   As Long
Dim j                   As Long

On Error Resume Next
Set wsCompetitor = Application.InputBox("Please activate the Competitor Sheet you want to compare." & vbNewLine & _
                                "And select any cell on that Sheet to set the Competitor Sheet.", "Select Competitor Sheet!", Type:=8).Parent

If wsCompetitor Is Nothing Then
    MsgBox "You didn't select the Competitor Sheet.", vbExclamation
    Exit Sub
End If

On Error Resume Next
Set wsPlan = Application.InputBox("Please activate the Plan Sheet you want to compare the Competitor Sheet with." & vbNewLine & _
                                "And select any cell on that Sheet to set the Plan Sheet.", "Select Plan Sheet!", Type:=8).Parent

If wsPlan Is Nothing Then
    MsgBox "You didn't select the Plan Sheet.", vbExclamation
    Exit Sub
End If

x = wsPlan.Range("A1").CurrentRegion.Value
Set Plan = CreateObject("Scripting.Dictionary")

For i = 3 To UBound(x, 1)
    For j = 2 To UBound(x, 2)
        Plan.Item(x(1, j) & x(2, j) & x(i, 1)) = x(i, j)
    Next j
Next i

x = wsCompetitor.Range("A1").CurrentRegion.Value
wsCompetitor.Range("A1").CurrentRegion.Offset(, 1).Interior.ColorIndex = xlNone

For i = 4 To UBound(x, 1)
    For j = 1 To UBound(x, 2)
        If Plan.exists(x(3, j) & x(2, j) & x(i, 1)) Then
            If x(i, j) > Plan(x(3, j) & x(2, j) & x(i, 1)) Then
                wsCompetitor.Cells(i, j).Interior.Color = vbRed
            ElseIf x(i, j) <= Plan(x(3, j) & x(2, j) & x(i, 1)) Then
                wsCompetitor.Cells(i, j).Interior.Color = RGB(0, 176, 80)
            End If
        End If
    Next j
Next i
End Sub

 

Hi @Subodh_Tiwari_sktneer 

 

can you help me understand the working functionality of this code?

 

Thanks

 

@vicasso 

Please let me know which part do you find difficult to understand.

To fully understand the code you must have an idea about Arrays and Dictionary. If you are not comfortable with them, please read about them first. There are plenty of study material available online even videos. You may search on YouTube for Excel VBA arrays tutorial and Dictionary tutorial.

Let me know if you don't find one.

@Subodh_Tiwari_sktneer 

Can you explain to me this part of code.
------------------------------------------------------------------------------------------
x = wsPlan.Range("A1").CurrentRegion.Value
Set Plan = CreateObject("Scripting.Dictionary")

For i = 3 To UBound(x, 1)
    For j = 2 To UBound(x, 2)
        Plan.Item(x(1, j) & x(2, j) & x(i, 1)) = x(i, j)
    Next j
Next i
----------------------------------------------------------------------------------------
Thanks

@vicasso 

x = wsPlan.Range("A1").CurrentRegion.Value

The Current Region of A1 is the range A1:E5. If you manually select the cell A1 on Plan Sheet and hit Ctrl+* it will select the range A1:E5 and this is the current region for cell A1 i.e. all the adjacent rows and columns with data with no blank row or column in between.

The line reads all the value of the current region into the array x.

So now the array x will contain all the data in the range A1:E5.

Array x has two dimensions, one dimension is for rows and another for columns.

 

Set Plan = CreateObject("Scripting.Dictionary")

Here a dictionary called Plan is being created to store the actual data from the actual data range B3:E5 for specific criteria Plan ID, State and the column header.

e.g. B3 has a value 400 which is specific for Plan ID Plan Premium, State CA and column header 1300.

 

For i = 3 To UBound(x, 1)
   For j = 2 To UBound(x, 2)
      Plan.Item(x(1, j) & x(2, j) & x(i, 1)) = x(i, j)
   Next j
Next i

In the above lines, we are looping through the array x and populating the dictionary Plan with the combination of Plan ID, State and Column Header as a Key and the actual value as Item in the following line...

Plan.Item(x(1, j) & x(2, j) & x(i, 1)) = x(i, j)

 

e.g. in the above line i stands for Rows and j stands for Columns and we start the row loop with i=3 and column loop with j=2 i.e. Row3 and column 2 which should be cell B3 on the Plan Sheet.

So take the example of B3, if we translate the above line of code into values, it is like this...

Plan.Item(1300 & CA & Plan Premium) = 400

 

So the dictionary Plan will have a unique key "1300CAPlan Premium" with a value 400 assigned to it.

And later we can loop through the data on the Competitor Sheet and check if a same combination is found there and if yes, we can compare the stored value in the dictionary for that key with the value on the Competitor Sheet and highlight the cells accordingly.

 

Hope I was able to explain it.

Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
16 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
*Updated 9/3* Syncing in Microsoft Edge Preview Channels
Elliot Kirk in Articles on
217 Replies