SaveAs CSV fails when saving to desktop

plsm5882

New Member
Joined
Apr 22, 2016
Messages
20
Hi.

SaveAs CSV saving to the desktop fails with a 1004 error File cannot be accessed... It works fine on other folders. Haven't been able to tweak why but I am wondering if its a Windows desktop special folder issue. Here is the Function that errors:

Function SaveSheetToCSV(sNurse, sVenue, sSheet, sSavePath)
Dim sCSVFileName As String, wSh As Worksheet, bRtn As Boolean, wWb As Workbook

Set wWb = ThisWorkbook
Set wSh = wWb.Worksheets(sSheet)

bRtn = True

sSavePath = Trim(sSavePath)

If Right(sSavePath, 1) <> "\" Then sSavePath = sSavePath & "\"

sNurse = Replace(Trim(sNurse), " ", "")
sVenue = Replace(Trim(sVenue), " ", "")

sCSVFileName = Chr(34) & sSavePath & sNurse & "-" & sVenue & "-" & Format(Now(), "ddMMMyyyy-hhmm") & ".csv" & Chr(34)

Application.DisplayAlerts = False
Application.ScreenUpdating = False

On Error Resume Next
wSh.Copy
If err.Number = 0 Then
With wWb
.SaveAs Filename:=sCSVFileName, FileFormat:=xlCSV, CreateBackup:=False ' Fails here with 1004
If err.Number = 0 And CheckPath(sCSVFileName, "File") = True Then
On Error GoTo 0
.Close
Else
bRtn = False
On Error GoTo 0
End If
End With
Else
bRtn = False
On Error GoTo 0
End If

Application.ScreenUpdating = True
Application.DisplayAlerts = True

SaveSheetToCSV = bRtn
End Function

Thanks
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Get rid of the Chr(34) values in your formula:
VBA Code:
sCSVFileName = Chr(34) & sSavePath & sNurse & "-" & sVenue & "-" & Format(Now(), "ddMMMyyyy-hhmm") & ".csv" & Chr(34)
sCSVFileName is already formatted as String. You do not need to literally add the quotations marks.
That may be causing your error.

If not, after that line, add this one:
VBA Code:
MsgBox sCSVFileName
and confirm that it is returning a valid file path and file name.
 
Upvote 0
You are welcome.
 
Upvote 0

Forum statistics

Threads
1,214,967
Messages
6,122,503
Members
449,090
Latest member
RandomExceller01

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