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

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
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,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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