Forum Discussion

UG_1180's avatar
UG_1180
Copper Contributor
Oct 11, 2021
Solved

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

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 ...
  • HansVogelaar's avatar
    HansVogelaar
    Oct 12, 2021

    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

Resources