SOLVED
Home

Is there a way to use a macro to find a print area when it varies with each run?

%3CLINGO-SUB%20id%3D%22lingo-sub-830208%22%20slang%3D%22en-US%22%3EIs%20there%20a%20way%20to%20use%20a%20macro%20to%20find%20a%20print%20area%20when%20it%20varies%20with%20each%20run%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-830208%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20All...%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20a%20situation%20where%20I'm%20trying%20to%20set%20the%20print%20area%20for%2023%20different%20worksheets%20in%20one%20file%20using%20a%20macro.%26nbsp%3B%20The%20columns%20are%20the%20same%20on%20each%20worksheet%2C%20but%20number%20of%20rows%20is%20different%20in%20each%20one%20and%20will%20vary%20each%20time%20the%20spreadsheet%20is%20run.%26nbsp%3B%20When%20I%20try%20to%20use%20the%20standard%20method%20for%20creating%20a%20macro%20to%20set%20the%20print%20area%2C%20it%20always%20lists%20the%20rows%20and%20columns.%26nbsp%3B%20Is%20there%20a%20way%20to%20program%20it%20so%20it%20looks%20for%20the%20rows%20on%20its%20own%20each%20time%3F%26nbsp%3B%20Am%20I%20making%20any%20sense%3F%26nbsp%3B%20Happy%20to%20elaborate%20if%20you%20can%20tell%20me%20what%20you%20need.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20help%20would%20be%20greatly%20appreciated...thanks!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-830208%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-831090%22%20slang%3D%22en-US%22%3ERe%3A%20Is%20there%20a%20way%20to%20use%20a%20macro%20to%20find%20a%20print%20area%20when%20it%20varies%20with%20each%20run%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-831090%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F400666%22%20target%3D%22_blank%22%3E%40dchiggins54%3C%2FA%3EThis%20macro%20would%20set%20the%20print-area%20of%20each%20sheet%20for%20columns%20A%3AN%20for%20as%20many%20rows%20as%20there%20is%20data%20in%20the%20sheet%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3ESub%20SetPrintAreas()%0A%20%20%20%20Dim%20Sh%20As%20Worksheet%0A%20%20%20%20For%20Each%20Sh%20In%20Worksheets%0A%20%20%20%20%20%20%20%20With%20Sh.PageSetup%0A%20%20%20%20%20%20%20%20%20%20%20%20.PrintArea%20%3D%20Intersect(Sh.UsedRange%2C%20Sh.Range(%22A%3AN%22)).Address%0A%20%20%20%20%20%20%20%20End%20With%0A%20%20%20%20Next%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-831128%22%20slang%3D%22en-US%22%3ERe%3A%20Is%20there%20a%20way%20to%20use%20a%20macro%20to%20find%20a%20print%20area%20when%20it%20varies%20with%20each%20run%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-831128%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F22322%22%20target%3D%22_blank%22%3E%40Jan%20Karel%20Pieterse%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20so%20much%20for%20your%20kind%20response!%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20created%20the%20macro%20(copied%2Fpasted)%20and%20I%20get%20the%20error%20stating%20that%20only%20one%20cell%20has%20been%20selected%20(see%20attached%20file).%26nbsp%3B%20I%20was%20hoping%20that%20with%20a%20click%20of%20a%20button%20on%20a%20%22master%22%20worksheet%20in%20the%20file%2C%20the%20assigned%20macro%20would%20run%20through%20all%20of%20the%2023%20other%20worksheets%20and%20set%20the%20print%20area%20accordingly%20to%20each%20one.%26nbsp%3B%20This%20macro%20did%20not%20accomplish%20that%2C%20I'm%20afraid.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhat%20am%20I%20doing%20wrong%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CDIV%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-831159%22%20slang%3D%22en-US%22%3ERe%3A%20Is%20there%20a%20way%20to%20use%20a%20macro%20to%20find%20a%20print%20area%20when%20it%20varies%20with%20each%20run%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-831159%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F400666%22%20target%3D%22_blank%22%3E%40dchiggins54%3C%2FA%3EI%20was%20assuming%20the%20worksheets%20contain%20sufficient%20data%2C%20hence%20the%20intersection%20with%20the%20usedrange%20to%20get%20the%20correct%20number%20of%20rows.%20Apparently%20one%20or%20more%20worksheets%20contain%20just%20one%20cell%20with%20data%20in%20the%20desginated%20columns.%20YOu%20can%20avoid%20the%20message%20by%20adding%20this%20line%20of%20code%20to%20the%20macro%2C%20just%20below%20the%20Dim%20statement%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EApplication.DisplayAlerts%20%3D%20False%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-831229%22%20slang%3D%22en-US%22%3ERe%3A%20Is%20there%20a%20way%20to%20use%20a%20macro%20to%20find%20a%20print%20area%20when%20it%20varies%20with%20each%20run%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-831229%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F400666%22%20target%3D%22_blank%22%3E%40dchiggins54%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAdd%20one%20line%20to%20activate%20the%20sheet%20in%20your%20code%20like%20this...%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EFor%20Each%20Sh%20In%20Worksheets%0A%20%20%20%20Sh.Activate%0A%20%20%20%20With%20Sh.PageSetup%0A%20%20%20%20%20%20%20%20.PrintArea%20%3D%20Intersect(Sh.UsedRange%2C%20Sh.Range(%22A%3AN%22)).Address%0A%20%20%20%20End%20With%0ANext%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnd%20when%20the%20code%20produces%20an%20error%2C%20can%20you%20look%20at%20the%20ActiveSheet%20and%20confirm%20if%20that%20sheet%20is%20empty%3F%3C%2FP%3E%3CP%3EWhat%20is%20the%20layout%20of%20your%20data%20on%20each%20sheet%20in%20the%20workbook%3F%20Is%20the%20data%20scattered%20all%20over%20or%20it%20starts%20from%20row1%20and%20column%20A%20with%20no%20rows%20or%20columns%20in%20between%20the%20data%3F%3C%2FP%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-831333%22%20slang%3D%22en-US%22%3ERe%3A%20Is%20there%20a%20way%20to%20use%20a%20macro%20to%20find%20a%20print%20area%20when%20it%20varies%20with%20each%20run%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-831333%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%3CP%3EHi%20Subodh%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThere%20is%20always%20data%20on%20each%20worksheet.%26nbsp%3B%20They%20are%20never%20null...at%20least%2010%20rows%20of%20data%20or%20more.%26nbsp%3B%20The%20data%20is%20not%20scattered.%26nbsp%3B%20Starts%20in%20A1.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAre%20you%20saying%20to%20use%20the%20code%20you%20just%20gave%20me%20instead%20of%20the%20previous%20version%3F%26nbsp%3B%20I%20ask%20because%20the%20line%20you%20gave%20me%20in%20the%20previous%20post%20to%20add%20to%20the%20macro%20worked%2C%20so%20I%20want%20to%20be%20clear.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDebbie%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-831375%22%20slang%3D%22en-US%22%3ERe%3A%20Is%20there%20a%20way%20to%20use%20a%20macro%20to%20find%20a%20print%20area%20when%20it%20varies%20with%20each%20run%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-831375%22%20slang%3D%22en-US%22%3EThe%20message%20you%20got%20suggests%20otherwise%2C%20check%20all%20worksheets%2C%20there%20should%20be%201%20or%20more%20which%20have%20just%20a%20small%20number%20of%20cells%20filled%20within%20columns%20A%3AN%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-831378%22%20slang%3D%22en-US%22%3ERe%3A%20Is%20there%20a%20way%20to%20use%20a%20macro%20to%20find%20a%20print%20area%20when%20it%20varies%20with%20each%20run%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-831378%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F22322%22%20target%3D%22_blank%22%3E%40Jan%20Karel%20Pieterse%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOk%2C%20there's%20some%20confusion.%26nbsp%3B%20I'm%20not%20getting%20the%20error%20message%20anymore.%26nbsp%3B%20The%20code%20I%20was%20provided%20is%20working%20perfectly%20now.%20%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20so%20much%20for%20your%20kind%20assistance!%26nbsp%3B%20You%20have%20been%20very%20helpful!%26nbsp%3B%20Thank%20you!!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-831379%22%20slang%3D%22en-US%22%3ERe%3A%20Is%20there%20a%20way%20to%20use%20a%20macro%20to%20find%20a%20print%20area%20when%20it%20varies%20with%20each%20run%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-831379%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F400666%22%20target%3D%22_blank%22%3E%40dchiggins54%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20only%20added%20the%20below%20line%20in%20the%20proposed%20code%20in%20other%20post...%3C%2FP%3E%3CPRE%3ESh.Activate%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20added%20the%20above%20line%20in%20order%20to%20help%20you%20know%20that%20which%20sheet%20is%20causing%20the%20error.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBtw%20did%20you%20add%20the%20following%20line%20suggested%20by%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F22322%22%20target%3D%22_blank%22%3E%40Jan%20Karel%20Pieterse%3C%2FA%3E%3F%20That%20should%20resolve%20your%20issue.%3C%2FP%3E%3CDIV%20class%3D%22post-author-login%22%3E%26nbsp%3B%3C%2FDIV%3E%3CPRE%3EApplication.DisplayAlerts%20%3D%20False%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-831396%22%20slang%3D%22en-US%22%3ERe%3A%20Is%20there%20a%20way%20to%20use%20a%20macro%20to%20find%20a%20print%20area%20when%20it%20varies%20with%20each%20run%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-831396%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%3CP%3EI'm%20sorry...I%20was%20confused.%26nbsp%3B%20I%20only%20just%20realized%20that%20two%20people%20were%20helping%20me%20here.%26nbsp%3B%20Subodh%2C%20I%20added%20your%20line%20of%20code%20and%20to%20Jan's%20macro%20and%20everything%20works%20great%20now.%26nbsp%3B%20I%20apologize%20for%20the%20confusion.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBottom%20line%2C%20I%20have%20a%20macro%20that%20does%20exactly%20what%20I%20need%20it%20to%20do.%26nbsp%3B%20There%20are%20no%20longer%20any%20errors.%26nbsp%3B%20My%20guess%20is%2C%20though%2C%20that%20perhaps%20I%20should%20add%20that%20code%20that%20detects%20a%20null%20and%20produces%20an%20error...just%20in%20case.%26nbsp%3B%20Perhaps%20I%20will%20should%20the%20time%20come.%26nbsp%3B%20I%20will%20certainly%20save%20this%2C%20because%20you%20have%20both%20been%20so%20very%20helpful.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20just%20don't%20know%20how%20to%20mark%20my%20post%20as%20answered.%26nbsp%3B%20It%20took%20more%20than%20one%20response.%20%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20again!%26nbsp%3B%20%3CLI-EMOJI%20id%3D%22lia_slightly-smiling-face%22%20title%3D%22%3Aslightly_smiling_face%3A%22%3E%3C%2FLI-EMOJI%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-831431%22%20slang%3D%22en-US%22%3ERe%3A%20Is%20there%20a%20way%20to%20use%20a%20macro%20to%20find%20a%20print%20area%20when%20it%20varies%20with%20each%20run%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-831431%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F400666%22%20target%3D%22_blank%22%3E%40dchiggins54%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou're%20welcome!%20Glad%20we%20could%20help%20and%20your%20issue%20is%20resolved%20now.%3C%2FP%3E%3CP%3EPlease%20accept%20the%20solution%20proposed%20by%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F22322%22%20target%3D%22_blank%22%3E%40Jan%20Karel%20Pieterse%3C%2FA%3Eas%20an%20Answer%20without%20any%20second%20thought.%20%3CLI-EMOJI%20id%3D%22lia_slightly-smiling-face%22%20title%3D%22%3Aslightly_smiling_face%3A%22%3E%3C%2FLI-EMOJI%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-832794%22%20slang%3D%22en-US%22%3ERe%3A%20Is%20there%20a%20way%20to%20use%20a%20macro%20to%20find%20a%20print%20area%20when%20it%20varies%20with%20each%20run%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-832794%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F22322%22%20target%3D%22_blank%22%3E%40Jan%20Karel%20Pieterse%3C%2FA%3E%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENow%20I%20need%20something%20similar%20to%20reformat%20the%20same%20column%20in%20each%20worksheet.%26nbsp%3B%20Again%2C%20the%20number%20of%20rows%20is%20different%20in%20each%20worksheet%2C%20so%20I%20can't%20just%20use%20the%20same%20macro%20I%20created%20for%20one%20on%20all%20others.%20It%20involves%20adding%20a%20working%20column%20after%20column%20J%2C%20dividing%20column%20J%20by%20%241%2C000%2C000%2C%20copying%20the%20results%20into%20the%20column%20J%2C%20and%20then%20deleting%20the%20working%20column.%26nbsp%3B%20I%20know%20you%20need%20to%20create%20some%20type%20of%20loop%20to%20run%20through%20each%20worksheet%20again%2C%20but%20my%20knowledge%20of%20the%20language%20is%20minimal.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere's%20what%20the%20initial%20macro%20looks%20like%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%230000ff%22%3EColumns(%22K%3AK%22).Select%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000ff%22%3ESelection.Insert%20Shift%3A%3DxlToRight%2C%20CopyOrigin%3A%3DxlFormatFromLeftOrAbove%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000ff%22%3ERange(%22K2%22).Select%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000ff%22%3EActiveCell.FormulaR1C1%20%3D%20%22%3DRC%5B-1%5D%2F1000000%22%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000ff%22%3ERange(%22K2%22).Select%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000ff%22%3ESelection.AutoFill%20Destination%3A%3DRange(%22K2%3AK59%22)%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000ff%22%3ERange(%22K2%3AK59%22).Select%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000ff%22%3ESelection.Copy%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000ff%22%3ERange(%22J2%22).Select%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000ff%22%3ESelection.PasteSpecial%20Paste%3A%3DxlPasteValues%2C%20Operation%3A%3DxlNone%2C%20SkipBlanks%20_%3A%3DFalse%2C%20Transpose%3A%3DFalse%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000ff%22%3EColumns(%22K%3AK%22).Select%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000ff%22%3EApplication.CutCopyMode%20%3D%20False%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000ff%22%3ESelection.Delete%20Shift%3A%3DxlToLeft%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000ff%22%3EColumns(%22J%3AJ%22).Select%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000ff%22%3ESelection.NumberFormat%20%3D%20%22%24%23%2C%23%230.0_)%3B(%24%23%2C%23%230.0)%22%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20need%20to%20replace%20the%20part%20that%20shows%20the%20range%20and%20have%20it%20search%20for%20the%20range%20on%20each%20worksheet.%20Does%20that%20makes%20sense%3F%26nbsp%3B%20If%20not%2C%20please%20let%20me%20know%20what%20else%20you%20need.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20so%20much!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-832854%22%20slang%3D%22en-US%22%3ERe%3A%20Is%20there%20a%20way%20to%20use%20a%20macro%20to%20find%20a%20print%20area%20when%20it%20varies%20with%20each%20run%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-832854%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F400666%22%20target%3D%22_blank%22%3E%40dchiggins54%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%20Debbie%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20try%20the%20following%20code...%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3ESub%20ReFormatColumns()%0ADim%20sh%20%20%20%20%20%20%20%20%20%20As%20Worksheet%0ADim%20LastRow%20%20%20%20%20As%20Long%0A%0AWith%20Application%0A%20%20%20%20.Calculation%20%3D%20xlCalculationManual%0A%20%20%20%20.EnableEvents%20%3D%20False%0A%20%20%20%20.ScreenUpdating%20%3D%20False%0AEnd%20With%0A%0AFor%20Each%20sh%20In%20ThisWorkbook.Worksheets%0A%20%20%20%20LastRow%20%3D%20sh.Cells.Find(%22*%22%2C%20SearchOrder%3A%3DxlByRows%2C%20SearchDirection%3A%3DxlPrevious).Row%0A%20%20%20%20sh.Columns(%22K%22).Insert%20Shift%3A%3DxlToRight%2C%20CopyOrigin%3A%3DxlFormatFromLeftOrAbove%0A%20%20%20%20With%20sh.Range(%22K2%3AK%22%20%26amp%3B%20LastRow)%0A%20%20%20%20%20%20%20%20.FormulaR1C1%20%3D%20%22%3DRC%5B-1%5D%2F1000000%22%0A%20%20%20%20%20%20%20%20.Copy%0A%20%20%20%20End%20With%0A%20%20%20%20With%20sh.Range(%22J2%3AJ%22%20%26amp%3B%20LastRow)%0A%20%20%20%20%20%20%20%20.PasteSpecial%20xlPasteValues%0A%20%20%20%20%20%20%20%20.NumberFormat%20%3D%20%22%24%23%2C%23%230.0_)%3B(%24%23%2C%23%230.0)%22%0A%20%20%20%20End%20With%0A%20%20%20%20sh.Columns(%22K%22).Delete%0A%20%20%20%20Application.CutCopyMode%20%3D%20False%0ANext%20sh%0A%0AWith%20Application%0A%20%20%20%20.Calculation%20%3D%20xlCalculationAutomatic%0A%20%20%20%20.EnableEvents%20%3D%20True%0A%20%20%20%20.ScreenUpdating%20%3D%20True%0AEnd%20With%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-832871%22%20slang%3D%22en-US%22%3ERe%3A%20Is%20there%20a%20way%20to%20use%20a%20macro%20to%20find%20a%20print%20area%20when%20it%20varies%20with%20each%20run%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-832871%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%3CP%3EThank%20you%20so%20much%20Subodh!%26nbsp%3B%20This%20works%20on%20one%20worksheet.%26nbsp%3B%20However%2C%20I%20need%20it%20to%20work%20through%20all%2023%20of%20the%20worksheets.%26nbsp%3B%20The%20same%20column%20is%20on%20all%20worksheets%20and%20must%20be%20converted.%20%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20you%20add%20the%20loop%20that%20is%20required%2C%20please%3F%26nbsp%3B%20I%20so%20appreciate%20your%20help!%26nbsp%3B%20My%20boss%20is%20waiting%20for%20this%20worksheet%20to%20be%20completed%20today%2C%20so%20I'm%20so%20happy%20you%20are%20available.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDebbie%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-832876%22%20slang%3D%22en-US%22%3ERe%3A%20Is%20there%20a%20way%20to%20use%20a%20macro%20to%20find%20a%20print%20area%20when%20it%20varies%20with%20each%20run%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-832876%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F400666%22%20target%3D%22_blank%22%3E%40dchiggins54%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThere%20is%20already%20a%20For%20loop%20looping%20through%20all%20the%20sheets%20in%20the%20workbook%20which%20contains%20this%20macro...%3C%2FP%3E%3CPRE%3EFor%20Each%20sh%20In%20ThisWorkbook.Worksheets%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20you%20want%20to%20run%20the%20macro%20on%20ActiveWorkbook%2C%20just%20remove%20ThisWorkbook%20from%20the%20For%20loop%20so%20that%20it%20would%20become%20%3CSTRONG%3EFor%20Each%20sh%20In%20Worksheets%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThough%20I%20didn't%20test%20the%20code%20earlier%2C%20I%20have%20just%20tested%20it%20after%20getting%20your%20feedback%20and%20it%20worked%20on%20all%20the%20sheets%20not%20on%20just%20one.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-832883%22%20slang%3D%22en-US%22%3ERe%3A%20Is%20there%20a%20way%20to%20use%20a%20macro%20to%20find%20a%20print%20area%20when%20it%20varies%20with%20each%20run%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-832883%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%3CP%3ENow%20there's%20a%20new%20error.%26nbsp%3B%20Pic%20attached...what%20did%20I%20do%20wrong%3F%26nbsp%3B%20I'm%20running%20it%20from%20the%20first%20worksheet%20in%20the%20file.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-832890%22%20slang%3D%22en-US%22%3ERe%3A%20Is%20there%20a%20way%20to%20use%20a%20macro%20to%20find%20a%20print%20area%20when%20it%20varies%20with%20each%20run%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-832890%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F400666%22%20target%3D%22_blank%22%3E%40dchiggins54%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDo%20you%20have%20any%20blank%20sheet%20in%20the%20file%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EReplace%20the%20existing%20For%20loop%20with%20the%20following%20one%20and%20let%20me%20know%20if%20that%20works%20for%20you...%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EFor%20Each%20sh%20In%20Worksheets%0A%20%20%20%20If%20sh.UsedRange.Address(0%2C%200)%20%26lt%3B%26gt%3B%20%22A1%22%20Then%0A%20%20%20%20%20%20%20%20LastRow%20%3D%20sh.Cells.Find(%22*%22%2C%20SearchOrder%3A%3DxlByRows%2C%20SearchDirection%3A%3DxlPrevious).Row%0A%20%20%20%20%20%20%20%20sh.Columns(%22K%22).Insert%20Shift%3A%3DxlToRight%2C%20CopyOrigin%3A%3DxlFormatFromLeftOrAbove%0A%20%20%20%20%20%20%20%20With%20sh.Range(%22K2%3AK%22%20%26amp%3B%20LastRow)%0A%20%20%20%20%20%20%20%20%20%20%20%20.FormulaR1C1%20%3D%20%22%3DRC%5B-1%5D%2F1000000%22%0A%20%20%20%20%20%20%20%20%20%20%20%20.Copy%0A%20%20%20%20%20%20%20%20End%20With%0A%20%20%20%20%20%20%20%20With%20sh.Range(%22J2%3AJ%22%20%26amp%3B%20LastRow)%0A%20%20%20%20%20%20%20%20%20%20%20%20.PasteSpecial%20xlPasteValues%0A%20%20%20%20%20%20%20%20%20%20%20%20.NumberFormat%20%3D%20%22%24%23%2C%23%230.0_)%3B(%24%23%2C%23%230.0)%22%0A%20%20%20%20%20%20%20%20End%20With%0A%20%20%20%20%20%20%20%20sh.Columns(%22K%22).Delete%0A%20%20%20%20%20%20%20%20Application.CutCopyMode%20%3D%20False%0A%20%20%20%20End%20If%0ANext%20sh%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-832904%22%20slang%3D%22en-US%22%3ERe%3A%20Is%20there%20a%20way%20to%20use%20a%20macro%20to%20find%20a%20print%20area%20when%20it%20varies%20with%20each%20run%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-832904%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%3CP%3EThere%20is%20one%20blank%20sheet%20at%20the%20beginning%2C%20however%2C%20your%20updated%20code%20worked.%26nbsp%3B%20All%20of%20the%20worksheets%20were%20done.%26nbsp%3B%20Please%20know%20that%20I%20made%20the%20second%20worksheet%20active%20before%20running%20the%20code.%26nbsp%3B%20Does%20it%20loop%20back%20to%20the%20beginning%3F%26nbsp%3B%20If%20so%2C%20then%20that's%20probably%20what%20caused%20the%20error.%20%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20so%20very%20much%20for%20all%20your%20help!%26nbsp%3B%20You're%20amazing!%26nbsp%3B%20Wish%20I%20knew%20the%20coding%20like%20this%2C%20but%20I%20just%20don't.%26nbsp%3B%20I%20tend%20to%20understand%20what%20I%20see%2C%20but%20I%20don't%20know%20how%20to%20code%20myself.%26nbsp%3B%20When%20I%20first%20wrote%20to%20you%20again%20about%20adding%20the%20loop%2C%20I%20simply%20missed%20the%20For%20command.%26nbsp%3B%20Thought%20it%20was%20missing%20since%20it%20didn't%20work%20but%20for%20one%20worksheet.%26nbsp%3B%20Pardon%20my%20own%20ignorance.%26nbsp%3B%20I%20so%20appreciate%20you!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-832993%22%20slang%3D%22en-US%22%3ERe%3A%20Is%20there%20a%20way%20to%20use%20a%20macro%20to%20find%20a%20print%20area%20when%20it%20varies%20with%20each%20run%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-832993%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F400666%22%20target%3D%22_blank%22%3E%40dchiggins54%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou're%20welcome%20Debbie!%20Glad%20I%20could%20help%20to%20resolve%20the%20issue%20you%20were%20having.%3C%2FP%3E%3CP%3EBtw%20Liking%20the%20posts%20which%20helped%20is%20another%20way%20to%20say%20thanks.%20%3CLI-EMOJI%20id%3D%22lia_slightly-smiling-face%22%20title%3D%22%3Aslightly_smiling_face%3A%22%3E%3C%2FLI-EMOJI%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
dchiggins54
Occasional Contributor

Hi All...

 

I have a situation where I'm trying to set the print area for 23 different worksheets in one file using a macro.  The columns are the same on each worksheet, but number of rows is different in each one and will vary each time the spreadsheet is run.  When I try to use the standard method for creating a macro to set the print area, it always lists the rows and columns.  Is there a way to program it so it looks for the rows on its own each time?  Am I making any sense?  Happy to elaborate if you can tell me what you need.

 

Any help would be greatly appreciated...thanks!

18 Replies
Solution

@dchiggins54 This macro would set the print-area of each sheet for columns A:N for as many rows as there is data in the sheet:

Sub SetPrintAreas()
    Dim Sh As Worksheet
    For Each Sh In Worksheets
        With Sh.PageSetup
            .PrintArea = Intersect(Sh.UsedRange, Sh.Range("A:N")).Address
        End With
    Next
End Sub

@Jan Karel Pieterse 

Thank you so much for your kind response! 

 

I created the macro (copied/pasted) and I get the error stating that only one cell has been selected (see attached file).  I was hoping that with a click of a button on a "master" worksheet in the file, the assigned macro would run through all of the 23 other worksheets and set the print area accordingly to each one.  This macro did not accomplish that, I'm afraid.

 

What am I doing wrong?

 

 

@dchiggins54 I was assuming the worksheets contain sufficient data, hence the intersection with the usedrange to get the correct number of rows. Apparently one or more worksheets contain just one cell with data in the desginated columns. YOu can avoid the message by adding this line of code to the macro, just below the Dim statement:

Application.DisplayAlerts = False

@dchiggins54 

 

Add one line to activate the sheet in your code like this...

For Each Sh In Worksheets
    Sh.Activate
    With Sh.PageSetup
        .PrintArea = Intersect(Sh.UsedRange, Sh.Range("A:N")).Address
    End With
Next

 

And when the code produces an error, can you look at the ActiveSheet and confirm if that sheet is empty?

What is the layout of your data on each sheet in the workbook? Is the data scattered all over or it starts from row1 and column A with no rows or columns in between the data?

 

 

@Subodh_Tiwari_sktneer 

Hi Subodh,

 

There is always data on each worksheet.  They are never null...at least 10 rows of data or more.  The data is not scattered.  Starts in A1.

 

Are you saying to use the code you just gave me instead of the previous version?  I ask because the line you gave me in the previous post to add to the macro worked, so I want to be clear.

 

Debbie

The message you got suggests otherwise, check all worksheets, there should be 1 or more which have just a small number of cells filled within columns A:N

@Jan Karel Pieterse 

Ok, there's some confusion.  I'm not getting the error message anymore.  The code I was provided is working perfectly now.  

 

Thank you so much for your kind assistance!  You have been very helpful!  Thank you!!

@dchiggins54 

 

I only added the below line in the proposed code in other post...

Sh.Activate

 

I added the above line in order to help you know that which sheet is causing the error.

 

Btw did you add the following line suggested by @Jan Karel Pieterse? That should resolve your issue.

Application.DisplayAlerts = False

 

@Subodh_Tiwari_sktneer 

I'm sorry...I was confused.  I only just realized that two people were helping me here.  Subodh, I added your line of code and to Jan's macro and everything works great now.  I apologize for the confusion.

 

Bottom line, I have a macro that does exactly what I need it to do.  There are no longer any errors.  My guess is, though, that perhaps I should add that code that detects a null and produces an error...just in case.  Perhaps I will should the time come.  I will certainly save this, because you have both been so very helpful.

 

I just don't know how to mark my post as answered.  It took more than one response.  

 

Thanks again! 

@dchiggins54 

You're welcome! Glad we could help and your issue is resolved now.

Please accept the solution proposed by @Jan Karel Pieterse as an Answer without any second thought.

Hi @Jan Karel Pieterse,

 

Now I need something similar to reformat the same column in each worksheet.  Again, the number of rows is different in each worksheet, so I can't just use the same macro I created for one on all others. It involves adding a working column after column J, dividing column J by $1,000,000, copying the results into the column J, and then deleting the working column.  I know you need to create some type of loop to run through each worksheet again, but my knowledge of the language is minimal.

 

Here's what the initial macro looks like:

 

Columns("K:K").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("K2").Select
ActiveCell.FormulaR1C1 = "=RC[-1]/1000000"
Range("K2").Select
Selection.AutoFill Destination:=Range("K2:K59")
Range("K2:K59").Select
Selection.Copy
Range("J2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _:=False, Transpose:=False
Columns("K:K").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("J:J").Select
Selection.NumberFormat = "$#,##0.0_);($#,##0.0)"

 

I need to replace the part that shows the range and have it search for the range on each worksheet. Does that makes sense?  If not, please let me know what else you need.

 

Thanks so much!

 

@dchiggins54 

 

Hi Debbie,

 

Please try the following code...

 

Sub ReFormatColumns()
Dim sh          As Worksheet
Dim LastRow     As Long

With Application
    .Calculation = xlCalculationManual
    .EnableEvents = False
    .ScreenUpdating = False
End With

For Each sh In ThisWorkbook.Worksheets
    LastRow = sh.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    sh.Columns("K").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    With sh.Range("K2:K" & LastRow)
        .FormulaR1C1 = "=RC[-1]/1000000"
        .Copy
    End With
    With sh.Range("J2:J" & LastRow)
        .PasteSpecial xlPasteValues
        .NumberFormat = "$#,##0.0_);($#,##0.0)"
    End With
    sh.Columns("K").Delete
    Application.CutCopyMode = False
Next sh

With Application
    .Calculation = xlCalculationAutomatic
    .EnableEvents = True
    .ScreenUpdating = True
End With
End Sub

@Subodh_Tiwari_sktneer 

Thank you so much Subodh!  This works on one worksheet.  However, I need it to work through all 23 of the worksheets.  The same column is on all worksheets and must be converted.  

 

Can you add the loop that is required, please?  I so appreciate your help!  My boss is waiting for this worksheet to be completed today, so I'm so happy you are available.

 

Debbie

@dchiggins54 

There is already a For loop looping through all the sheets in the workbook which contains this macro...

For Each sh In ThisWorkbook.Worksheets

 

If you want to run the macro on ActiveWorkbook, just remove ThisWorkbook from the For loop so that it would become For Each sh In Worksheets

 

Though I didn't test the code earlier, I have just tested it after getting your feedback and it worked on all the sheets not on just one.

@Subodh_Tiwari_sktneer 

Now there's a new error.  Pic attached...what did I do wrong?  I'm running it from the first worksheet in the file.

@dchiggins54 

Do you have any blank sheet in the file?

 

Replace the existing For loop with the following one and let me know if that works for you...

 

 

For Each sh In Worksheets
    If sh.UsedRange.Address(0, 0) <> "A1" Then
        LastRow = sh.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        sh.Columns("K").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        With sh.Range("K2:K" & LastRow)
            .FormulaR1C1 = "=RC[-1]/1000000"
            .Copy
        End With
        With sh.Range("J2:J" & LastRow)
            .PasteSpecial xlPasteValues
            .NumberFormat = "$#,##0.0_);($#,##0.0)"
        End With
        sh.Columns("K").Delete
        Application.CutCopyMode = False
    End If
Next sh

 

@Subodh_Tiwari_sktneer 

There is one blank sheet at the beginning, however, your updated code worked.  All of the worksheets were done.  Please know that I made the second worksheet active before running the code.  Does it loop back to the beginning?  If so, then that's probably what caused the error.  

 

Thank you so very much for all your help!  You're amazing!  Wish I knew the coding like this, but I just don't.  I tend to understand what I see, but I don't know how to code myself.  When I first wrote to you again about adding the loop, I simply missed the For command.  Thought it was missing since it didn't work but for one worksheet.  Pardon my own ignorance.  I so appreciate you!

@dchiggins54 

You're welcome Debbie! Glad I could help to resolve the issue you were having.

Btw Liking the posts which helped is another way to say thanks.

Related Conversations
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
flashing a white screen while open new tab
cntvertex in Discussions on
13 Replies
Tabs and Dark Mode
cjc2112 in Discussions on
22 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
28 Replies
description for autoplay blocking in settings page
HotCakeX in Discussions on
8 Replies