PeterS
PeterS

Reputation: 724

How to Substitute Cell Value to TextFile

I'm very new with the VBA. Now I want to practice using a simple problem.

I have a excel file that contains the ff values:

Juan    Miguel  28
John    Smith   25

I want to substitute these value cell per cell in a text file.

Let's say my text file is something like this:

FirstName Person1: $A1
LastName Person1: $B1
Age Person1: $C1

FirstName Person2: $A2
LastName Person2: $B2
Age Person2: $C2

Is it possible to have this instead of hardcoding the content of a testfile inside my code using the code below?

Print #TextFile, "Hello Everyone!"

Upvotes: 1

Views: 95

Answers (1)

ib11
ib11

Reputation: 2558

You can use this code to get you started:

Dim i As Long, lastRow As Long

Set mySheet= ActiveSheet

lastRow = mySheet.Cells(Rows.Count, "A").End(xlUp).Row

For i = 1 To lastRow
    Print #TextFile, "FirstName Person" & CStr(i) & ": " & mySheet.Cells(i,1).Text
    Print #TextFile, "LastName Person" & CStr(i) & ": " & mySheet.Cells(i,2).Text
    Print #TextFile, "Age Person" & CStr(i) & ": " & mySheet.Cells(i,3).Text
    Print #TextFile,     'print a blank line
Next i

Upvotes: 2

Related Questions