Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

Excel VBA: export range as csv

Hello experts,

The following procedure allows me to export range to a csv file:

Sub CSV_Export_Based_On_Range()

    Dim myCSVFileName As String
    Dim myWB As Workbook
    Dim tempWB As Workbook
    Dim rngToSave As Range

    Application.DisplayAlerts = False
    

    Set myWB = ThisWorkbook

    Set rngToSave = Range("A2:C30")
    rngToSave.Copy

    Set tempWB = Application.Workbooks.Add(1)
    
    On Error GoTo Error_Routine
    With tempWB
        .Sheets(1).Range("A1").PasteSpecial xlPasteValues
        .SaveAs Filename:=ActiveSheet.Name & "_" & Format(Now, "YYYYMMDDMMSS"), _
        FileFormat:=xlCSV, _
        CreateBackup:=False, Local:=True
        
        .Close
    End With

   Shell "C:\WINDOWS\explorer.exe """ & ActiveWorkbook.Path & "\", vbNormalFocus
   Application.ScreenUpdating = True
    Exit Sub
Error_Routine:
    MsgBox err.Description, vbExclamation, "Something went wrong!"
    Application.ScreenUpdating = True

End Sub

Open in new window


I would like to add the following requirements:

1-Inputbox related to the range to export: "Please Enter range that you want to export, example: A;C"
2-Loop from first row till the last used range involved by range reported in first inputbox.

If you have questions, please contact me.
SOLUTION
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Luis Diaz

ASKER

Thank you Subodh. I will test it and keep you informed.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much Subodh.

I tested and it works!

I have reviewed the question and I don't know why I request to export based on the following format:
A;C. I think that the most logic is to request to report like this A:C. I don't know if this can be modified.

If not I will select your previous proposal as solution.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you Subodh.

I tested and it works!

Thank you for your help.
You're welcome Luis!