Hi Folks,
I have a workbook that I use as a template to export data to a csv (for uploading to a different system)
Following research I uploaded code. However, at the time, I had to manually select the cells for export. I've managed to change that (with the Used Range)
Can anyone help me so to remove the Input box and it just execute the code
Here's the code: (I've highlighted in Red the bit I want to get rid of)
Public Sub ExcelRowsToCSV()
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
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets(1)
ws.UsedRange.Select
Set aRange = Application.InputBox("Select a range:-", , Selection.Address, , , , , Type:=8)
iLastColumn = aRange.Column + aRange.Columns.Count - 1
iPtr = InStrRev(ActiveWorkbook.FullName, ".")
sFileName = Left(ActiveWorkbook.FullName, iPtr - 1) & ".csv"
sFileName = Application.GetSaveAsFilename(InitialFileName:=sFileName, FileFilter:="CSV (Comma delimited) (*.csv), *.csv")
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
I still need the SaveAs dialog box to open and, being honest, I can live without the final message box.
So if any of you geniuses out there have a smarter way of achieving the same ends, I'd be very grateful.
Ta!
MM
Bookmarks