SOLVED
Home

help making a loop (to automate table formatting)

%3CLINGO-SUB%20id%3D%22lingo-sub-836798%22%20slang%3D%22en-US%22%3Ehelp%20making%20a%20loop%20(to%20automate%20table%20formatting)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-836798%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20using%20excel%20VBA%20to%20automate%20table%20formatting.%20I%20have%2024%20tables%20to%20automate.%20I%20wrote%20the%20code%20with%20a%20macro%20then%20made%20some%20changes.%20It%20works%20fine%20(it%20autofills%20data%20on%20A3%2C%20E3%2C%20and%20F3%2C%20based%20on%20the%20last%20row%20in%20filled%20in%20column%20B.%20The%20problem%20is%20I%20need%20to%20modify%20the%20code%20for%20Tables%202%20though%2024.%20I%20could%20do%20this%20manually%2C%20but%20its%20a%20pain!!%20Is%20there%20a%20way%20to%20set%20A%2CB%2CE%2C%20an%20F%20as%20variables%2C%20and%20loop%20though%20this%2023%20more%20times%20by%20adding%2018%26nbsp%3B%20letters%20each%20loop%3F%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EGreg%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E***CODE%20FOR%20TABLE%201%3C%2FP%3E%3CP%3ERange(%22A3%22).Select%3CBR%20%2F%3ESelection.autofill%20Destination%3A%3DRange(%22A3%3AA%22%20%26amp%3B%20Range(%22B%22%20%26amp%3B%20Rows.Count).End(xlUp).Row)%3CBR%20%2F%3ERange(Selection%2C%20Selection.End(xlDown)).Select%3CBR%20%2F%3ERange(%22E3%22).Select%3CBR%20%2F%3ESelection.autofill%20Destination%3A%3DRange(%22E3%3AE%22%20%26amp%3B%20Range(%22B%22%20%26amp%3B%20Rows.Count).End(xlUp).Row)%3CBR%20%2F%3ERange(Selection%2C%20Selection.End(xlDown)).Select%3CBR%20%2F%3ERange(%22F3%22).Select%3CBR%20%2F%3ESelection.autofill%20Destination%3A%3DRange(%22F3%3AF%22%20%26amp%3B%20Range(%22B%22%20%26amp%3B%20Rows.Count).End(xlUp).Row)%3CBR%20%2F%3ERange(Selection%2C%20Selection.End(xlDown)).Select%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E***CODE%20FOR%20TABLE%202%3C%2FP%3E%3CP%3ERange(%22S3%22).Select%3CBR%20%2F%3ESelection.autofill%20Destination%3A%3DRange(%22S3%3AS%22%20%26amp%3B%20Range(%22T%22%20%26amp%3B%20Rows.Count).End(xlUp).Row)%3CBR%20%2F%3ERange(Selection%2C%20Selection.End(xlDown)).Select%3CBR%20%2F%3ERange(%22W3%22).Select%3CBR%20%2F%3ESelection.autofill%20Destination%3A%3DRange(%22W3%3AW%22%20%26amp%3B%20Range(%22T%22%20%26amp%3B%20Rows.Count).End(xlUp).Row)%3CBR%20%2F%3ERange(Selection%2C%20Selection.End(xlDown)).Select%3CBR%20%2F%3ERange(%22X3%22).Select%3CBR%20%2F%3ESelection.autofill%20Destination%3A%3DRange(%22X3%3AX%22%20%26amp%3B%20Range(%22T%22%20%26amp%3B%20Rows.Count).End(xlUp).Row)%3CBR%20%2F%3ERange(Selection%2C%20Selection.End(xlDown)).Select%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E****and%20then%2022%20more%20times%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-836798%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-837965%22%20slang%3D%22en-US%22%3ERe%3A%20help%20making%20a%20loop%20(to%20automate%20table%20formatting)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-837965%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F301568%22%20target%3D%22_blank%22%3E%40gms4b%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20should%20do%20what%20you%20are%20trying%20to%20achieve....%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3ESub%20FormatTables()%0ADim%20c%20%20%20As%20Long%0ADim%20lr%20%20As%20Long%0ADim%20i%20%20%20As%20Long%0A%0AApplication.ScreenUpdating%20%3D%20False%0A%0Ac%20%3D%201%0A%0AFor%20i%20%3D%201%20To%2024%0A%20%20%20%20lr%20%3D%20Cells(Rows.Count%2C%20c%20%2B%201).End(xlUp).Row%0A%20%20%20%20If%20lr%20%26gt%3B%202%20Then%0A%20%20%20%20%20%20%20%20Cells(3%2C%20c).AutoFill%20Destination%3A%3DRange(Cells(3%2C%20c)%2C%20Cells(lr%2C%20c))%0A%20%20%20%20%20%20%20%20Cells(3%2C%20c%20%2B%204).AutoFill%20Destination%3A%3DRange(Cells(3%2C%20c%20%2B%204)%2C%20Cells(lr%2C%20c%20%2B%204))%0A%20%20%20%20%20%20%20%20Cells(3%2C%20c%20%2B%205).AutoFill%20Destination%3A%3DRange(Cells(3%2C%20c%20%2B%205)%2C%20Cells(lr%2C%20c%20%2B%205))%0A%20%20%20%20End%20If%0A%20%20%20%20c%20%3D%20c%20%2B%2018%0ANext%20i%0A%0AApplication.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-839495%22%20slang%3D%22en-US%22%3ERe%3A%20help%20making%20a%20loop%20(to%20automate%20table%20formatting)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-839495%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%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAMAZING!!!%20I%20spent%2C%20like%2C%20hours%20yesterday%20entering%20all%20of%20the%20cells%20names%20in%20manually...and%20this%20does%20it%20perfectly!%20Even%20better%2C%20there%20are%20other%20actions%20that%20I%20need%20to%20automate%20on%20the%20same%20data%20set%20as%20well.%20I'm%20hoping%20that%20I%20can%20just%20modify%20this%20sub%20and%20use%20it%20in%20other%20ways%20as%20well.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20so%20much!!!!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGreg%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-839546%22%20slang%3D%22en-US%22%3ERe%3A%20help%20making%20a%20loop%20(to%20automate%20table%20formatting)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-839546%22%20slang%3D%22en-US%22%3E%3CP%3EYou're%20welcome%20Greg!%20Glad%20it%20worked%20as%20desired.%3C%2FP%3E%3CP%3EThanks%20for%20the%20feedback.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20take%20a%20minute%20to%20accept%20my%20post%20with%20the%20proposed%20solution%20as%20an%20Answer%20and%20hit%20the%20like%20button.%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%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESubodh%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-839716%22%20slang%3D%22en-US%22%3ERe%3A%20help%20making%20a%20loop%20(to%20automate%20table%20formatting)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-839716%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%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHaha!%20I%20used%20your%20sub%20as%20a%20template%20and%20this%20new%20sub%20goes%20back%20and%20merges%20those%20cells%20that%20you%20did%20the%20autofill%20on.%20Worked%20great!%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EActually%2C%20instead%20of%202%20subs%2C%20I%20could%20probably%20just%20merge%20the%20two%20subs%20together...%3C%2FP%3E%3CP%3EGreg%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESub%20mergecells()%3C%2FP%3E%3CP%3EDim%20c%20As%20Long%3CBR%20%2F%3EDim%20lr%20As%20Long%3CBR%20%2F%3EDim%20i%20As%20Long%3C%2FP%3E%3CP%3EApplication.ScreenUpdating%20%3D%20False%3CBR%20%2F%3EApplication.DisplayAlerts%20%3D%20False%3C%2FP%3E%3CP%3Ec%20%3D%201%3C%2FP%3E%3CP%3EFor%20i%20%3D%201%20To%2026%3CBR%20%2F%3Elr%20%3D%20Cells(Rows.Count%2C%20c%20%2B%201).End(xlUp).Row%3CBR%20%2F%3EIf%20lr%20%26gt%3B%202%20Then%3CBR%20%2F%3EWith%20Sheets(%22QC%20Chart%202%20Prep%22)%3CBR%20%2F%3E.Range(.Cells(3%2C%20c)%2C%20.Cells(lr%2C%20c)).Merge%3CBR%20%2F%3E.Range(.Cells(3%2C%20c%20%2B%204)%2C%20.Cells(lr%2C%20c%20%2B%204)).Merge%3CBR%20%2F%3E.Range(.Cells(3%2C%20c%20%2B%205)%2C%20.Cells(lr%2C%20c%20%2B%205)).Merge%3CBR%20%2F%3EEnd%20With%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3Ec%20%3D%20c%20%2B%2018%3CBR%20%2F%3E%3CBR%20%2F%3ENext%20i%3C%2FP%3E%3CP%3EApplication.ScreenUpdating%20%3D%20True%3CBR%20%2F%3EApplication.DisplayAlerts%20%3D%20True%3CBR%20%2F%3EEnd%20Sub%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-839805%22%20slang%3D%22en-US%22%3ERe%3A%20help%20making%20a%20loop%20(to%20automate%20table%20formatting)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-839805%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%20rel%3D%22noopener%22%3E%40Subodh_Tiwari_sktneer%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3ESo%2C%20I%20combined%20those%202%20subs%20together%20and%20it%20works%20great!%20%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3EThe%20last%20thing%20I%20need%20to%20do%20is%20to%20move%20the%2026%20charts%20I%20made%20I%20made%20into%20a%20single%20column.%20manualy%2C%20I%20do%20this%3A%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E------------------------------%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3ERange(%22S3%3AX37%22).Select%3CBR%20%2F%3ESelection.Copy%3CBR%20%2F%3ERange(%22A38%22).Select%3CBR%20%2F%3EActiveSheet.Paste%3CBR%20%2F%3E%3CBR%20%2F%3ERange(%22AK3%3AAP37%22).Select%3CBR%20%2F%3ESelection.Copy%3CBR%20%2F%3ERange(%22A73%22).Select%3CBR%20%2F%3EActiveSheet.Paste%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3Eetc....26%20times...%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E------------------------------------%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3Eso%2C%20the%20selected%20range%20is%26nbsp%3BS3%3AX37%20(which%20is%20.cell(3%2C19)%20to%20.cell(37%2C24))%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3Ethen%20copy%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3Eand%20paste%20into%20A38%20(which%20is%20.cell(35%2C1))%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EThe%20next%20chart%20is%26nbsp%3BRange%20%22AK3%3AAP37%20(which%20is%2018%20columns%20to%20the%20right)%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3Ethen%20copy%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3Eand%20paste%20into%20A73%20(which%20is%2038%20rows%20down%20on%20column%20A).%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3Ehowever%2C%20when%20I%20run%20the%20sub%20(below)%20I%20get%20the%20a%20compile%20error%20(invalid%20or%20unqualified%20reference)!%20Hopefully%20its%20something%20simple%20that%20I'm%20doing%20wrong....do%20you%20see%20what%20that%20might%20be%3F%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EThanks%2C%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3EGreg%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3ESub%20movecharts()%0A%0ADim%20er%20As%20Long%0ADim%20ec%20As%20Long%0ADim%20sc%20As%20Long%0ADim%20i%20As%20Long%0ADim%20fr%20As%20Long%0A%0AApplication.ScreenUpdating%20%3D%20False%0A%0Asc%20%3D%2019%0Aec%20%3D%2024%0Afr%20%3D%2035%0A%0AFor%20i%20%3D%201%20To%2026%0ARange(.Cells(3%2C%20sc)%2C%20.Cells(37%2C%20ec)).Select%0ASelection.Copy%0ARange(.Cells(fr%2C%201)).Select%0AActiveSheet.Paste%0A%0Asc%20%3D%20sc%20%2B%2018%0Asr%20%3D%20sr%20%2B%2018%0Afr%20%3D%20fr%20%2B%2035%0A%0ANext%20i%0A%0AApplication.ScreenUpdating%20%3D%20True%0AEnd%20Sub%0A%3C%2FCODE%3E%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-840199%22%20slang%3D%22en-US%22%3ERe%3A%20help%20making%20a%20loop%20(to%20automate%20table%20formatting)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-840199%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F301568%22%20target%3D%22_blank%22%3E%40gms4b%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20problem%20is%20with%20the%20following%20two%20lines...%3C%2FP%3E%3CP%3E1.%20Range(.Cells(3%2C%20sc)%2C%20.Cells(37%2C%20ec)).Select%3C%2FP%3E%3CP%3E2.%20Range(.Cells(fr%2C%201)).Select%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20the%20first%20line%20you%20are%20using%20.%20(period)%20before%20the%20Cells%20which%20is%20used%20when%20you%20specify%20the%20parent%20object%20in%20a%20WITH%20and%20END%20WITH%20block%20so%20it%20is%20not%20required%20here%20and%20you%20should%20remove%20the%20period%20from%20both%20the%20occurrences%20of%20Cells%20in%20that%20line.%20Also%2C%20you%20don't%20need%20to%20use%20Select%20in%20order%20to%20copy%20a%20range%20so%20you%20can%20replace%20Select%20with%20Copy.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20second%20line%20Range%20object%20expects%20a%20cell%20address%20not%20the%20cell%20object%20itself%20so%20use%20of%20Range%20in%20that%20line%20doesn't%20make%20any%20sense%2C%20you%20can%20use%20only%20Cells(fr%2C%201).Select%20and%20that%20will%20work%20as%20desired.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20try%20it%20like%20this...%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3ESub%20movecharts()%0ADim%20sr%20As%20Long%0ADim%20er%20As%20Long%0ADim%20ec%20As%20Long%0ADim%20sc%20As%20Long%0ADim%20i%20As%20Long%0ADim%20fr%20As%20Long%0A%0AApplication.ScreenUpdating%20%3D%20False%0A%0Asc%20%3D%2019%0Aec%20%3D%2024%0Afr%20%3D%2035%0A%0AFor%20i%20%3D%201%20To%2026%0A%20%20%20%20Range(Cells(3%2C%20sc)%2C%20Cells(37%2C%20ec)).Copy%0A%20%20%20%20Cells(fr%2C%201).Select%0A%20%20%20%20ActiveSheet.Paste%0A%20%20%20%20Application.CutCopyMode%20%3D%200%0A%0A%20%20%20%20sc%20%3D%20sc%20%2B%2018%0A%20%20%20%20sr%20%3D%20sr%20%2B%2018%0A%20%20%20%20fr%20%3D%20fr%20%2B%2035%0A%0ANext%20i%0A%0AApplication.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-841494%22%20slang%3D%22en-US%22%3ERe%3A%20help%20making%20a%20loop%20(to%20automate%20table%20formatting)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-841494%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%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPerfect%20fix!%20Thanks%20so%20much!%20Obviously%2C%20I'm%20not%20a%20programmer%2C%20just%20a%20chemist%20trying%20to%20automate%20reports%20that%20my%20team%20and%20I%20do%20over%20and%20over%20again.%20This%20will%20help%20so%20much!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThere%20was%20a%20mistake%20in%20the%20code%20I%20fixed....(needed%20to%20fix%20the%20increment%20at%20the%20end%20(ec%20%3D%20ec%20%2B%2018)).%20I%20also%20didn't%20need%20some%20of%20the%20variables%20I%20initially%20included%20(%22sr%22%20and%20%22er%22%2C%20so%20I%20deleted%20them).%20Also%2C%20I%20ran%20this%20directly%20after%20another%20loop%20so%20I%20changed%20%22i%22%20to%20%22j%22.%20I%20have%20the%20final%20code%20included%20below%20for%20completeness.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EDim%20ec%20As%20Long%0ADim%20sc%20As%20Long%0ADim%20j%20As%20Long%0ADim%20fr%20As%20Long%0A%0AApplication.ScreenUpdating%20%3D%20False%0A%0Asc%20%3D%2019%0Aec%20%3D%2024%0Afr%20%3D%2035%0A%0AFor%20j%20%3D%201%20To%2026%0A%20%20%20%20Range(Cells(3%2C%20sc)%2C%20Cells(37%2C%20ec)).Copy%0A%20%20%20%20Cells(fr%2C%201).Select%0A%20%20%20%20ActiveSheet.Paste%0A%20%20%20%20Application.CutCopyMode%20%3D%200%0A%0A%20%20%20%20sc%20%3D%20sc%20%2B%2018%0A%20%20%20%20ec%20%3D%20ec%20%2B%2018%0A%20%20%20%20fr%20%3D%20fr%20%2B%2035%0A%0ANext%20j%0A%0AApplication.ScreenUpdating%20%3D%20True%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
gms4b
Contributor

I am using excel VBA to automate table formatting. I have 24 tables to automate. I wrote the code with a macro then made some changes. It works fine (it autofills data on A3, E3, and F3, based on the last row in filled in column B. The problem is I need to modify the code for Tables 2 though 24. I could do this manually, but its a pain!! Is there a way to set A,B,E, an F as variables, and loop though this 23 more times by adding 18  letters each loop?


Greg

 

 

***CODE FOR TABLE 1

Range("A3").Select
Selection.autofill Destination:=Range("A3:A" & Range("B" & Rows.Count).End(xlUp).Row)
Range(Selection, Selection.End(xlDown)).Select
Range("E3").Select
Selection.autofill Destination:=Range("E3:E" & Range("B" & Rows.Count).End(xlUp).Row)
Range(Selection, Selection.End(xlDown)).Select
Range("F3").Select
Selection.autofill Destination:=Range("F3:F" & Range("B" & Rows.Count).End(xlUp).Row)
Range(Selection, Selection.End(xlDown)).Select

 

***CODE FOR TABLE 2

Range("S3").Select
Selection.autofill Destination:=Range("S3:S" & Range("T" & Rows.Count).End(xlUp).Row)
Range(Selection, Selection.End(xlDown)).Select
Range("W3").Select
Selection.autofill Destination:=Range("W3:W" & Range("T" & Rows.Count).End(xlUp).Row)
Range(Selection, Selection.End(xlDown)).Select
Range("X3").Select
Selection.autofill Destination:=Range("X3:X" & Range("T" & Rows.Count).End(xlUp).Row)
Range(Selection, Selection.End(xlDown)).Select

 

****and then 22 more times

 

7 Replies
Solution

@gms4b 

 

This should do what you are trying to achieve....

 

Sub FormatTables()
Dim c   As Long
Dim lr  As Long
Dim i   As Long

Application.ScreenUpdating = False

c = 1

For i = 1 To 24
    lr = Cells(Rows.Count, c + 1).End(xlUp).Row
    If lr > 2 Then
        Cells(3, c).AutoFill Destination:=Range(Cells(3, c), Cells(lr, c))
        Cells(3, c + 4).AutoFill Destination:=Range(Cells(3, c + 4), Cells(lr, c + 4))
        Cells(3, c + 5).AutoFill Destination:=Range(Cells(3, c + 5), Cells(lr, c + 5))
    End If
    c = c + 18
Next i

Application.ScreenUpdating = True
End Sub

@Subodh_Tiwari_sktneer 

 

AMAZING!!! I spent, like, hours yesterday entering all of the cells names in manually...and this does it perfectly! Even better, there are other actions that I need to automate on the same data set as well. I'm hoping that I can just modify this sub and use it in other ways as well.

 

Thank you so much!!!!

 

Greg

You're welcome Greg! Glad it worked as desired.

Thanks for the feedback.

 

Please take a minute to accept my post with the proposed solution as an Answer and hit the like button.

 

Subodh

@Subodh_Tiwari_sktneer 

 

Haha! I used your sub as a template and this new sub goes back and merges those cells that you did the autofill on. Worked great! 

 

Actually, instead of 2 subs, I could probably just merge the two subs together...

Greg

 

Sub mergecells()

Dim c As Long
Dim lr As Long
Dim i As Long

Application.ScreenUpdating = False
Application.DisplayAlerts = False

c = 1

For i = 1 To 26
lr = Cells(Rows.Count, c + 1).End(xlUp).Row
If lr > 2 Then
With Sheets("QC Chart 2 Prep")
.Range(.Cells(3, c), .Cells(lr, c)).Merge
.Range(.Cells(3, c + 4), .Cells(lr, c + 4)).Merge
.Range(.Cells(3, c + 5), .Cells(lr, c + 5)).Merge
End With
End If
c = c + 18

Next i

Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub

@Subodh_Tiwari_sktneer 

 

So, I combined those 2 subs together and it works great!

The last thing I need to do is to move the 26 charts I made I made into a single column. manualy, I do this:

------------------------------

Range("S3:X37").Select
Selection.Copy
Range("A38").Select
ActiveSheet.Paste

Range("AK3:AP37").Select
Selection.Copy
Range("A73").Select
ActiveSheet.Paste

 

etc....26 times...

------------------------------------

so, the selected range is S3:X37 (which is .cell(3,19) to .cell(37,24))

then copy

and paste into A38 (which is .cell(35,1))

 

The next chart is Range "AK3:AP37 (which is 18 columns to the right)

then copy

and paste into A73 (which is 38 rows down on column A).

 

however, when I run the sub (below) I get the a compile error (invalid or unqualified reference)! Hopefully its something simple that I'm doing wrong....do you see what that might be?

 

Thanks,

Greg

 

Sub movecharts()

Dim er As Long
Dim ec As Long
Dim sc As Long
Dim i As Long
Dim fr As Long

Application.ScreenUpdating = False

sc = 19
ec = 24
fr = 35

For i = 1 To 26
Range(.Cells(3, sc), .Cells(37, ec)).Select
Selection.Copy
Range(.Cells(fr, 1)).Select
ActiveSheet.Paste

sc = sc + 18
sr = sr + 18
fr = fr + 35

Next i

Application.ScreenUpdating = True
End Sub

 

 

@gms4b 

 

The problem is with the following two lines...

1. Range(.Cells(3, sc), .Cells(37, ec)).Select

2. Range(.Cells(fr, 1)).Select

 

In the first line you are using . (period) before the Cells which is used when you specify the parent object in a WITH and END WITH block so it is not required here and you should remove the period from both the occurrences of Cells in that line. Also, you don't need to use Select in order to copy a range so you can replace Select with Copy.

 

In second line Range object expects a cell address not the cell object itself so use of Range in that line doesn't make any sense, you can use only Cells(fr, 1).Select and that will work as desired.

 

Please try it like this...

 

Sub movecharts()
Dim sr As Long
Dim er As Long
Dim ec As Long
Dim sc As Long
Dim i As Long
Dim fr As Long

Application.ScreenUpdating = False

sc = 19
ec = 24
fr = 35

For i = 1 To 26
    Range(Cells(3, sc), Cells(37, ec)).Copy
    Cells(fr, 1).Select
    ActiveSheet.Paste
    Application.CutCopyMode = 0

    sc = sc + 18
    sr = sr + 18
    fr = fr + 35

Next i

Application.ScreenUpdating = True
End Sub

@Subodh_Tiwari_sktneer 

 

Perfect fix! Thanks so much! Obviously, I'm not a programmer, just a chemist trying to automate reports that my team and I do over and over again. This will help so much!

 

There was a mistake in the code I fixed....(needed to fix the increment at the end (ec = ec + 18)). I also didn't need some of the variables I initially included ("sr" and "er", so I deleted them). Also, I ran this directly after another loop so I changed "i" to "j". I have the final code included below for completeness. 

 

Dim ec As Long
Dim sc As Long
Dim j As Long
Dim fr As Long

Application.ScreenUpdating = False

sc = 19
ec = 24
fr = 35

For j = 1 To 26
    Range(Cells(3, sc), Cells(37, ec)).Copy
    Cells(fr, 1).Select
    ActiveSheet.Paste
    Application.CutCopyMode = 0

    sc = sc + 18
    ec = ec + 18
    fr = fr + 35

Next j

Application.ScreenUpdating = True

 

 

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