SOLVED
Home

Help - Cancel MsgBox If Condition is Met

%3CLINGO-SUB%20id%3D%22lingo-sub-520376%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20-%20Cancel%20MsgBox%20If%20Condition%20is%20Met%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-520376%22%20slang%3D%22en-US%22%3E%3CP%3EHello%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F330615%22%20target%3D%22_blank%22%3E%40ACPNR%3C%2FA%3E%26nbsp%3B%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20think%20all%20you%20need%20to%20do%20is%20add%20another%20condition%20to%20the%20IF%20statement%20so%20it%20says%20in%20words%20%22If%20the%20Code%20is%20X%20and%20the%20cell%20next%20to%20it%20is%20blank%22%20then%20show%20the%20message.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20only%20need%20to%20loop%20through%20the%20codes%20in%20column%20H%2C%26nbsp%3B%20and%20you%20don't%20need%20the%20%24%20signs%20in%20the%20range%20reference.%26nbsp%3B%20Also%2C%20when%20you%20dim%20several%20elements%20in%20one%20row%20of%20code%2C%20you%20need%20to%20specify%20the%20data%20type%20of%20each.%20If%20you%20don't%2C%20it%20will%20be%20a%20variant.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAlso%2C%20you%20don't%20want%20to%20loop%20through%20the%20whole%20list.%20You%20only%20want%20to%20check%20the%20row%20where%20the%20data%20was%20entered%2C%20i.e.%20the%20target%20cell%20in%20column%20H.%20Therefore%2C%20the%20code%20should%20only%20watch%20column%20H%2C%20not%20column%20I.%26nbsp%3B%20When%20you%20operate%20on%20the%20target%20cell%2C%20you%20don't%20need%20to%20declare%20any%20ranges%20at%20all.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3EPrivate%20Sub%20Worksheet_Change(ByVal%20Target%20As%20Range)%0A%20%20%20%20If%20Not%20Intersect(Target%2C%20Range(%22H15%3AH32%22))%20Is%20Nothing%20Then%0A%20%20%20%20%20%20%20%20If%20Target.Value%20%3D%20%22X%22%20And%20Len(Target.Offset(0%2C%201))%20%3D%200%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20MsgBox%20(%22Please%20include%20a%20repair%20comment%20for%20corresponding%20line.%22)%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20End%20If%0A%0AEnd%20Sub%0A%3C%2FPRE%3E%0A%3CP%3EIf%20you%20want%20to%20cater%20for%20a%20scenario%20where%20several%20rows%20of%20values%20are%20filled%20in%20at%20once%2C%20for%20example%20with%20a%20copy%20%2F%20paste%20operation%2C%20THEN%20you%20would%20need%20to%20loop%20through%20all%20the%20cells%20in%20the%20target.%20In%20that%20case%20it%20would%20also%20help%20to%20give%20the%20user%20an%20indication%20which%20row%20needs%20to%20be%20addressed.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3EPrivate%20Sub%20Worksheet_Change(ByVal%20Target%20As%20Range)%0ADim%20cel%20As%20Range%0A%20%20%20%20If%20Not%20Intersect(Target%2C%20Range(%22H15%3AH32%22))%20Is%20Nothing%20Then%0A%20%20%20%20%20%20%20%20For%20Each%20cel%20In%20Target%0A%20%20%20%20%20%20%20%20%20%20%20%20If%20cel.Value%20%3D%20%22X%22%20And%20Len(cel.Offset(0%2C%201))%20%3D%200%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20MsgBox%20(%22Please%20include%20a%20repair%20comment%20for%20row%20%22%20%26amp%3B%20cel.Row%20%26amp%3B%20%22.%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20%20%20%20%20Next%20cel%0A%20%20%20%20End%20If%0A%0AEnd%20Sub%3C%2FPRE%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20988px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F111877i5D7912EA08C8363E%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%222019-05-03_10-03-58.png%22%20title%3D%222019-05-03_10-03-58.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-535226%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20-%20Cancel%20MsgBox%20If%20Condition%20is%20Met%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-535226%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F7724%22%20target%3D%22_blank%22%3E%40Ingeborg%20Hawighorst%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20tried%20the%20first%20code%20you%20wrote%2C%20but%20it%20didn't%20work%20properly.%20I%20understand%20what%20you%20mean%20though.%20However%2C%20I%20still%20get%20the%20message%20box%20popping%20up%20even%20if%20the%20repair%20comments%20are%20filled.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-516636%22%20slang%3D%22en-US%22%3EHelp%20-%20Cancel%20MsgBox%20If%20Condition%20is%20Met%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-516636%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20trying%20to%20write%20a%20macro%20that%20does%20one%20thing%20or%20the%20other.%20There%20are%20two%20ranges%3A%20Code%20(H15%3AI32)%20and%20Repair_Comments%20(N15%3AR32).%20First%2C%20if%20X%20is%20marked%20in%20Code%20range%20then%20a%20message%20box%20should%20appear%20saying%2C%20%22Please%20include%20a%20repair%20comment%20for%20corresponding%20line.%22%20I%20got%20this%20part%20to%20work%20on%20its%20own.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPrivate%20Sub%20Worksheet_Change(ByVal%20Target%20As%20Range)%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BIf%20Not%20Intersect(Target%2C%20Range(%22H15%3AI32%22))%20Is%20Nothing%20Then%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20Dim%20Code%2C%20Repair_Comments%20As%20Range%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20For%20Each%20Code%20In%20Range(%22%24H%2415%3A%24I%2432%22)%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BIf%20Code.Value%20%3D%20%22X%22%20Then%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BMsgBox%20(%22Please%20include%20a%20repair%20comment%20for%20corresponding%20line.%22)%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BEnd%20If%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BNext%20Code%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BEnd%20If%3C%2FP%3E%3CP%3EEnd%20Sub%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHowever%2C%20the%20message%20box%20continues%20to%20pop%20up%20as%20long%20as%20the%20X%20is%20marked%20even%20if%20the%20user%20writes%20a%20repair%20comment.%20I%20want%20to%20prevent%20the%20message%20box%20from%20popping%20up%20if%20a%20repair%20comment%20is%20added.%20However%2C%20if%20an%20X%20is%20marked%20and%20has%20a%20repair%20comment%20and%26nbsp%3B%3CEM%3Eanother%3C%2FEM%3E%20X%20is%20marked%20but%20is%20missing%20a%20repair%20comment%2C%20then%20I%20want%20the%20message%20box%20to%20pop%20up%20again.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20I%20want%20to%20be%20able%20to%20stop%20the%20message%20box%20from%20popping%20if%20they%20meet%20the%20requirement%20of%20inputting%20a%20comment%2C%20but%20as%20long%20as%20there%20is%20an%20X%20missing%20the%20requirement%20of%20inputting%20a%20comment%20then%20I%20want%20the%20box%20to%20show.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20made%20an%20attempt%20but%20I%20can't%20seem%20to%20figure%20it%20out.%20This%20is%20the%20rest%20of%20the%20code%20I've%20attempted%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPrivate%20Sub%20Worksheet_Change(ByVal%20Target%20As%20Range)%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BIf%20Not%20Intersect(Target%2C%20Range(%22H15%3AI32%22))%20Is%20Nothing%20Then%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20Dim%20Code%2C%20Repair_Comments%20As%20Range%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20For%20Each%20Code%20In%20Range(%22%24H%2415%3A%24I%2432%22)%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BIf%20Code.Value%20%3D%20%22X%22%20Then%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BMsgBox%20(%22Please%20include%20a%20repair%20comment%20for%20corresponding%20line.%22)%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BEnd%20If%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BNext%20Code%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20For%20Each%20cell%20In%20Intersect(Range(%22H15%3AI32%2CN15%3AR32%22)%2C%20ActiveSheet.UsedRange)%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BIf%20Code.Value%20%3D%20%22X%22%20And%20Repair_Comments%20%3D%20Not%20Blank%20Then%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BCancel%20MsgBox%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BEnd%20If%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BNext%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BEnd%20If%3CBR%20%2F%3EEnd%20Sub%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%20728px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F112292i5B93D682FFFB3919%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22macro.PNG%22%20title%3D%22macro.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20help%20will%20be%20appreciated!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-516636%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-536398%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20-%20Cancel%20MsgBox%20If%20Condition%20is%20Met%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-536398%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F330615%22%20target%3D%22_blank%22%3E%40ACPNR%3C%2FA%3E%26nbsp%3B%20the%20code%20I%20wrote%20will%20not%20pop%20up%20for%20every%20row.%20Post%20the%20code%20you%20are%20using.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-536603%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20-%20Cancel%20MsgBox%20If%20Condition%20is%20Met%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-536603%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F7724%22%20target%3D%22_blank%22%3E%40Ingeborg%20Hawighorst%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20also%20edited%20the%20original%20post%20with%20a%20photo%20when%20I%20edit%20another%20row%20within%20the%20range%20and%20get%20the%20pop%20up%20message%20despite%20having%20the%20other%20cell%20filled%20in.%20It%20seems%20to%20ignore%20the%20second%20condition%20for%20some%20reason.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20is%20the%20code%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPrivate%20Sub%20Worksheet_Change(ByVal%20Target%20As%20Range)%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BIf%20Not%20Intersect(Target%2C%20Range(%22H15%3AI32%22))%20Is%20Nothing%20Then%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20Dim%20Code%20As%20Range%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20For%20Each%20Code%20In%20Range(%22H15%3AH32%22)%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20If%20Code.Value%20%3D%20%22X%22%20And%20Len(Target.Offset(0%2C%202))%20%3D%200%20Then%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20MsgBox%20(%22Please%20include%20a%20repair%20comment%20for%20corresponding%20line.%22)%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20End%20If%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20Next%20Code%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BEnd%20If%3CBR%20%2F%3EEnd%20Sub%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-550026%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20-%20Cancel%20MsgBox%20If%20Condition%20is%20Met%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-550026%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F330615%22%20target%3D%22_blank%22%3E%40ACPNR%3C%2FA%3E%26nbsp%3BWell%2C%20that%20is%20not%20the%20code%20I%20posted%2C%20is%20it%3F%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhat%20happens%20if%20you%20try%20the%20code%20I%20posted%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-550209%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20-%20Cancel%20MsgBox%20If%20Condition%20is%20Met%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-550209%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F7724%22%20target%3D%22_blank%22%3E%40Ingeborg%20Hawighorst%3C%2FA%3E%26nbsp%3BLike%20I%20said%2C%20the%20code%20you%20gave%20me%20didn't%20work%20properly%20or%20the%20way%20it%20was%20intended%20to.%20It%20worked%20in%20the%20sense%20of%20syntax%20(I%20didn't%20get%20any%20errors%20or%20bugs)%2C%20but%20as%20mentioned%20earlier%20the%20message%20still%20popped%20up.%20I%20copy%20pasted%20it%3B%20didn't%20work.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20tried%20playing%20around%20with%20it%20and%20got%20some%20errors%2Fbugs%20sometimes%2C%20other%20times%20I%20wouldn't%20get%20errors%2Fbugs%20but%20would%20get%20the%20message%20popping%20up%2C%20and%20other%20times%20I%20wouldn't%20get%20errors%2Fbugs%20but%20the%20message%20wouldn't%20pop%20up.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-550367%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20-%20Cancel%20MsgBox%20If%20Condition%20is%20Met%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-550367%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F330615%22%20target%3D%22_blank%22%3E%40ACPNR%3C%2FA%3E%26nbsp%3BI'm%20attaching%20a%20file%20with%20my%20code.%20It%20works%20fine%20for%20me.%20Let%20me%20know%20how%20you%20get%20on%20with%20this.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-552851%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20-%20Cancel%20MsgBox%20If%20Condition%20is%20Met%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-552851%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F7724%22%20target%3D%22_blank%22%3E%40Ingeborg%20Hawighorst%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYeah%20it%20ended%20up%20working%20this%20time%20(I%20don't%20know%20why%20it%20didn't%20work%20the%20first%20time%20though).%20Thank%20you%20for%20helping%20me%20out.%20I%20really%20appreciate%20it.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
ACPNR
Occasional Contributor

Hello,

 

I'm trying to write a macro that does one thing or the other. There are two ranges: Code (H15:I32) and Repair_Comments (N15:R32). First, if X is marked in Code range then a message box should appear saying, "Please include a repair comment for corresponding line." I got this part to work on its own.

 

Private Sub Worksheet_Change(ByVal Target As Range)
     If Not Intersect(Target, Range("H15:I32")) Is Nothing Then
          Dim Code, Repair_Comments As Range
          For Each Code In Range("$H$15:$I$32")
               If Code.Value = "X" Then
               MsgBox ("Please include a repair comment for corresponding line.")
               End If
               Next Code

     End If

End Sub

 

However, the message box continues to pop up as long as the X is marked even if the user writes a repair comment. I want to prevent the message box from popping up if a repair comment is added. However, if an X is marked and has a repair comment and another X is marked but is missing a repair comment, then I want the message box to pop up again.

 

So I want to be able to stop the message box from popping if they meet the requirement of inputting a comment, but as long as there is an X missing the requirement of inputting a comment then I want the box to show.

 

I've made an attempt but I can't seem to figure it out. This is the rest of the code I've attempted:

 

Private Sub Worksheet_Change(ByVal Target As Range)
     If Not Intersect(Target, Range("H15:I32")) Is Nothing Then
          Dim Code, Repair_Comments As Range
          For Each Code In Range("$H$15:$I$32")
               If Code.Value = "X" Then
               MsgBox ("Please include a repair comment for corresponding line.")
               End If
               Next Code
          For Each cell In Intersect(Range("H15:I32,N15:R32"), ActiveSheet.UsedRange)
               If Code.Value = "X" And Repair_Comments = Not Blank Then
               Cancel MsgBox
               End If
               Next
     End If
End Sub

 

macro.PNG

 

Any help will be appreciated!

8 Replies
Solution

Hello @ACPNR ,

 

I think all you need to do is add another condition to the IF statement so it says in words "If the Code is X and the cell next to it is blank" then show the message.

 

You only need to loop through the codes in column H,  and you don't need the $ signs in the range reference.  Also, when you dim several elements in one row of code, you need to specify the data type of each. If you don't, it will be a variant. 

 

Also, you don't want to loop through the whole list. You only want to check the row where the data was entered, i.e. the target cell in column H. Therefore, the code should only watch column H, not column I.  When you operate on the target cell, you don't need to declare any ranges at all. 

 

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("H15:H32")) Is Nothing Then
        If Target.Value = "X" And Len(Target.Offset(0, 1)) = 0 Then
                MsgBox ("Please include a repair comment for corresponding line.")
        End If
    End If

End Sub

If you want to cater for a scenario where several rows of values are filled in at once, for example with a copy / paste operation, THEN you would need to loop through all the cells in the target. In that case it would also help to give the user an indication which row needs to be addressed.

 

Private Sub Worksheet_Change(ByVal Target As Range)
Dim cel As Range
    If Not Intersect(Target, Range("H15:H32")) Is Nothing Then
        For Each cel In Target
            If cel.Value = "X" And Len(cel.Offset(0, 1)) = 0 Then
                    MsgBox ("Please include a repair comment for row " & cel.Row & ".")
            End If
        Next cel
    End If

End Sub

2019-05-03_10-03-58.png

 

 

@Ingeborg Hawighorst 

I tried the first code you wrote, but it didn't work properly. I understand what you mean though. However, I still get the message box popping up even if the repair comments are filled.

@ACPNR  the code I wrote will not pop up for every row. Post the code you are using.

@Ingeborg Hawighorst 

I also edited the original post with a photo when I edit another row within the range and get the pop up message despite having the other cell filled in. It seems to ignore the second condition for some reason.

 

This is the code:

 

Private Sub Worksheet_Change(ByVal Target As Range)
     If Not Intersect(Target, Range("H15:I32")) Is Nothing Then
          Dim Code As Range
          For Each Code In Range("H15:H32")
          If Code.Value = "X" And Len(Target.Offset(0, 2)) = 0 Then
          MsgBox ("Please include a repair comment for corresponding line.")
          End If
          Next Code
     End If
End Sub

@ACPNR Well, that is not the code I posted, is it? 

What happens if you try the code I posted?

@Ingeborg Hawighorst Like I said, the code you gave me didn't work properly or the way it was intended to. It worked in the sense of syntax (I didn't get any errors or bugs), but as mentioned earlier the message still popped up. I copy pasted it; didn't work.

 

I tried playing around with it and got some errors/bugs sometimes, other times I wouldn't get errors/bugs but would get the message popping up, and other times I wouldn't get errors/bugs but the message wouldn't pop up.

@ACPNR I'm attaching a file with my code. It works fine for me. Let me know how you get on with this.

 

 

@Ingeborg Hawighorst 

Yeah it ended up working this time (I don't know why it didn't work the first time though). Thank you for helping me out. I really appreciate 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
Early preview of Microsoft Edge group policies
Sean Lyndersay in Discussions on
65 Replies