Saving Selection as CSV to a predetermined location and name as cell Value

pooadrid

New Member
Joined
Nov 24, 2014
Messages
14
Good Morning,

I'm currently using two different codes, but I would like to blend these two together...


One code, simply saves the entire workbook as a spreadsheet, which is saving the file under the name which is referenced to a specific cell.
the file is automatically saved to a pre determined location within the code.

this code is currently working the way I need it to.

Code:
Sub SaveFileAsDate()
 Dim WSName As String, CName As String, Directory As String, savename As String
 ''''''''''''''''''''CHANGE THE NEXT 3 LINES TO FIT YOUR NEEDS'''''''''''''''''''''
 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 WSName = "Sheet1"
 'change "Sheet1" to sheet tab name containing cell reference
 CName = "C2"
 'change "A1" to the cell with your date
 Directory = "Z:\Accounting\Accounts Receivable\Endeavor ADP\1-Grids By First Name\"
 'directory you want to save to--(make sure string ends with forward slash \)
 '...to save to default directory change to "" (Null)
 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 savename = Sheets(WSName).Range(CName).Text
 If Directory = "" Then Directory = CurDir & "\"
 On Error GoTo errorsub:
 ActiveWorkbook.SaveAs Filename:=Directory & savename & ".xls"
 Exit Sub
 errorsub:
 Beep
 MsgBox "Changes not saved!", vbExclamation, Title:=savename & ".xls"
 End Sub


I have another code, which saves my selection as a .CSV file, however It pops up the windows save box, which I have to manually locate the file where I want to save it to, and also name the file manually....

Code:
[Sub CSVFile()

 Dim SrcRg As Range

 Dim CurrRow As Range

 Dim CurrCell As Range

 Dim CurrTextStr As String

 Dim ListSep As String

 Dim FName As Variant

 FName = Application.GetSaveAsFilename("", "CSV File (*.csv), *.csv")

 ListSep = Application.International(xlListSeparator)

     If Selection.Cells.Count > 1 Then

         Set SrcRg = Selection

     Else

         Set SrcRg = ActiveSheet.UsedRange

     End If

 Open FName For Output As #1
  
  
 For Each CurrRow In SrcRg.Rows

     CurrTextStr = ìî

     For Each CurrCell In CurrRow.Cells

         If IsNumeric(CurrCell.Value) Then

             CurrTextStr = CurrTextStr & CurrCell.Value & ListSep

         Else

             CurrTextStr = CurrTextStr & "" & CurrCell.Value & "" & ListSep

         End If

     Next

     While Right(CurrTextStr, 1) = ListSep

         CurrTextStr = Left(CurrTextStr, Len(CurrTextStr) - 1)

     Wend
  
  
     Print #1, CurrTextStr

 Next

 Close #1

 End Sub

Essentially, what I would like to do.


is have it where the 2nd code, will Save my selection as a CSV file, and automatically name its self based on whats in a specific cell, and saved to a specific location.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Forum statistics

Threads
1,215,516
Messages
6,125,284
Members
449,218
Latest member
Excel Master

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