我正在使用宏和VBA代碼創建具有特定格式的文本文件。所有創建文本文件所需的數據都是從宏單元中收集的。 我附上了宏數據文件和輸出文本文件的圖片(請參見下文)。使用Excel宏和VBA創建和寫入文本文件
Desired output txt format-example
此外,下面是我產生擺脫宏觀數據和創建/寫入到一個文本文件,我的VBA代碼。我仍然需要弄清楚如何以指定的格式編寫它(期望的輸出txt格式示例)。
Sub ExcelToTxt()
'Declaring variables
Dim lCounter As Long
Dim lLastRow As Long
Dim destgroup As String
Dim parmlabel as Variant
Dim FName As Variant
'Activate Sheet1
Sheet1.Activate
'Find the last row that contains data
With Sheet1
lLastRow = .Cells(.Rows.Count, "A").End(xlDown).Row
End With
'Create txt file
FName = Application.GetSaveAsFilename("", "txt file (*.txt), *.txt")
'Open FName For Output As #1
For lCounter = 2 To lLastRow
'Read specific data from the worksheet
With Sheet1 destgroup = .Cells(lCounter, 19)
parmlabel = .Cells(lCounter, 8)
If destgroup="trex_15hz" Or destgroup="trex_10hz" Or destgroup="trex_5hz" Then
'Write selected data to text file
'Write #1, parmlabel
End If
End With
'Continue looping until the last row
Next lCounter
'Close the text file
Close #1
End Sub
任何幫助,我需要添加在我的VBA創建格式化輸出txt文件將不勝感激。
預先感謝您。
編輯你的問題,並添加代碼出現。人們不會去另一個網站看看你有什麼。 – BerticusMaximus
謝謝編輯@BerticusMaximus!我很感激。 – Jesus
爲LABEL DEFINITION塊創建一個「模板」,其中可變部分用標記表示,例如「」,「」等。使用Replace()替換每個標記與工作表行中的值,重新出口。替換完成後,將該塊寫出到您的文本文件中。 –