Kojo Jojo
Kojo Jojo

Reputation: 73

How to underline a sentence (with a dash ) in a text file using VBA

Please how do you output or print a line/dash (e.g ---------------) as an underline in a text file using excel vba. I am creating a log file from excel into an external file. I want to underline each line with several dashes (------) as below. I saw a vba code : String(60, "-") on one website " Daily doses of excel " but it didn't work.

Sheet1$D$16 Changed:  13 Jul 2013 15:01:14
------------------------------------------
Sheet1$B$21 Changed:  13 Jul 2013 15:04:04
------------------------------------------

I think I have managed to solve it using the function below

Application.WorksheetFunction.Rept("-", 65)

The other one e.g Mystring = String (65,"-") did not work for me. I defined "Dim Mystring as String".

Upvotes: 3

Views: 570

Answers (1)

Santosh
Santosh

Reputation: 12353

Try below code

Sub LogInformation()
    Const LogFileName As String = "D:\TEXTFILE.LOG"
    Dim LogMessage As String
    Dim FileNum As Integer
    LogMessage = "This is a test run"
    FileNum = FreeFile    ' next file number
    Open LogFileName For Append As #FileNum    ' creates the file if it doesn't exist
    Print #FileNum, LogMessage    ' write information at the end of the text file
    Print #FileNum, Application.Rept("-", 65)
    Close #FileNum    ' close the file
End Sub

OUTPUT

enter image description here

Upvotes: 3

Related Questions