2013-07-13 93 views
3
在文本文件中強調的一句話(用破折號)

請你怎麼輸出或打印線/破折號(例如---------------)作爲使用excel vba在文本文件中加下劃線。我正在創建一個從excel到一個外部文件的日誌文件。我想用下面的幾個破折號(------)來加下劃線。我在一個網站上看到一個vba代碼:字符串(60,「 - 」)「每日劑量的excel」,但沒有奏效。如何使用VBA

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

我想我已經設法解決它使用下面

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

另一種功能e.g的myString =字符串(65「 - 」)並沒有爲我工作。我將「Dim Mystring as String」定義爲。

回答

3

嘗試下面代碼

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

+1

一個很好的參考。我爲它添加了書籤。 TY – 2013-07-13 19:13:02