View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.misc
DJ MC DJ MC is offline
external usenet poster
 
Posts: 14
Default Copying cell values to a external file in VBA

Im looking for a piece of code which looks through cells and exports
the text in the cell into a .txt file. on each new row, column A must
contain a 2, if not the macro stops. Also after every cell copied into
the txt file, a comma needs to separate the values in the txt file.
for example this is my data:
A B C D E
F
2 5977494A Shaw Ann 1943/06/18 45 Sarto Park
2 1613589R Nolan Mary 1945/10/31 4 Priory Grove St
2 6112747J Kennedy Harry 1946/02/12 50 Raheen Road

so the txt file will look like:
2,5977494A,Shaw,Ann,1943/06/18,45 Sarto Park
2,1613589R,Nolan,Mary,1945/10/31,4 Priory Grove St
2,6112747J,Kennedy,Harry,1946/02/12,50 Raheen Road

This is the code i have already, just cant seem to crack it!

Sub Report_Body()

Dim Cell_Loc As String
Dim Cell_Num As Integer
Dim Cell_Contents As String
Dim Output As String
newfname = "C:\Documents and Settings\mcragg\My Documents\Excel
Reports\CWPS Folder\Record.txt"
Open newfname For Output As #2
Cell_Contents = 2
Cell_Num = 2

Do While Cell_Contents = "2"

Cell_Loc = "A" & Cell_Num
Cell_Contents = Worksheets("Sheet1").Range(Cell_Loc).Value
Output = Output & Cell_Contents
Output = Output & ","

Cell_Loc = "B" & Cell_Num
Cell_Contents = Worksheets("Sheet1").Range(Cell_Loc).Value
Output = Output & Cell_Contents
Output = Output & ","

Cell_Loc = "C" & Cell_Num
Cell_Contents = Worksheets("Sheet1").Range(Cell_Loc).Value
Output = Output & Cell_Contents
Output = Output & ","

Cell_Loc = "D" & Cell_Num
Cell_Contents = Worksheets("Sheet1").Range(Cell_Loc).Value
Output = Output & Cell_Contents
Output = Output & ","

Cell_Loc = "E" & Cell_Num
Cell_Contents = Worksheets("Sheet1").Range(Cell_Loc).Value
Output = Output & Cell_Contents
Output = Output & ","

Cell_Loc = "F" & Cell_Num
Cell_Contents = Worksheets("Sheet1").Range(Cell_Loc).Value
Output = Output & Cell_Contents
Output = Output & ","

Any help much appreciated

Matt