Option Explicit
Public Enum eWriteMode
eNormal = 0
eAppend = 1
eOverwrite = 2
End Enum
Public Sub example()
WriteRange Sheet1.Columns(1), "C:\test\ColumnA.dat", eOverwrite
End Sub
Public Sub WriteRange(ByVal inputRange As Excel.Range, ByVal outputPath As String, _
Optional writeMode As eWriteMode = eWriteMode.eNormal)
Dim lngFileNum As Long
Dim rngCll As Excel.Range
Dim blnFileExists As Boolean
blnFileExists = LenB(Dir(outputPath))
If blnFileExists Then
If writeMode = eNormal Then
Err.Raise vbObjectError, Description:="A file already exists in the location specified."
ElseIf writeMode = eOverwrite Then
Kill outputPath
End If
End If
Set inputRange = Excel.Intersect(inputRange, inputRange.Parent.UsedRange) 'Make sure you notice this:)
lngFileNum = FreeFile
Open outputPath For Append Access Write Lock Read Write As lngFileNum
For Each rngCll In inputRange.Cells
Write #lngFileNum, rngCll.Value
Next
Close lngFileNum
End Sub