Create CSV in VBA for Excel for certain range with prompt if file exists
Clash Royale CLAN TAG#URR8PPP
Create CSV in VBA for Excel for certain range with prompt if file exists
I've mixed code and get good results considering that I wish to create csv file without header and for first 12 columns of file.
Also, I've found way to send message about successful creation. My main problem now, is the fact that I can't push code to ask me if file exists, and to create it just after confirmation.
The best solution will be if I may on easier way do next:
Below is code and obviously I need help
Private Sub CommandButton1_Click()
Dim fs As Object, a As Object, i As Integer, s As String, t As String, l As String, mn As String, PathCSV As String, NameCSV As String
PathCSV = "D:BOM"
NameCSV = "MMA - " & Format(Date, "mmmm yyyy") & ".csv"
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("D:BOMMMA - " & Format(Date, "mmmm yyyy") & ".csv", True)
For r = 5 To Range("A65536").End(xlUp).Row 'start in row 5 due row 1-4 is header
s = ""
c = 1
While c < 13
s = s & Cells(r, c) & ","
c = c + 1
Wend
a.writeline s 'write line
Next r
MsgBox "CSV file successfully save to " & PathCSV & NameCSV
End Sub
I've tried several example as well as one suggested by you, but I didn't implement in code in good way. Probably I should check if file exists before creation but I am not so familiar with this part of VBA. Thanks
– Radomir
Aug 12 at 6:17
1 Answer
1
try use the snippet
if fs.FileExists(pathcsv & namecsv) then
overwrite = msgbox("overwrite?", vbyesno)
if overwrite = vbno then exit sub
end if
Thanks a lot. Where I should put this part in my code? before creation fs or on some another place.
– Radomir
Aug 12 at 6:20
after
set fs = ...
and before set a = ...
.– PaichengWu
Aug 12 at 7:49
set fs = ...
set a = ...
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Not sure I understand the question but if you're trying to turn off (or on) the prompts for overwriting a file, you might want to check stackoverflow.com/questions/14634453/… . If, however, you actually want to create your own prompt to confirm the overwrite, you can use MsgBox with vbYesNoCancel - see David Zemens' example stackoverflow.com/a/16839561/8676647
– ifo20
Aug 11 at 21:32