Hello Bitter/Oso,
Welcome to the Forum!
Here is a macro that will copy the data from the active Excel worksheet into a text file. The file will be created if doesn't exist, and will overwrite the file if it does exist. You can change the starting row, the file name, and file path to what you are using. These are marked in the code in blue.
Macro to Write to Text File
Sub CreateTextFile()
Dim FB As Integer 'File Buffer Number
Dim FileName As String
Dim FilePath As String
Dim LastRow As Long
Dim MyFile As String
Dim R As Long
Dim Rng As Range
Dim StartRow As Long
StartRow = 1
With ActiveSheet.UsedRange
LastRow = .Rows.Count + .Row - 1
End With
FB = FreeFile
FileName = "XYZ.txt"
FilePath = "C:\"
MyFile = FilePath & "\" & FileName
Open MyFile For Output As #FB
For R = StartRow To LastRow
Set Rng = Range(Cells(R, "A"), Cells(R, "G"))
Print #FB, "COMPUTE LT TP LCODE=1 UPLAND/LAG TIME TRANSITION METHOD"
Print #FB, "LENGTH=" & Rng(1, 1) & "FT SLOPE=" & Rng(1, 2) & " K=" & Rng(1, 3)
Print #FB, "Kn=" & Rng(1, 4) & " CR=" & Rng(1, 5)
Print #FB, "COMPUTE NM HYD ID=1 HYD NO=" & Rng(1, 6) & " DA=" & Rng(1, 7) & " SQ MI"
Print #FB, "PER A=50 PER B=30 PER C=15 PER D=5"
Print #FB, "TP=0.0 MASSRAIN=-1"
Print #FB, 'Add a blank line
Next R
Close #FB
End Sub
Adding the Macro
1. Copy the macro above pressing the keys CTRL+C
2. Open your workbook
3. Press the keys ALT+F11 to open the Visual Basic Editor
4. Press the keys ALT+I to activate the Insert menu
5. Press M to insert a Standard Module
6. Paste the code by pressing the keys CTRL+V
7. Make any custom changes to the macro if needed at this time
8. Save the Macro by pressing the keys CTRL+S
9. Press the keys ALT+Q to exit the Editor, and return to Excel.
To Run the Macro...
To run the macro from Excel, open the workbook, and press ALT+F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.
Sincerely,
Leith Ross
Bookmarks