SOLVED

VBA - export two ranges to a CSV file

%3CLINGO-SUB%20id%3D%22lingo-sub-2270788%22%20slang%3D%22en-US%22%3EVBA%20-%20export%20two%20ranges%20to%20a%20CSV%20file%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2270788%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20everyone!%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EI%20have%20the%20following%20VBA%20code%20that%20saves%20a%20Excel%20range%20in%20a%20CSV%20file.%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EBut%20I%20need%20to%20increment%20this%20code%20so%20that%20I%20can%20insert%2C%20in%20the%20same%20CSV%20file%2C%20a%20second%20sheet%20with%20another%20Excel%20range.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3EHow%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3ESub%20saveRangeToCSV()%0A%0A%20%20%20%20Dim%20myCSVFileName%20As%20String%0A%20%20%20%20Dim%20myWB%20As%20Workbook%0A%20%20%20%20Dim%20tempWB%20As%20Workbook%0A%20%20%20%20Dim%20rngToSave%20As%20Range%0A%20%20%20%20Dim%20Sharepoint%20As%20String%0A%20%20%20%20Dim%20Usuario%20As%20String%0A%20%20%20%20Dim%20Protocolo%20As%20String%0A%20%20%20%20%0A%20%20%20%20Sharepoint%20%3D%20Sheets(%22Config%22).Range(%22D5%22).Text%0A%20%20%20%20Usuario%20%3D%20Sheets(%22Config%22).Range(%22L6%22).Text%0A%20%20%20%20Protocolo%20%3D%20Sheets(%22Config%22).Range(%22J6%22).Text%0A%0A%20%20%20%20Application.DisplayAlerts%20%3D%20False%0A%20%20%20%20On%20Error%20GoTo%20err%0A%0A%20%20%20%20myCSVFileName%20%3D%20Sharepoint%20%26amp%3B%20%22%5C%22%20%26amp%3B%20Usuario%20%26amp%3B%20%22%20%22%20%26amp%3B%20Protocolo%20%26amp%3B%20%22%20%22%20%26amp%3B%20VBA.Format(VBA.Now%2C%20%22dd-MMM-yyyy%20hh-mm%22)%20%26amp%3B%20%22.csv%22%0A%0A%20%20%20%20Set%20rngToSave%20%3D%20Range(%22J6%3ABB6%22)%0A%20%20%20%20rngToSave.Copy%0A%0A%20%20%20%20Set%20tempWB%20%3D%20Application.Workbooks.Add(1)%0A%20%20%20%20With%20tempWB%0A%20%20%20%20%20%20%20%20.Sheets(1).Range(%22A1%22).PasteSpecial%20xlPasteValues%0A%20%20%20%20%20%20%20%20.SaveAs%20Filename%3A%3DmyCSVFileName%2C%20FileFormat%3A%3DxlCSV%2C%20CreateBackup%3A%3DFalse%0A%20%20%20%20%20%20%20%20.Close%0A%20%20%20%20End%20With%0A%20%20%20%20%0Aerr%3A%0A%20%20%20%20Application.DisplayAlerts%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2270788%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-2270794%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20-%20export%20two%20ranges%20to%20a%20CSV%20file%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2270794%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F964793%22%20target%3D%22_blank%22%3E%40LeandroCampacci%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETry%20something%20along%20the%20following%20lines%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3ESub%20saveRangeToCSV()%0A%20%20%20%20Dim%20myCSVFileName%20As%20String%0A%20%20%20%20Dim%20myWB%20As%20Workbook%0A%20%20%20%20Dim%20tempWB%20As%20Workbook%0A%20%20%20%20Dim%20rngToSave%20As%20Range%0A%20%20%20%20Dim%20Sharepoint%20As%20String%0A%20%20%20%20Dim%20Usuario%20As%20String%0A%20%20%20%20Dim%20Protocolo%20As%20String%0A%20%20%20%20%0A%20%20%20%20Set%20myWB%20%3D%20ActiveWorkbook%20'%20or%20ThisWorkbook%0A%20%20%20%20Sharepoint%20%3D%20myWB.Sheets(%22Config%22).Range(%22D5%22).Text%0A%20%20%20%20Usuario%20%3D%20myWB.Sheets(%22Config%22).Range(%22L6%22).Text%0A%20%20%20%20Protocolo%20%3D%20myWB.Sheets(%22Config%22).Range(%22J6%22).Text%0A%0A%20%20%20%20Application.DisplayAlerts%20%3D%20False%0A%20%20%20%20On%20Error%20GoTo%20err%0A%0A%20%20%20%20myCSVFileName%20%3D%20Sharepoint%20%26amp%3B%20%22%5C%22%20%26amp%3B%20Usuario%20%26amp%3B%20%22%20%22%20%26amp%3B%20Protocolo%20%26amp%3B%20_%0A%20%20%20%20%20%20%20%20%22%20%22%20%26amp%3B%20Format(VBA.Now%2C%20%22dd-MMM-yyyy%20hh-mm%22)%20%26amp%3B%20%22.csv%22%0A%0A%20%20%20%20Set%20tempWB%20%3D%20Application.Workbooks.Add(1)%0A%20%20%20%20With%20tempWB%0A%20%20%20%20%20%20%20%20Set%20rngToSave%20%3D%20myWB.Worksheets(%22SheetOne%22).Range(%22J6%3ABB6%22)%0A%20%20%20%20%20%20%20%20rngToSave.Copy%0A%20%20%20%20%20%20%20%20.Sheets(1).Range(%22A1%22).PasteSpecial%20xlPasteValues%0A%20%20%20%20%20%20%20%20Set%20rngToSave%20%3D%20myWB.Worksheets(%22SheetTwo%22).Range(%22J6%3ABB6%22)%0A%20%20%20%20%20%20%20%20rngToSave.Copy%0A%20%20%20%20%20%20%20%20.Sheets(1).Range(%22A2%22).PasteSpecial%20xlPasteValues%0A%20%20%20%20%20%20%20%20.SaveAs%20Filename%3A%3DmyCSVFileName%2C%20FileFormat%3A%3DxlCSV%2C%20CreateBackup%3A%3DFalse%0A%20%20%20%20%20%20%20%20.Close%0A%20%20%20%20End%20With%0A%0Aerr%3A%0A%20%20%20%20Application.DisplayAlerts%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2270857%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20-%20export%20two%20ranges%20to%20a%20CSV%20file%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2270857%22%20slang%3D%22en-US%22%3E%3CP%3E%3CBR%20%2F%3E%3CSTRIKE%3EHelo!%20It%20doesn't%20seem%20to%20work.%20The%20routine%20creates%20a%20new%20file%20and%20pastes%20only%20the%20first%20interval.%20There%20is%20no%20creation%20of%20a%20second%20sheet%20and%20the%20process%20for%20opening%20the%20new%20temporary%20file.%3C%2FSTRIKE%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRIKE%3EEdit%3A%20seems%20to%20work%20until%20firts%20copy%20and%20paste.%20The%20code%20stops%26nbsp%3Bwhen%20to%20select%20the%20second%20interval.%3C%2FSTRIKE%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENevermind.%20Works%20so%20fine%2C%20but%20how%20I%20add%20the%20second%20range%20as%20a%20new%20sheet%3F%3C%2FP%3E%3CP%3EIt's%20possible%20to%20rename%20these%20two%20new%20sheets%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2270932%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20-%20export%20two%20ranges%20to%20a%20CSV%20file%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2270932%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F964793%22%20target%3D%22_blank%22%3E%40LeandroCampacci%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20example%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3ESub%20saveRangeToCSV()%0A%20%20%20%20Dim%20myCSVFileName%20As%20String%0A%20%20%20%20Dim%20myWB%20As%20Workbook%0A%20%20%20%20Dim%20tempWB%20As%20Workbook%0A%20%20%20%20Dim%20ws1%20As%20Worksheet%0A%20%20%20%20Dim%20ws2%20As%20Worksheet%0A%20%20%20%20Dim%20rngToSave%20As%20Range%0A%20%20%20%20Dim%20Sharepoint%20As%20String%0A%20%20%20%20Dim%20Usuario%20As%20String%0A%20%20%20%20Dim%20Protocolo%20As%20String%0A%0A%20%20%20%20Set%20myWB%20%3D%20ActiveWorkbook%20'%20or%20ThisWorkbook%0A%20%20%20%20Sharepoint%20%3D%20myWB.Sheets(%22Config%22).Range(%22D5%22).Text%0A%20%20%20%20Usuario%20%3D%20myWB.Sheets(%22Config%22).Range(%22L6%22).Text%0A%20%20%20%20Protocolo%20%3D%20myWB.Sheets(%22Config%22).Range(%22J6%22).Text%0A%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%20%20Application.DisplayAlerts%20%3D%20False%0A%20%20%20%20On%20Error%20GoTo%20err%0A%0A%20%20%20%20myCSVFileName%20%3D%20Sharepoint%20%26amp%3B%20%22%5C%22%20%26amp%3B%20Usuario%20%26amp%3B%20%22%20%22%20%26amp%3B%20Protocolo%20%26amp%3B%20_%0A%20%20%20%20%20%20%20%20%22%20%22%20%26amp%3B%20Format(VBA.Now%2C%20%22dd-MMM-yyyy%20hh-mm%22)%20%26amp%3B%20%22.csv%22%0A%0A%20%20%20%20Set%20tempWB%20%3D%20Workbooks.Add(xlWBATWorksheet)%0A%20%20%20%20Set%20ws1%20%3D%20tempWB.Worksheets(1)%0A%20%20%20%20ws1.Name%20%3D%20%22Name%201%22%0A%20%20%20%20Set%20ws2%20%3D%20tempWB.Worksheets.Add(After%3A%3Dws1)%0A%20%20%20%20ws2.Name%20%3D%20%22Name%202%22%0A%0A%20%20%20%20Set%20rngToSave%20%3D%20myWB.Worksheets(%22SheetOne%22).Range(%22J6%3ABB6%22)%0A%20%20%20%20rngToSave.Copy%0A%20%20%20%20ws1.Range(%22A1%22).PasteSpecial%20xlPasteValues%0A%20%20%20%20Set%20rngToSave%20%3D%20myWB.Worksheets(%22SheetTwo%22).Range(%22J6%3ABB6%22)%0A%20%20%20%20rngToSave.Copy%0A%20%20%20%20ws2.Range(%22A1%22).PasteSpecial%20xlPasteValues%0A%20%20%20%20With%20tempWB%0A%20%20%20%20%20%20%20%20.SaveAs%20Filename%3A%3DmyCSVFileName%2C%20FileFormat%3A%3DxlCSV%2C%20CreateBackup%3A%3DFalse%0A%20%20%20%20%20%20%20%20.Close%0A%20%20%20%20End%20With%0A%0Aerr%3A%0A%20%20%20%20Application.DisplayAlerts%20%3D%20True%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2270986%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20-%20export%20two%20ranges%20to%20a%20CSV%20file%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2270986%22%20slang%3D%22en-US%22%3EDon't%20work.%3CBR%20%2F%3E%3CBR%20%2F%3EIf%20I%20don't%20rename%20%22SheetOne%22%20or%20%22SheetTwo%22%2C%20the%20macro%20works%20until%20create%20a%20new%20temp%20file%20with%20no%20data.%3CBR%20%2F%3E%3CBR%20%2F%3EWhen%20change%20the%20sheet%20name%20(both%20ranges%20in%20%22Config%22%20sheet)%2C%20only%20second%20range%20works.%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EI%20used%20this%20code%20model%2C%20since%20I%20need%20a%20lot%20of%20data%20and%20I%20will%20have%20commas%20in%20some%20cells.%20Will%20this%20interfere%3F%3CBR%20%2F%3E%3CBR%20%2F%3E%3CA%20href%3D%22http%3A%2F%2Flearnexcelmacro.com%2Fwp%2F2017%2F09%2Fsave-excel-range-data-as-csv-file-through-excel-vba%2F%23VBA-to-save-excel-range-to-csv-method-2%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Flearnexcelmacro.com%2Fwp%2F2017%2F09%2Fsave-excel-range-data-as-csv-file-through-excel-vba%2F%23VBA-to-save-excel-range-to-csv-method-2%3C%2FA%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2271014%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20-%20export%20two%20ranges%20to%20a%20CSV%20file%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2271014%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F964793%22%20target%3D%22_blank%22%3E%40LeandroCampacci%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECould%20you%20attach%20a%20copy%20of%20your%20workbook%20without%20sensitive%20data%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2271034%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20-%20export%20two%20ranges%20to%20a%20CSV%20file%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2271034%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127945%22%20target%3D%22_blank%22%3E%40Hans%20Vogelaar%3C%2FA%3E%26nbsp%3BSure!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2271060%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20-%20export%20two%20ranges%20to%20a%20CSV%20file%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2271060%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F964793%22%20target%3D%22_blank%22%3E%40LeandroCampacci%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAh%20-%20of%20course%2C%20stupid%20of%20me.%3C%2FP%3E%0A%3CP%3EA%20.csv%20file%20is%20a%20text%20file%2C%20It%20cannot%20contain%20multiple%20worksheets!%3C%2FP%3E%0A%3CP%3ESo%20only%20one%20of%20the%20ranges%20ends%20up%20in%20the%20.csv%20file.%3C%2FP%3E%0A%3CP%3EWould%20it%20be%20OK%20to%20export%202%20csv%20files%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2272224%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20-%20export%20two%20ranges%20to%20a%20CSV%20file%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2272224%22%20slang%3D%22en-US%22%3ENo%20problem!%20True%2C%20I%20also%20forgot%20this.%20How%20would%20it%20look%20for%20two%20files%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2272245%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20-%20export%20two%20ranges%20to%20a%20CSV%20file%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2272245%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F964793%22%20target%3D%22_blank%22%3E%40LeandroCampacci%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELike%20this%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3ESub%20saveRangeToCSV()%0A%20%20%20%20Dim%20myCSVFileName%20As%20String%0A%20%20%20%20Dim%20myWB%20As%20Workbook%0A%20%20%20%20Dim%20tempWB%20As%20Workbook%0A%20%20%20%20Dim%20ws%20As%20Worksheet%0A%20%20%20%20Dim%20rngToSave%20As%20Range%0A%20%20%20%20'Dim%20Sharepoint%20As%20String%0A%20%20%20%20'Dim%20Usuario%20As%20String%0A%20%20%20%20Dim%20Protocolo%20As%20String%0A%0A%20%20%20%20Set%20myWB%20%3D%20ActiveWorkbook%20'%20or%20ThisWorkbook%0A%20%20%20%20'Sharepoint%20%3D%20myWB.Sheets(%22Config%22).Range(%22D5%22).Text%0A%20%20%20%20'Usuario%20%3D%20myWB.Sheets(%22Config%22).Range(%22L6%22).Text%0A%20%20%20%20'Protocolo%20%3D%20myWB.Sheets(%22Config%22).Range(%22J6%22).Text%0A%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%20%20Application.DisplayAlerts%20%3D%20False%0A%20%20%20%20On%20Error%20GoTo%20err%0A%0A%20%20%20%20'%20First%20export%0A%20%20%20%20Set%20tempWB%20%3D%20Workbooks.Add(xlWBATWorksheet)%0A%20%20%20%20Set%20ws%20%3D%20tempWB.Worksheets(1)%0A%20%20%20%20ws.Name%20%3D%20%22Export%20A%22%0A%20%20%20%20Set%20rngToSave%20%3D%20myWB.Worksheets(%22Config%22).Range(%22J5%3ABB6%22)%0A%20%20%20%20rngToSave.Copy%0A%20%20%20%20ws.Range(%22A1%22).PasteSpecial%20xlPasteValues%0A%20%20%20%20myCSVFileName%20%3D%20myWB.Path%20%26amp%3B%20%22%5C%22%20%26amp%3B%20%22CSV-Exported-File-A-%22%20%26amp%3B%20VBA.Format(VBA.Now%2C%20%22dd-MMM-yyyy%20hh-mm%22)%20%26amp%3B%20%22.csv%22%0A%20%20%20%20With%20tempWB%0A%20%20%20%20%20%20%20%20.SaveAs%20Filename%3A%3DmyCSVFileName%2C%20FileFormat%3A%3DxlCSV%2C%20CreateBackup%3A%3DFalse%0A%20%20%20%20%20%20%20%20.Close%0A%20%20%20%20End%20With%0A%0A%20%20%20%20'%20Second%20export%0A%20%20%20%20Set%20tempWB%20%3D%20Workbooks.Add(xlWBATWorksheet)%0A%20%20%20%20Set%20ws%20%3D%20tempWB.Worksheets(1)%0A%20%20%20%20ws.Name%20%3D%20%22Export%20B%22%0A%20%20%20%20Set%20rngToSave%20%3D%20myWB.Worksheets(%22Config%22).Range(%22D8%3AE20%22)%0A%20%20%20%20rngToSave.Copy%0A%20%20%20%20ws.Range(%22A1%22).PasteSpecial%20xlPasteValues%0A%20%20%20%20myCSVFileName%20%3D%20myWB.Path%20%26amp%3B%20%22%5C%22%20%26amp%3B%20%22CSV-Exported-File-B-%22%20%26amp%3B%20VBA.Format(VBA.Now%2C%20%22dd-MMM-yyyy%20hh-mm%22)%20%26amp%3B%20%22.csv%22%0A%20%20%20%20With%20tempWB%0A%20%20%20%20%20%20%20%20.SaveAs%20Filename%3A%3DmyCSVFileName%2C%20FileFormat%3A%3DxlCSV%2C%20CreateBackup%3A%3DFalse%0A%20%20%20%20%20%20%20%20.Close%0A%20%20%20%20End%20With%0A%0Aerr%3A%0A%20%20%20%20Application.DisplayAlerts%20%3D%20True%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2272267%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20-%20export%20two%20ranges%20to%20a%20CSV%20file%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2272267%22%20slang%3D%22en-US%22%3EWorked%20so%20fine.%20Thanks%20for%20the%20support%2C%20Hans!%3C%2FLINGO-BODY%3E
Occasional Contributor

Hi everyone!


I have the following VBA code that saves a Excel range in a CSV file.


But I need to increment this code so that I can insert, in the same CSV file, a second sheet with another Excel range.

How?

 

Sub saveRangeToCSV()

    Dim myCSVFileName As String
    Dim myWB As Workbook
    Dim tempWB As Workbook
    Dim rngToSave As Range
    Dim Sharepoint As String
    Dim Usuario As String
    Dim Protocolo As String
    
    Sharepoint = Sheets("Config").Range("D5").Text
    Usuario = Sheets("Config").Range("L6").Text
    Protocolo = Sheets("Config").Range("J6").Text

    Application.DisplayAlerts = False
    On Error GoTo err

    myCSVFileName = Sharepoint & "\" & Usuario & " " & Protocolo & " " & VBA.Format(VBA.Now, "dd-MMM-yyyy hh-mm") & ".csv"

    Set rngToSave = Range("J6:BB6")
    rngToSave.Copy

    Set tempWB = Application.Workbooks.Add(1)
    With tempWB
        .Sheets(1).Range("A1").PasteSpecial xlPasteValues
        .SaveAs Filename:=myCSVFileName, FileFormat:=xlCSV, CreateBackup:=False
        .Close
    End With
    
err:
    Application.DisplayAlerts = True
End Sub

 

10 Replies

@LeandroCampacci 

Try something along the following lines:

 

Sub saveRangeToCSV()
    Dim myCSVFileName As String
    Dim myWB As Workbook
    Dim tempWB As Workbook
    Dim rngToSave As Range
    Dim Sharepoint As String
    Dim Usuario As String
    Dim Protocolo As String
    
    Set myWB = ActiveWorkbook ' or ThisWorkbook
    Sharepoint = myWB.Sheets("Config").Range("D5").Text
    Usuario = myWB.Sheets("Config").Range("L6").Text
    Protocolo = myWB.Sheets("Config").Range("J6").Text

    Application.DisplayAlerts = False
    On Error GoTo err

    myCSVFileName = Sharepoint & "\" & Usuario & " " & Protocolo & _
        " " & Format(VBA.Now, "dd-MMM-yyyy hh-mm") & ".csv"

    Set tempWB = Application.Workbooks.Add(1)
    With tempWB
        Set rngToSave = myWB.Worksheets("SheetOne").Range("J6:BB6")
        rngToSave.Copy
        .Sheets(1).Range("A1").PasteSpecial xlPasteValues
        Set rngToSave = myWB.Worksheets("SheetTwo").Range("J6:BB6")
        rngToSave.Copy
        .Sheets(1).Range("A2").PasteSpecial xlPasteValues
        .SaveAs Filename:=myCSVFileName, FileFormat:=xlCSV, CreateBackup:=False
        .Close
    End With

err:
    Application.DisplayAlerts = True
End Sub


Helo! It doesn't seem to work. The routine creates a new file and pastes only the first interval. There is no creation of a second sheet and the process for opening the new temporary file.

 

Edit: seems to work until firts copy and paste. The code stops when to select the second interval.

 

Nevermind. Works so fine, but how I add the second range as a new sheet?

It's possible to rename these two new sheets?

@LeandroCampacci 

For example:

Sub saveRangeToCSV()
    Dim myCSVFileName As String
    Dim myWB As Workbook
    Dim tempWB As Workbook
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim rngToSave As Range
    Dim Sharepoint As String
    Dim Usuario As String
    Dim Protocolo As String

    Set myWB = ActiveWorkbook ' or ThisWorkbook
    Sharepoint = myWB.Sheets("Config").Range("D5").Text
    Usuario = myWB.Sheets("Config").Range("L6").Text
    Protocolo = myWB.Sheets("Config").Range("J6").Text

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    On Error GoTo err

    myCSVFileName = Sharepoint & "\" & Usuario & " " & Protocolo & _
        " " & Format(VBA.Now, "dd-MMM-yyyy hh-mm") & ".csv"

    Set tempWB = Workbooks.Add(xlWBATWorksheet)
    Set ws1 = tempWB.Worksheets(1)
    ws1.Name = "Name 1"
    Set ws2 = tempWB.Worksheets.Add(After:=ws1)
    ws2.Name = "Name 2"

    Set rngToSave = myWB.Worksheets("SheetOne").Range("J6:BB6")
    rngToSave.Copy
    ws1.Range("A1").PasteSpecial xlPasteValues
    Set rngToSave = myWB.Worksheets("SheetTwo").Range("J6:BB6")
    rngToSave.Copy
    ws2.Range("A1").PasteSpecial xlPasteValues
    With tempWB
        .SaveAs Filename:=myCSVFileName, FileFormat:=xlCSV, CreateBackup:=False
        .Close
    End With

err:
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub
Don't work.

If I don't rename "SheetOne" or "SheetTwo", the macro works until create a new temp file with no data.

When change the sheet name (both ranges in "Config" sheet), only second range works.


I used this code model, since I need a lot of data and I will have commas in some cells. Will this interfere?

http://learnexcelmacro.com/wp/2017/09/save-excel-range-data-as-csv-file-through-excel-vba/#VBA-to-sa...

@LeandroCampacci 

Could you attach a copy of your workbook without sensitive data?

@LeandroCampacci 

Ah - of course, stupid of me.

A .csv file is a text file, It cannot contain multiple worksheets!

So only one of the ranges ends up in the .csv file.

Would it be OK to export 2 csv files?

No problem! True, I also forgot this. How would it look for two files?
best response confirmed by LeandroCampacci (Occasional Contributor)
Solution

@LeandroCampacci 

Like this:

 

Sub saveRangeToCSV()
    Dim myCSVFileName As String
    Dim myWB As Workbook
    Dim tempWB As Workbook
    Dim ws As Worksheet
    Dim rngToSave As Range
    'Dim Sharepoint As String
    'Dim Usuario As String
    Dim Protocolo As String

    Set myWB = ActiveWorkbook ' or ThisWorkbook
    'Sharepoint = myWB.Sheets("Config").Range("D5").Text
    'Usuario = myWB.Sheets("Config").Range("L6").Text
    'Protocolo = myWB.Sheets("Config").Range("J6").Text

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    On Error GoTo err

    ' First export
    Set tempWB = Workbooks.Add(xlWBATWorksheet)
    Set ws = tempWB.Worksheets(1)
    ws.Name = "Export A"
    Set rngToSave = myWB.Worksheets("Config").Range("J5:BB6")
    rngToSave.Copy
    ws.Range("A1").PasteSpecial xlPasteValues
    myCSVFileName = myWB.Path & "\" & "CSV-Exported-File-A-" & VBA.Format(VBA.Now, "dd-MMM-yyyy hh-mm") & ".csv"
    With tempWB
        .SaveAs Filename:=myCSVFileName, FileFormat:=xlCSV, CreateBackup:=False
        .Close
    End With

    ' Second export
    Set tempWB = Workbooks.Add(xlWBATWorksheet)
    Set ws = tempWB.Worksheets(1)
    ws.Name = "Export B"
    Set rngToSave = myWB.Worksheets("Config").Range("D8:E20")
    rngToSave.Copy
    ws.Range("A1").PasteSpecial xlPasteValues
    myCSVFileName = myWB.Path & "\" & "CSV-Exported-File-B-" & VBA.Format(VBA.Now, "dd-MMM-yyyy hh-mm") & ".csv"
    With tempWB
        .SaveAs Filename:=myCSVFileName, FileFormat:=xlCSV, CreateBackup:=False
        .Close
    End With

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

 

Worked so fine. Thanks for the support, Hans!