I have the following VBA which works but I would like to adjust it to do the following:
1) Hard code where the file is going to be saved. "C:/investment/guy"
2) Name the new xlsx file the Value of the named range 'Allocation_Model_Name' & date 'YYYMMDD'.CVS
3) Open the new file upon creation to review it.
Is this possible?
Cheers,
GG
1) Hard code where the file is going to be saved. "C:/investment/guy"
2) Name the new xlsx file the Value of the named range 'Allocation_Model_Name' & date 'YYYMMDD'.CVS
3) Open the new file upon creation to review it.
Is this possible?
Cheers,
GG
VBA Code:
Option Explicit
Public Sub SaveasCSV()
Dim iPtr As Integer
Dim sFileName As String
Dim intFH As Integer
Dim aRange As Range
Dim iLastColumn As Integer
Dim oCell As Range
Dim iRec As Long
Set aRange = Range("B2:D200")
iLastColumn = aRange.Column + aRange.Columns.Count - 1
iPtr = InStrRev(ActiveWorkbook.FullName, ".")
sFileName = Left(ActiveWorkbook.FullName, iPtr - 1) & ".xlsx"
sFileName = Application.GetSaveAsFilename(InitialFileName:=sFileName, FileFilter:="Excel Workbooks (*.xlsx*),*.xlsx*")
If sFileName = "False" Then Exit Sub
Close
intFH = FreeFile()
Open sFileName For Output As intFH
iRec = 0
For Each oCell In aRange
If oCell.Column = iLastColumn Then
Print #intFH, oCell.Value
iRec = iRec + 1
Else
Print #intFH, oCell.Value; ",";
End If
Next oCell
Close intFH
MsgBox "Finished: " & CStr(iRec) & " records written to " _
& sFileName & Space(10), vbOKOnly + vbInformation
End Sub
Last edited: