Writing the contents of cells into a file

MyHero

New Member
Joined
Jan 22, 2009
Messages
26
Hey Folks,

I have a bunch of values in column A, and I need to write (export) the entire column into a file called "ColumnA.dat".

Can someone please help me create an Excel Macro Script that would accomplish that?

Thanks in advance.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Code:
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
 
Last edited:
Upvote 0
I was suggesting that perhaps code for this procedure was overkill for what is a simple manual process.
 
Upvote 0
Thank you all very much for your contributions.

Oorang, when I run your code I get the error "Compile Error: User-defined type not defined"

It then highlights:

Public Sub WriteRange(ByVal inputRange As Excel.Range, ByVal outputPath As String, _
Optional writeMode As eWriteMode =

Any ideas on how to improve your code?
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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