SOLVED

How to convert and save particular columns of a .xlsm file to .csv format

%3CLINGO-SUB%20id%3D%22lingo-sub-2834731%22%20slang%3D%22en-US%22%3EHow%20to%20convert%20and%20save%20particular%20columns%20of%20a%20.xlsm%20file%20to%20.csv%20format%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2834731%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20microsoft%20excel%20comunity%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20want%20to%20create%20a%20vba%20button%20to%20save%20first%20four%20columns%20of%20the%20active%20sheet(the%20sheet%20on%20which%20the%20button%20is%20created)%20of%20a%20excel%20file%20which%20is%20in%20.xlsm%20format%20to%20.csv%20format.%20How%20can%20I%20do%20%3F%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%26nbsp%3B%3C%2FP%3E%3CP%3EUmang%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2834731%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-2834779%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20convert%20and%20save%20particular%20columns%20of%20a%20.xlsm%20file%20to%20.csv%20format%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2834779%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1168851%22%20target%3D%22_blank%22%3E%40UG_1180%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3ESub%20Export4()%0A%20%20%20%20Dim%20wbkSource%20As%20Workbook%0A%20%20%20%20Dim%20wshSource%20As%20Worksheet%0A%20%20%20%20Dim%20wbkTarget%20As%20Workbook%0A%20%20%20%20Dim%20wshTarget%20As%20Worksheet%0A%20%20%20%20Set%20wbkSource%20%3D%20ActiveWorkbook%0A%20%20%20%20Set%20wshSource%20%3D%20ActiveSheet%0A%20%20%20%20Set%20wbkTarget%20%3D%20Workbooks.Add(Template%3A%3DxlWBATWorksheet)%0A%20%20%20%20Set%20wshTarget%20%3D%20wbkTarget.Worksheets(1)%0A%20%20%20%20wshSource.Range(%22A%3AD%22).Copy%0A%20%20%20%20wshTarget.Range(%22A1%22).PasteSpecial%20Paste%3A%3DxlPasteValues%0A%20%20%20%20wbkTarget.SaveAs%20Filename%3A%3DwbkSource.Path%20%26amp%3B%20Application.PathSeparator%20%26amp%3B%20%22Export.csv%22%2C%20FileFormat%3A%3DxlCSV%0A%20%20%20%20wbkTarget.Close%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2836096%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20convert%20and%20save%20particular%20columns%20of%20a%20.xlsm%20file%20to%20.csv%20format%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2836096%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%3B%3C%2FP%3E%3CP%3EThank%20you%20so%20much%20for%20the%20reply.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFile%20export%20into%20.csv%20is%20working%20good%20for%20me%20with%20this%20code%20but%20two%20issues-%3A%3C%2FP%3E%3CP%3E1)%20Column%201%20in%20my%20excel%20contains%20time%20and%20date%20in%20yyyy-mm-dd%20hh%3Amm%3Ass%20format%20but%20when%20I%20export%2C%20it%20converts%20to%20a%20number%20format.%20How%20can%20I%20keep%20the%20same%20formatting%3F%3F%3C%2FP%3E%3CP%3E2)%20I%20want%20to%20change%20the%20name%20of%20the%20exported%20file%20(.csv%20file)%20to%20include%20time%20and%20date%20at%20which%20it%20is%20being%20saved%20so%20that%20every%20time%20the%20file%20is%20not%20saved%20with%20the%20same%20name%20and%20it%20is%20easy%20to%20manage%20the%20saved%20files%20with%20different%20names.%20Will%20it%20be%20possible%3F%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you.%3C%2FP%3E%3CP%3EUmang%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2837040%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20convert%20and%20save%20particular%20columns%20of%20a%20.xlsm%20file%20to%20.csv%20format%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2837040%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1168851%22%20target%3D%22_blank%22%3E%40UG_1180%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESee%20if%20you%20can%20adapt%20this%20to%20suit%20your%20needs%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3ESub%20Export4()%0A%20%20%20%20Dim%20wbkSource%20As%20Workbook%0A%20%20%20%20Dim%20wshSource%20As%20Worksheet%0A%20%20%20%20Dim%20wbkTarget%20As%20Workbook%0A%20%20%20%20Dim%20wshTarget%20As%20Worksheet%0A%20%20%20%20Set%20wbkSource%20%3D%20ActiveWorkbook%0A%20%20%20%20Set%20wshSource%20%3D%20ActiveSheet%0A%20%20%20%20Set%20wbkTarget%20%3D%20Workbooks.Add(Template%3A%3DxlWBATWorksheet)%0A%20%20%20%20Set%20wshTarget%20%3D%20wbkTarget.Worksheets(1)%0A%20%20%20%20wshSource.Range(%22A%3AD%22).Copy%0A%20%20%20%20wshTarget.Range(%22A1%22).PasteSpecial%20Paste%3A%3DxlPasteValuesAndNumberFormats%0A%20%20%20%20wbkTarget.SaveAs%20Filename%3A%3DwbkSource.Path%20%26amp%3B%20Application.PathSeparator%20%26amp%3B%20_%0A%20%20%20%20%20%20%20%20%22Export%22%20%26amp%3B%20Format(Now%2C%20%22_yyyy_mm_dd_hh_nn_ss%22)%20%26amp%3B%20%22.csv%22%2C%20FileFormat%3A%3DxlCSV%0A%20%20%20%20wbkTarget.Close%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Hello microsoft excel comunity,

 

I want to create a vba button to save first four columns of the active sheet(the sheet on which the button is created) of a excel file which is in .xlsm format to .csv format. How can I do ??

 

Thank you 

Umang 

7 Replies

@UG_1180 

Sub Export4()
    Dim wbkSource As Workbook
    Dim wshSource As Worksheet
    Dim wbkTarget As Workbook
    Dim wshTarget As Worksheet
    Set wbkSource = ActiveWorkbook
    Set wshSource = ActiveSheet
    Set wbkTarget = Workbooks.Add(Template:=xlWBATWorksheet)
    Set wshTarget = wbkTarget.Worksheets(1)
    wshSource.Range("A:D").Copy
    wshTarget.Range("A1").PasteSpecial Paste:=xlPasteValues
    wbkTarget.SaveAs Filename:=wbkSource.Path & Application.PathSeparator & "Export.csv", FileFormat:=xlCSV
    wbkTarget.Close
End Sub

@Hans Vogelaar 

Thank you so much for the reply.

 

File export into .csv is working good for me with this code but two issues-:

1) Column 1 in my excel contains time and date in yyyy-mm-dd hh:mm:ss format but when I export, it converts to a number format. How can I keep the same formatting??

2) I want to change the name of the exported file (.csv file) to include time and date at which it is being saved so that every time the file is not saved with the same name and it is easy to manage the saved files with different names. Will it be possible??

 

Thank you.

Umang 

best response confirmed by UG_1180 (Occasional Contributor)
Solution

@UG_1180 

See if you can adapt this to suit your needs:

Sub Export4()
    Dim wbkSource As Workbook
    Dim wshSource As Worksheet
    Dim wbkTarget As Workbook
    Dim wshTarget As Worksheet
    Set wbkSource = ActiveWorkbook
    Set wshSource = ActiveSheet
    Set wbkTarget = Workbooks.Add(Template:=xlWBATWorksheet)
    Set wshTarget = wbkTarget.Worksheets(1)
    wshSource.Range("A:D").Copy
    wshTarget.Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    wbkTarget.SaveAs Filename:=wbkSource.Path & Application.PathSeparator & _
        "Export" & Format(Now, "_yyyy_mm_dd_hh_nn_ss") & ".csv", FileFormat:=xlCSV
    wbkTarget.Close
End Sub
Hello Hans,
This works perfectly!!!!

Thanks a lot.

@Hans Vogelaar 

Hello Hans,
I just need a small modification in the code so that it does not copy the last row of the first column while exporting it to the .csv file.

 

Can you please help as I tried myself but its not working out properly.

Thank You

@UG_1180 

Does this do what you want?

Sub Export4()
    Dim wbkSource As Workbook
    Dim wshSource As Worksheet
    Dim wbkTarget As Workbook
    Dim wshTarget As Worksheet
    Set wbkSource = ActiveWorkbook
    Set wshSource = ActiveSheet
    Set wbkTarget = Workbooks.Add(Template:=xlWBATWorksheet)
    Set wshTarget = wbkTarget.Worksheets(1)
    wshSource.Range("A:D").Copy
    wshTarget.Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    wshTarget.Range("A" & wshTarget.Rows.Count).End(xlUp).ClearContents
    wbkTarget.SaveAs Filename:=wbkSource.Path & Application.PathSeparator & _
        "Export" & Format(Now, "_yyyy_mm_dd_hh_nn_ss") & ".csv", FileFormat:=xlCSV
    wbkTarget.Close
End Sub