Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

data transfer?

Status
Not open for further replies.

q4s72534

MIS
Aug 19, 2003
59
US
I want to write a program that takes data from an excel spread sheet and puts it into a text file(comma delimited).

Any ideas or sample code?

Thanks
 
Select the range of cells that you want to export and run this:
Code:
Sub ExportRange()
    Dim Filename As String
    Dim NumRows As Long, NumCols As Integer
    Dim r As Long, c As Integer
    Dim Data
    Dim ExpRng As Range
    Set ExpRng = Selection
    NumCols = ExpRng.Columns.Count
    NumRows = ExpRng.Rows.Count
Code:
Filename = "c:\textfile.txt" ' You should change this
Code:
    Open Filename For Output As #1
        For r = 1 To NumRows
            For c = 1 To NumCols
                Data = ExpRng.Cells(r, c).Value
                If IsNumeric(Data) Then Data = Val(Data)
                If IsEmpty(ExpRng.Cells(r, c)) Then Data = ""
                If c <> NumCols Then
                    Write #1, Data;
                Else
                    Write #1, Data
                End If
            Next c
        Next r
    Close #1
End Sub

I hope this helps!




Peace! [peace]

Mike

Never say Never!!!
Nothing is impossible!!!
 
this might be a dumb question, but where do i put the name of the sheet and where do I select the rows?


Set ExpRng = a1:a6??


thanks
 
Glad I could help!



Peace! [peace]

Mike

Never say Never!!!
Nothing is impossible!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top