我有一个excel文件的用户输入,我需要输出到txt。 它被称为csv,但是分隔符不是"""但" |"所以我需要自己创建文件。
我最终创建了文件但是我需要让每一行以换行结束并完成工作但是在创建的文件的末尾有一个空行 carriege return and line feed 。
如何使用crlf ??
停止此操作或删除最后一行这里的功能不是我的设计我刚刚废弃了我在别处找到的东西。
现在我用它:
Sub CreateAfile()
rok = Format(Date, "yyyy")
Mesic = Format(Date, "mm")
dnes = Format(Date, "dd")
cesta = "\\somepath\"
plnacesta = cesta & "Data_" & rok & Mesic & dnes & "_0001" & ".csv"
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim pth As String
pth = ThisWorkbook.Path
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
Dim a As Object
If myFileExists(plnacesta) Then
MsgBox "Existuje!"
Exit Sub
Else
End If
Set a = fs.CreateTextFile(plnacesta, True)
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Výsledek")
Dim rng As Range
Set rng = sh.UsedRange
Dim sRange As String
sRange = GetTextFromRangeText(rng)
Call a.WriteLine(sRange)
'Call a.WriteLine(sRange)
a.Close
End Sub
Function myFileExists(ByVal strPath As String) As Boolean
'Function returns true if file exists, false otherwise
If Dir(strPath) > "" Then
myFileExists = True
Else
myFileExists = False
End If
End Function
Function GetTextFromRangeText(ByVal poRange As Range) As String
Dim vRange As Variant
Dim sRet As String
Dim i As Integer
Dim j As Integer
If Not poRange Is Nothing Then
vRange = poRange
For i = LBound(vRange) To UBound(vRange)
For j = LBound(vRange, 2) To UBound(vRange, 2)
sRet = sRet & vRange(i, j)
Next j
sRet = sRet & vbLf
Next i
End If
GetTextFromRangeText = sRet
End Function
答案 0 :(得分:1)
解决了它,而不是
Call a.WriteLine(sRange)
应该是
Call a.Write(sRange)
由于write不添加换行符
答案 1 :(得分:0)
我建议添加if条件
For i = LBound(vRange) To UBound(vRange)
For j = LBound(vRange, 2) To UBound(vRange, 2)
sRet = sRet & vRange(i, j)
Next j
If(i<> UBound(vRange)) Then
sRet = sRet & vbNewLine
End If
Next i