Hi,
I currently have this code :
Attaching the file file test.xlsb
Sub btnSaveDataCSV_Click()
Dim csvFileName As String
Dim fs As FileSystemObject ' ***** need Microsoft Scripting Runtine Reference
Dim exportFile As TextStream
Dim dataSheet As Worksheet
Dim dataSheetRow As Long
Dim dataSheetCol As Integer
Dim lastRow As Long
Dim lastColumn As Integer
Dim csvLine As String
' save the current data on the data sheet to a CSV file
csvFileName = GetFolder
If csvFileName = "" Then Exit Sub
csvFileName = csvFileName & "\" & ThisWorkbook.Sheets("Main").Range("C2") & ".csv"
'If MsgBox("Do you want to create " & csvFileName & "?", vbYesNo, "Confirm") = vbNo Then Exit Sub
Set fs = New FileSystemObject
' create the file - Should the file be overwritten without warning???
Set exportFile = fs.CreateTextFile(csvFileName, True)
Set dataSheet = ThisWorkbook.Sheets("Data")
lastRow = dataSheet.Cells(dataSheet.Rows.Count, 1).End(xlUp).Row
lastColumn = dataSheet.Cells(1, dataSheet.Columns.Count).End(xlToLeft).Column
' loop through the rows and columns to create the csv file
' ********** assumes there is a header row ******************
For dataSheetRow = 2 To lastRow
csvLine = "" ' start a new line of csv to write to the file
For dataSheetCol = 1 To lastColumn
csvLine = csvLine & dataSheet.Cells(dataSheetRow, dataSheetCol).Value & "," ' append each columns data to the line
Next dataSheetCol
csvLine = Left(csvLine, Len(csvLine) - 1) ' remove the last comma
exportFile.WriteLine csvLine ' write it to the file
Next dataSheetRow
exportFile.Close
Set exportFile = Nothing
Set dataSheet = Nothing
Set fs = Nothing
'MsgBox "The file " & csvFileName & " is created"
End Sub
Function GetFolder() As String
' display a dialogbox to the user to select where
' to save the CSV file
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Choose a folder"
.AllowMultiSelect = False
.InitialFileName = Application.DefaultFilePath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem
Set fldr = Nothing
End Function
What it does is save my Data sheet in CSV without the header row (first row). Everything works fine, except that I wish to change it to CSV UTF-8. Unfortunately, as it is currently I am unable to use the file unless I save it manually as CSV UTF-8.
Also this part is optional but it would be nice to have. When I click the button - It opens a dialog box prompting to choose the folder to save the file as. The filename is inside the C2 cell, so it will save as C2value.csv . The second part is fine, because if I want to create a dynamic filepath, I can do that and simply type in the path that I want to use (A2 as an example, but it will be replaced with a formula). However, if I want to create a dynamic filepath, I would like to make to be able to easily remove the dialog box (comment out), because there will be no point in selecting the folder if the path is chosen automatically... I don't like the current implimentation because it doesn't allow me to see files in the folder, only folders.
How can I edit my current code... or I can just replace my current code with something more efficient, to accomplish what I am trying to do ?
Thank you !
Bookmarks