View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
deanop deanop is offline
external usenet poster
 
Posts: 9
Default retain table formatting when writing excel table to a txt file

Ok, I see how I goofed and have revised code ro read as follows:
a range name of the excel table is defined as "worksheet_to_text"
--------------------------------------------------------------------------------------------------------------
Sub writetabletotxtfile()
Dim ExpRng As Range
Dim ff As Integer
Set ExpRng = Range("worksheet_to_text")
Firstcol = ExpRng.Columns(1).Column
LastCol = Firstcol + ExpRng.Columns.Count
FirstRow = ExpRng.Rows(1).Row
LastRow = FirstRow + ExpRng.Rows.Count

ff = FreeFile()

Open "C:\Documents and Settings\XYZ\Desktop\tabletotextfile.txt" For
Output As ff
Print #ff, ExpRng.AddressLocal()
Print #ff, ExpRng.AddressLocal(RowAbsolute:=False,
columnabsolute:=False)

For r = FirstRow To LastRow
For c = Firstcol To LastCol
Print #ff, Cells(r, c);
Next c
Print #ff, 'blank line
Next r
Close ff
End Sub

--------------------------------------------------------------------------------
Here is the result txt file. see how recreated table line by line but
still unable to get spacing right. plz help

$B$2:$E$18
B2:E18

Black-Scholes Directly in a Excel Sheet

Stock priceS 61
Strike priceX 65
Years to maturityT 0.25
Risk-free rater 0.08
Volatilityv 0.3

d1-0.215089371482172
d2-0.365089371482172

European call value 2.52698589175614
European put value 5.23989965669523