Copy worksheet to .csv save to folder

AawwYeahh

New Member
Joined
Aug 10, 2017
Messages
37
I am attempting to merge 2 macros but seem to be missing something
1 the first macro I have uses cell value to create folder and file (works fine)
2 the second macro copy's a worksheet and saves it to default folder as a .csv (works fine)
I am attempting to merge the 2 codes so that when executed macro will create folder
and copy and save worksheet in created folder as a .csv
VBA Code:
Sub tstcsv()
Dim strFilename, strDirname, strPathname, strDefpath As String
On Error Resume Next
Worksheets("Dashboard").Select
strDirname = Range("A4").Value
strFilename = Range("B4").Value
strDefpath = "C:\MidSouth\PENDING\"
If IsEmpty(strDirname) Then Exit Sub
If IsEmpty(strFilename) Then Exit Sub
Worksheets("Google").Copy
If Not Right(Defpath, 1) = "\" Then Defpath = Defpath & "\"
If Not Right(strFilename, 4) = ".csv" Then strFilename = strFilename & ".csv"
 
 Application.DisplayAlerts = False
With ActiveWorkbook
MkDir strDefpath & strDirname
strPathname = strDefpath & strDirname & "\" & strFilename 'create total string
ActiveSheet.ExportAsFixedFormat Type:=xlCSV, filename:= _
            strDefpath & strDirname & "\" & strFilename, Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
 .Close True
  End With
  
  Application.DisplayAlerts = True
 End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
ActiveSheet.ExportAsFixedFormat Type:=xlCSV, filename:= _ strDefpath & strDirname & "\" & strFilename, Quality:=xlQualityStandard, _ IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
That is to save as "pdf"


Try this:
VBA Code:
Sub tstcsv()
  Dim strFilename As String, strDirname As String, strPathname As String, strDefpath As String
  
  strDirname = Sheets("Dashboard").Range("A4").Value
  strFilename = Sheets("Dashboard").Range("B4").Value
  strDefpath = "C:\MidSouth\PENDING\"
   
  If strDirname = "" Then MsgBox "strDirname is blank":     Exit Sub
  If strFilename = "" Then MsgBox "strFilename is blank":   Exit Sub
  If Not Right(strDefpath, 1) = "\" Then strDefpath = strDefpath & "\"
  If Not Right(strFilename, 4) = ".csv" Then strFilename = strFilename & ".csv"
  If Dir(strDefpath & strDirname, vbDirectory) = "" Then MkDir strDefpath & strDirname
  strPathname = strDefpath & strDirname & "\" & strFilename 'create total string
    
  Application.DisplayAlerts = False
  Sheets("Google").Copy
  With ActiveWorkbook
    .SaveAs strPathname, xlCSV
    .Close True
  End With
  Application.DisplayAlerts = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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