how to write data and string to a text file

N

NewGuy100

I am very new to this. How do you write string and data to a file?
For example, I want to write:
"I am 28 years old"
28 is a calculated number from a function
 
D

Dave Peterson

Using VBA?

Option Explicit
Sub testme()

Dim FileNum As Long
Dim myYears As Long

myYears = SomeFunction(DateSerial(2000, 12, 31))

FileNum = FreeFile
Close #FileNum
Open "c:\myfile.txt" For Output As #FileNum
Print #FileNum, "I am " & myYears & "years old"
Close #FileNum

End Sub
Function SomeFunction(myDate As Date) As Long
SomeFunction = DateDiff("yyyy", myDate, Date)
End Function
 
R

RagDyer

Depends on exactly what you're trying to do.

If the 28 is the return of a function that is in, say cell D1, and you want
the string to be in, say cell D20, then enter this in D20:

="I am "&D1&" years old."

On the other hand, if you would want the cell containing the actual function
to calculate the value *and* return it together with the string, you could
have this in D1:

="I am "&DATEDIF(A3,B3,"y")&" years old."

Where "DATEDIF(A3,B3,"y")" is the formula that returns the value 28.
--
HTH,

RD
==============================================
Please keep all correspondence within the Group, so all may benefit!
==============================================
 
N

NewGuy100

I am sorry about that, I wanted to print it to a text file using vba.
haven't seen it using FileNum before. I've seen it using WriteFile o
something simila
 
Top