SOLVED
Home

Copy data from one workbook to another. then delete the original data

%3CLINGO-SUB%20id%3D%22lingo-sub-532074%22%20slang%3D%22en-US%22%3ECopy%20data%20from%20one%20workbook%20to%20another.%20then%20delete%20the%20original%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-532074%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20All%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20trying%20to%20achieve%20the%20following%20in%20VBA.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20a%20master%20workbook%20called%20Master.xlsm.%3C%2FP%3E%3CP%3EI%20have%20a%20number%20of%20workbooks%20called%20Engineer1%20to%20X%3C%2FP%3E%3CP%3EEngineer%20workbook%20only%20contains%201%20worksheet%20(Sheet1)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20would%20like%20to%20be%20able%20to%20copy%20all%20the%20contents%20from%20Engineer1%20workbook%20for%20all%20the%20Engineer%20workbooks%20to%20the%20Master%20Workbook.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20want%20to%20say%20copy%20rows%202%20to%2010%20from%20Engineer%201%20workbook%20and%20then%20copy%20rows%202%20to%2010%20from%20Engineer2%20workbook%20to%20the%20the%20cells%20below%20the%20data%20from%20engineer1.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20tried%20a%20few%20examples%20from%20the%20internet%20but%20I%20cannot%20get%20the%20code%20to%20work.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20help.%20I%20have%20attached%20my%20base%20files%20for%20your%20easy%20reference.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EVonryan%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-532074%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-532164%22%20slang%3D%22en-US%22%3ERe%3A%20Copy%20data%20from%20one%20workbook%20to%20another.%20then%20delete%20the%20original%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-532164%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F323150%22%20target%3D%22_blank%22%3E%40vonryan%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYour%20approach%20is%20possible%20but%20these%20days%20I%20would%20recommend%20the%20use%20of%20Power%20Query.%26nbsp%3B%20Combining%20a%20set%20of%20files%20from%20a%20folder%20is%20core%20functionality%20of%20PQ%20and%20once%20further%20'Engineer'%20files%20are%20added%20to%20the%20folder%20all%20that%20is%20needed%20is%20to%20click%20the%20refresh%20button.%26nbsp%3B%20Ideally%20the%20data%20should%20all%20be%20contained%20in%20Tables%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EData%20ribbon%20tab%3C%2FP%3E%3CP%3EGet%20Data%3C%2FP%3E%3CP%3EFrom%20File%3C%2FP%3E%3CP%3EFrom%20Folder%3C%2FP%3E%3CP%3EChoose%20'combine'%3C%2FP%3E%3CP%3ESelect%20the%20Table%20to%20import%20from%20each%20file%3C%2FP%3E%3CP%3EEdit%20as%20needed%3C%2FP%3E%3CP%3EClose%20and%20load%20to%20sheet%20within%20Master%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-532969%22%20slang%3D%22en-US%22%3ERe%3A%20Copy%20data%20from%20one%20workbook%20to%20another.%20then%20delete%20the%20original%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-532969%22%20slang%3D%22en-US%22%3E%3CP%3EPeter%2C%20thanks%20for%20your%20reply.%20I%20have%20tried%20your%20described%20method%20but%20I%20seem%20to%20be%20struggling.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20one%20file%20Master.xlsm%20on%20D%3A%5C%3C%2FP%3E%3CP%3EI%20have%20four%20files%20Engineer1%20thro%20Engineer4%20on%20D%3A%5CTest%5C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20go%20to%20data%20ribbon%20and%20select%20New%20Query%5CFrom%20File%5C%20From%20Folder%20and%20set%20the%20path%20to%20D%3A%5CTest%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethe%20query%20selects%20the%204%20files%20and%20I%20press%20Combine%20and%20load.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETHe%20combinne%20dialogue%20window%20appears%20and%20I%20select%20Example%20file%20as%20Engineer1.%20The%20prview%20pane%20show%20me%20the%20layout%20which%20is%20fine.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20press%20the%20OK%20button%20and%20the%20query%20starts%20running.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20then%20get%20a%26nbsp%3Berror%20message%26nbsp%3B%3CBR%20%2F%3E%5BExpression.Error%5D%20The%20key%20didn't%20match%20any%20rows%20in%20the%20table..%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20see%20attached%20screenshot%3C%2FP%3E%3CP%3EWhat%20am%20I%20doing%20wrong%20please%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EVonryan%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F214174%22%20target%3D%22_blank%22%3E%40Peter%20Bartholomew%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-534286%22%20slang%3D%22en-US%22%3ERe%3A%20Copy%20data%20from%20one%20workbook%20to%20another.%20then%20delete%20the%20original%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-534286%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F323150%22%20target%3D%22_blank%22%3E%40vonryan%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20screenshot%20is%20very%20close%20to%20that%20given%20by%20working%20versions.%3C%2FP%3E%3CP%3EOne%20thought%20is%20that%20the%20unit%20of%20interaction%20between%20PowerQuery%20and%20Excel%20is%20the%20Table.%26nbsp%3B%20I%20think%20it%20may%20be%20possible%20work%20with%20Named%20Ranges%20or%20the%20UsedRegion%20of%20a%20Sheet%20but%20they%20are%20not%20the%20preferred%20options.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EUnless%20you%20have%20already%20done%20it%2C%20I%20would%20suggest%20going%20through%20each%20of%20your%20four%20files%20and%20converting%20the%20data%20to%20a%20Table%20%5BCtrl%2FT%5D.%26nbsp%3B%20This%20will%20give%20a%20default%20name%20of%20Table1%20but%20it%20can%20be%20renamed%20to%20give%20some%20more%20meaningful%20reference%20such%20as%20'EngineeringTask'%20(the%20same%20name%20in%20each%20file).%26nbsp%3B%20Then%2C%20within%20the%20Combine%20%26amp%3B%20Edit%20dialogue%2C%20select%20the%20Table%20name%20to%20be%20the%20basis%20for%20extracting%20data%20from%20the%20sample%20file.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFingers%20crossed%20this%20takes%20us%20a%20step%20forward!%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-534331%22%20slang%3D%22en-US%22%3ERe%3A%20Copy%20data%20from%20one%20workbook%20to%20another.%20then%20delete%20the%20original%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-534331%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F214174%22%20target%3D%22_blank%22%3E%40Peter%20Bartholomew%3C%2FA%3E%26nbsp%3B%2C%20From%20Folder%20connector%20works%20fine%20with%20entire%20sheets.%20The%20only%20point%20data%20for%20all%20files%20in%20the%20folder%20have%20to%20have%20the%20same%20structure.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-534777%22%20slang%3D%22en-US%22%3ERe%3A%20Copy%20data%20from%20one%20workbook%20to%20another.%20then%20delete%20the%20original%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-534777%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F521%22%20target%3D%22_blank%22%3E%40Sergei%20Baklan%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you.%26nbsp%3B%20From%20the%20selections%20available%20on%20the%20combine%20files%20menu%2C%20I%20suspected%20that%20was%20the%20case.%26nbsp%3B%20I%20wouldn't%20have%20personal%20experience%20because%20I%20haven't%20used%20location%20(Sheet%20and%20cell%20notation)%20to%20reference%20data%20objects%20for%20a%20number%20of%20years%20now.%26nbsp%3B%20Tables%20are%20simply%20a%20more%20natural%20starting%20point%20for%20me%20and%2C%20most%20recently%2C%20this%20is%20followed%20by%20spilt%20arrays%20in%20which%20only%20the%20anchor%20cell%20is%20named.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOddly%20enough%2C%20it%20is%20sometimes%20difficult%20to%20establish%20a%20dialogue%20with%20Excel%20users%3B%20wonder%20why%20that%20is%3F%20%3B-)%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-535070%22%20slang%3D%22en-US%22%3ERe%3A%20Copy%20data%20from%20one%20workbook%20to%20another.%20then%20delete%20the%20original%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-535070%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F214174%22%20target%3D%22_blank%22%3E%40Peter%20Bartholomew%3C%2FA%3E%26nbsp%3B%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThat%20could%20be%20tables.%20With%20them%20even%20easier.%20But%20with%20that%20all%20your%20files%26nbsp%3B%20are%20to%20be%20with%20exactly%20the%20same%20Table1%20with%20exactly%20the%20same%20column%20names.%20Only%20values%20could%20be%20different.%20What%20is%20done%2C%20you%20take%20any%20file%20in%20the%20folder%20as%20a%20sample%20one%2C%20transform%20it%20(it%20could%20be%20Tables%2C%20Named%20ranges%20or%20entire%20sheet)%20and%20generated%20for%20such%20transformation%20function%20is%20applied%20to%20all%20files%20in%20the%20folder.%20If%20files%20have%20different%20structure%20you%20failed.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOh%2C%20Excel%20users%20are%20quite%20different.%20Even%20if%20speak%20about%20the%20Excel%2C%20sometimes%20we%20speak%20on%20different%20languages.%20That's%20not%20good%20and%20not%20bad%2C%20that%20as%20it%20is.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-557454%22%20slang%3D%22en-US%22%3ERe%3A%20Copy%20data%20from%20one%20workbook%20to%20another.%20then%20delete%20the%20original%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-557454%22%20slang%3D%22en-US%22%3E%3CP%3EGentlemen%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20have%20been%20a%20great%20help.%20Thank%20you.%20I%20have%20achieved%20my%20goal%20thanks%20to%20your%20help.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20will%20investigate%20Tables%20when%20I%20get%20the%20chance.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EVonryan%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F521%22%20target%3D%22_blank%22%3E%40Sergei%20Baklan%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-560246%22%20slang%3D%22en-US%22%3EHide%20Colums%20based%20on%20dates.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-560246%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Peter%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20a%20new%20challenge%20if%20you%20are%20interested.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20wish%20to%20hide%20culumns%20in%20a%20worksheet%20based%20on%20dates.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOn%20worksheet%201%20from%20B2%3AQ2%20are%20cells%20with%20dates.%20Each%20cell%20is%20one%20week%20apart.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20created%20a%20button%20on%20the%20page%20and%20it%20triggers%20a%20sub%20routine%20that%20is%20supposed%20to%20check%20the%20current%20date%20(Today())%20function%20and%20evaluate%20this%20date%20into%20a%20week%20number%20using%20%5BWeeknum(Today()%5D.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20then%20checks%20the%20cell%20Range%20(B2%3AQ2)%20date%20and%20evaluates%20these%20dates%20for%20%5BWeeknum(c.Value)%5D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20the%20week%20number%20of%20the%20date%20in%20the%20Range%20of%20cells%20B2%3AQ2%20is%20%26lt%3B%26gt%3B%20to%20week%20number%20of%20today%20then%20hide%20the%20columns%20as%20I%20only%20want%20to%20see%20this%20weeks%20column.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20written%20the%20following%20code%20to%20try%20and%20resolve%20my%20isssue%20by%20using%20a%20message%20box%20to%20return%20the%20values%20of%20the%20variables.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20cannot%20get%20the%20Week%20Number%20function%20to%20work.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESub%20ShowCurrentWeekOnly()%3CBR%20%2F%3EDim%20c%20As%20Range%2C%20msgValueC%20As%20Long%2C%20msgValueWeeknumC%20As%20Long%2C%20msgValueDate%20As%20Long%2C%20msgValueWeeknumDate%20As%20Long%2C%20WeekNum%20As%20Long%3CBR%20%2F%3EApplication.EnableEvents%20%3D%20False%3C%2FP%3E%3CP%3EOn%20Error%20Resume%20Next%3C%2FP%3E%3CP%3EFor%20Each%20c%20In%20Range(%22B2%3AQ2%22)%3CBR%20%2F%3EmsgValueC%20%3D%20c.Value%3CBR%20%2F%3EWeekNum%20%3D%20Evaluate(%22%3Dweeknum(msgValueC)%22)%3CBR%20%2F%3EmsgValueWeeknumC%20%3D%20WeekNum%3CBR%20%2F%3EmsgValueDate%20%3D%20Date%3CBR%20%2F%3EmsgValueWeeknumDate%20%3D%20%5BWeeknum(Date()%5D%3CBR%20%2F%3EMsgBox%20%22Returned%20values%20are%20%22%20%26amp%3B%20msgValueC%20%26amp%3B%20%22%20%22%20%26amp%3B%20msgValueWeeknumC%20%26amp%3B%20%22%20%22%20%26amp%3B%20msgValueDate%20%26amp%3B%20%22%20%22%20%26amp%3B%20msgValueWeeknumDate%3CBR%20%2F%3EIf%20WeekNum%20%26lt%3B%26gt%3B%20%5Bweeknum(Date())%5D%20Then%3CBR%20%2F%3Ec.EntireColumn.Hidden%20%3D%20True%3CBR%20%2F%3EElse%3CBR%20%2F%3Ec.EntireColumn.Hidden%20%3D%20False%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3ENext%3CBR%20%2F%3EOn%20Error%20GoTo%200%3CBR%20%2F%3EApplication.EnableEvents%20%3D%20True%3C%2FP%3E%3CP%3EEnd%20Sub%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20week%20number%20always%20returns%20zero.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20wonder%20if%20you%20can%20help.%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F323150%22%20target%3D%22_blank%22%3E%40vonryan%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-560621%22%20slang%3D%22en-US%22%3ERe%3A%20Hide%20Colums%20based%20on%20dates.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-560621%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F323150%22%20target%3D%22_blank%22%3E%40vonryan%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20statement%3C%2FP%3E%3CP%3EWeekNum%20%3D%20%5BWeeknum(msgValueC)%5D%3C%2FP%3E%3CP%3Egave%20me%20an%20error%202029.%3C%2FP%3E%3CP%3EYou%20could%20calculate%20the%20week%20numbers%20on%20the%20spreadsheet%20and%20then%20read%20them%20from%20the%20module%20using%20something%20of%20the%20form%3C%2FP%3E%3CP%3EWeekNumber%20%3D%20c.Offset(1).Value%3C%2FP%3E%3CP%3EAlternatively%3C%2FP%3E%3CP%3EWeekNum%20%3D%20Application.WorksheetFunction.WeekNum(msgValueC)%3C%2FP%3E%3CP%3Eseems%20to%20work%20OK.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBTW%2C%20I%20hate%20seeing%20direct%20references%20to%20ranges%20in%20VBA.%26nbsp%3B%20A%20user%20(could%20be%20you)%20inserts%20a%20row%20and%20the%20code%20crumbles.%26nbsp%3B%20A%20named%20range%20is%20far%20safer.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-561222%22%20slang%3D%22en-US%22%3ERe%3A%20Hide%20Colums%20based%20on%20dates.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-561222%22%20slang%3D%22en-US%22%3E%3CP%3EDear%20Peter%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20for%20your%20help.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFinal%20code%20looks%20like%20this%20and%20works%20fine.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESub%20ShowCurrentWeekOnly()%3CBR%20%2F%3EDim%20c%20As%20Range%2C%20WeekNumC%20As%20Long%2C%20WeekNumNow%20As%20Long%3CBR%20%2F%3EApplication.EnableEvents%20%3D%20False%3C%2FP%3E%3CP%3EOn%20Error%20Resume%20Next%3C%2FP%3E%3CP%3EFor%20Each%20c%20In%20Range(%22B2%3AQ2%22)%3CBR%20%2F%3EWeekNumC%20%3D%20Application.WorksheetFunction.WeekNum(c.Value)%3CBR%20%2F%3EWeekNumNow%20%3D%20Application.WorksheetFunction.WeekNum(Date)%3CBR%20%2F%3EIf%20(WeekNumC%20%26lt%3B%26gt%3B%20WeekNumNow)%20Then%3CBR%20%2F%3Ec.EntireColumn.Hidden%20%3D%20True%3CBR%20%2F%3EElse%3CBR%20%2F%3Ec.EntireColumn.Hidden%20%3D%20False%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3ENext%3CBR%20%2F%3EOn%20Error%20GoTo%200%3CBR%20%2F%3EApplication.EnableEvents%20%3D%20True%3C%2FP%3E%3CP%3EEnd%20Sub%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20will%20also%20do%20some%20research%20on%20Named%20Ranges%20as%20I%20do%20not%20know%20how%20to%20do%20this%20yet.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EVonryan%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F214174%22%20target%3D%22_blank%22%3E%40Peter%20Bartholomew%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-562066%22%20slang%3D%22en-US%22%3ERe%3A%20Hide%20Colums%20based%20on%20dates.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-562066%22%20slang%3D%22en-US%22%3E%3CP%3EDear%20Peter%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EJust%20an%20update%20after%20my%20research%20on%20Named%20Ranges.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20code%20looks%20like%20this%20and%20everything%20is%20good.%20Thanks%20for%20the%20guidance.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESub%20ShowCurrentWeekOnly()%3CBR%20%2F%3EDim%20c%20As%20Range%2C%20WeekNumC%20As%20Long%2C%20WeekNumNow%20As%20Long%3CBR%20%2F%3EApplication.EnableEvents%20%3D%20False%3C%2FP%3E%3CP%3EOn%20Error%20Resume%20Next%3C%2FP%3E%3CP%3EFor%20Each%20c%20In%20Range(%22LABOURRANGE%22)%3CBR%20%2F%3EWeekNumC%20%3D%20Application.WorksheetFunction.WeekNum(c.Value)%3CBR%20%2F%3EWeekNumNow%20%3D%20Application.WorksheetFunction.WeekNum(Date)%3CBR%20%2F%3EIf%20(WeekNumC%20%26lt%3B%26gt%3B%20WeekNumNow)%20Then%3CBR%20%2F%3Ec.EntireColumn.Hidden%20%3D%20True%3CBR%20%2F%3EElse%3CBR%20%2F%3Ec.EntireColumn.Hidden%20%3D%20False%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3ENext%3CBR%20%2F%3EOn%20Error%20GoTo%200%3CBR%20%2F%3EApplication.EnableEvents%20%3D%20True%3C%2FP%3E%3CP%3EEnd%20Sub%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F323150%22%20target%3D%22_blank%22%3E%40vonryan%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-562530%22%20slang%3D%22en-US%22%3ERe%3A%20Hide%20Colums%20based%20on%20dates.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-562530%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F323150%22%20target%3D%22_blank%22%3E%40vonryan%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThat%20will%20do%20it%20%3A-)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
vonryan
Contributor

Hi All,

 

I am trying to achieve the following in VBA.

 

I have a master workbook called Master.xlsm.

I have a number of workbooks called Engineer1 to X

Engineer workbook only contains 1 worksheet (Sheet1)

 

I would like to be able to copy all the contents from Engineer1 workbook for all the Engineer workbooks to the Master Workbook.

 

I want to say copy rows 2 to 10 from Engineer 1 workbook and then copy rows 2 to 10 from Engineer2 workbook to the the cells below the data from engineer1.

 

I have tried a few examples from the internet but I cannot get the code to work.

 

Please help. I have attached my base files for your easy reference.

 

Vonryan

12 Replies

@vonryan 

Your approach is possible but these days I would recommend the use of Power Query.  Combining a set of files from a folder is core functionality of PQ and once further 'Engineer' files are added to the folder all that is needed is to click the refresh button.  Ideally the data should all be contained in Tables

 

 

Data ribbon tab

Get Data

From File

From Folder

Choose 'combine'

Select the Table to import from each file

Edit as needed

Close and load to sheet within Master

 

Peter, thanks for your reply. I have tried your described method but I seem to be struggling.

 

I have one file Master.xlsm on D:\

I have four files Engineer1 thro Engineer4 on D:\Test\

 

I go to data ribbon and select New Query\From File\ From Folder and set the path to D:\Test

 

the query selects the 4 files and I press Combine and load.

 

THe combinne dialogue window appears and I select Example file as Engineer1. The prview pane show me the layout which is fine.

 

I press the OK button and the query starts running.

 

I then get a error message 
[Expression.Error] The key didn't match any rows in the table..

 

Please see attached screenshot

What am I doing wrong please

 

Vonryan@Peter Bartholomew 

Solution

@vonryan 

The screenshot is very close to that given by working versions.

One thought is that the unit of interaction between PowerQuery and Excel is the Table.  I think it may be possible work with Named Ranges or the UsedRegion of a Sheet but they are not the preferred options. 

 

Unless you have already done it, I would suggest going through each of your four files and converting the data to a Table [Ctrl/T].  This will give a default name of Table1 but it can be renamed to give some more meaningful reference such as 'EngineeringTask' (the same name in each file).  Then, within the Combine & Edit dialogue, select the Table name to be the basis for extracting data from the sample file.

 

Fingers crossed this takes us a step forward!

 

 

@Peter Bartholomew , From Folder connector works fine with entire sheets. The only point data for all files in the folder have to have the same structure.

@Sergei Baklan 

Thank you.  From the selections available on the combine files menu, I suspected that was the case.  I wouldn't have personal experience because I haven't used location (Sheet and cell notation) to reference data objects for a number of years now.  Tables are simply a more natural starting point for me and, most recently, this is followed by spilt arrays in which only the anchor cell is named.

 

Oddly enough, it is sometimes difficult to establish a dialogue with Excel users; wonder why that is? ;-)

@Peter Bartholomew ,

 

That could be tables. With them even easier. But with that all your files  are to be with exactly the same Table1 with exactly the same column names. Only values could be different. What is done, you take any file in the folder as a sample one, transform it (it could be Tables, Named ranges or entire sheet) and generated for such transformation function is applied to all files in the folder. If files have different structure you failed.

 

Oh, Excel users are quite different. Even if speak about the Excel, sometimes we speak on different languages. That's not good and not bad, that as it is.

Gentlemen,

 

You have been a great help. Thank you. I have achieved my goal thanks to your help.

 

I will investigate Tables when I get the chance.

 

Vonryan

 

@Sergei Baklan 

Hi Peter,

 

I have a new challenge if you are interested.

 

I wish to hide culumns in a worksheet based on dates.

 

On worksheet 1 from B2:Q2 are cells with dates. Each cell is one week apart.

 

I have created a button on the page and it triggers a sub routine that is supposed to check the current date (Today()) function and evaluate this date into a week number using [Weeknum(Today()].

 

It then checks the cell Range (B2:Q2) date and evaluates these dates for [Weeknum(c.Value)]

 

If the week number of the date in the Range of cells B2:Q2 is <> to week number of today then hide the columns as I only want to see this weeks column.

 

I have written the following code to try and resolve my isssue by using a message box to return the values of the variables.

 

I cannot get the Week Number function to work.

 

Sub ShowCurrentWeekOnly()
Dim c As Range, msgValueC As Long, msgValueWeeknumC As Long, msgValueDate As Long, msgValueWeeknumDate As Long, WeekNum As Long
Application.EnableEvents = False

On Error Resume Next

For Each c In Range("B2:Q2")
msgValueC = c.Value
WeekNum = Evaluate("=weeknum(msgValueC)")
msgValueWeeknumC = WeekNum
msgValueDate = Date
msgValueWeeknumDate = [Weeknum(Date()]
MsgBox "Returned values are " & msgValueC & " " & msgValueWeeknumC & " " & msgValueDate & " " & msgValueWeeknumDate
If WeekNum <> [weeknum(Date())] Then
c.EntireColumn.Hidden = True
Else
c.EntireColumn.Hidden = False
End If
Next
On Error GoTo 0
Application.EnableEvents = True

End Sub

 

The week number always returns zero.

 

I wonder if you can help.

@vonryan 

@vonryan 

The statement

WeekNum = [Weeknum(msgValueC)]

gave me an error 2029.

You could calculate the week numbers on the spreadsheet and then read them from the module using something of the form

WeekNumber = c.Offset(1).Value

Alternatively

WeekNum = Application.WorksheetFunction.WeekNum(msgValueC)

seems to work OK.

 

BTW, I hate seeing direct references to ranges in VBA.  A user (could be you) inserts a row and the code crumbles.  A named range is far safer.

Dear Peter,

 

Thank you for your help.

 

Final code looks like this and works fine.

 

Sub ShowCurrentWeekOnly()
Dim c As Range, WeekNumC As Long, WeekNumNow As Long
Application.EnableEvents = False

On Error Resume Next

For Each c In Range("B2:Q2")
WeekNumC = Application.WorksheetFunction.WeekNum(c.Value)
WeekNumNow = Application.WorksheetFunction.WeekNum(Date)
If (WeekNumC <> WeekNumNow) Then
c.EntireColumn.Hidden = True
Else
c.EntireColumn.Hidden = False
End If
Next
On Error GoTo 0
Application.EnableEvents = True

End Sub

 

I will also do some research on Named Ranges as I do not know how to do this yet.

 

Vonryan

@Peter Bartholomew 

Dear Peter,

 

Just an update after my research on Named Ranges.

 

The code looks like this and everything is good. Thanks for the guidance.

 

Sub ShowCurrentWeekOnly()
Dim c As Range, WeekNumC As Long, WeekNumNow As Long
Application.EnableEvents = False

On Error Resume Next

For Each c In Range("LABOURRANGE")
WeekNumC = Application.WorksheetFunction.WeekNum(c.Value)
WeekNumNow = Application.WorksheetFunction.WeekNum(Date)
If (WeekNumC <> WeekNumNow) Then
c.EntireColumn.Hidden = True
Else
c.EntireColumn.Hidden = False
End If
Next
On Error GoTo 0
Application.EnableEvents = True

End Sub

@vonryan 

@vonryan 

That will do it :-)