Search code examples
excelvba

How to create text file using VBA for Excel for MacOS


I am trying to create a file and save text into it. I tried below code but when I am using write #1, script will just replace existing text with the new text. I tried write #0 but no luck. Any suggestions.

Sub TestResultFile(output As String)
  Dim myFile As String
  myFile = Application.DefaultFilePath & "TestResults.txt"
  Debug.Print myFile
  Open myFile For Output As #1
  Write #1, output
  Close #1
End Sub

Update:

Answer by Lakshmi helped me solve this issue.


Solution

  • User Append to add text to new line.

    try below code-

    Sub TestResultFile(output As String)
      Dim myFile As String
      myFile = Application.DefaultFilePath & "TestResults.txt"
      Debug.Print myFile
      Open myFile For Append As #1
      Write #1, output
      Close #1
    End Sub