Saving a specific worksheet in csv by using Dialog box Save As, so that the user can choose the path

Gwhaou

Board Regular
Joined
May 10, 2022
Messages
78
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hi,

Actually I'm using this code, to save a specific worksheet in CSV, on a predefined folder path, with this code the user can add the name for the file and it will be saved on the predefined path : here is on the desktop


VBA Code:
Public Sub Save_CSV()


Application.ScreenUpdating = False

Sheets("DATA_SHEET").Select
Dim N_fold As Variant
Dim Stock_N As String


On Error GoTo Canceled

N_fold = InputBox("Your file will be saved on the desktop.", "Save in CSV", "add a name to the file")

If StrPtr(N_fold) = 0 Then Exit Sub

Stock_N = N_fichier

With ActiveSheet

.Copy
ActiveWorkbook.SaveAs "C:\Users\Johnny\Desktop\" & Stock_N & "_" & .Name & ".csv", xlCSV, Local:=True
ActiveWorkbook.Close False
.Activate


End With


Canceled:
Application.ScreenUpdating = True

End Sub


I want to use dialog box (Save as) predefined on CSV format, sorry i'm new to vba 😥 i don't know how to use dialog box to save one particular sheet on csv when the user use that macro.
I need some help 🙏
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
To prompt the user to specify or select a path and/or filename, try...

VBA Code:
    Dim saveAsFilename As Variant
    saveAsFilename = Application.GetSaveAsFilename( _
        InitialFileName:="filename.csv", _
        FileFilter:="CSV Files (*.csv), *.csv", _
        Title:="Save As")
  
    If saveAsFilename = False Then Exit Sub

Change the name of the file used for InitialFileName accordingly. Then the workbook can be saved using saveAsFilename for the path and filename.

Hope this helps!
 
Upvote 0
Hi yesterday i found a solution,

VBA Code:
Public Sub Save_CSV()

Dim FileName As String
FileName = Application.GetSaveAsFilename(FileFilter:="Excel Files (*.csv), *.csv")

    If FileName = "False" Then
        MsgBox "No filename specified!", vbExclamation
    Else
    Sheets("DATA_SHEET").Select
    ActiveSheet.Copy
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs FileName:=FileName, FileFormat:=xlCSV
    ActiveWorkbook.Close SaveChanges:=False
    Application.DisplayAlerts = True
    
    End If
  
End Sub

It works, the user can save the sheet called "DATA_SHEET" with a dialog box (Save As) in CSV.
But i have another problem. When the sheet is copied and saved the format is not correct :
All the data are in on cell.

Do you know how to fix that ?
 

Attachments

  • Capture2.PNG
    Capture2.PNG
    6.5 KB · Views: 7
  • Capture1.PNG
    Capture1.PNG
    8 KB · Views: 8
Upvote 0
Actually, I use a Template workbook to transfer needed data with macro from one base sheet to the sheet called "DATA_SHEET" after that I want to save that Sheet seperately on csv format because. U see the Template workbook will delete all the data and initialize when it close to allow another user to use the Template workbook.
So with the code "Save_CSV" the user can save that actual sheet in csv in the folder that he choose.

The code works but as i said the format has change from that (on the DATA_SHEET):

Capture1.PNG


to that (the saved one in csv ) :

Capture2.PNG


I don't know why Excel do that 🥲
 
Upvote 0
If it doesn't work, it's ok I gonna use the first code in which the folder path is already defined, with that code the csv file is saved as it is.
 
Upvote 0
The only way i can replicate your issue is by having the List Separator set to ; in the region setting.
 
Upvote 0
The only way i can replicate your issue is by having the List Separator set to ; in the region setting.
I tried both ";" and ",", And launched the macro. The result remain the same. by the way thank you for your help
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top