我从Excel导出工作表作为CSV,但我在CSV文件的开头部分得到了这一行:
"UTF-8","","","","","","","","","","","","","","","","","","","","","","","","","",""
那我怎么摆脱这个呢?我已经尝试过CurrRow.Replace等。我可以用它来替换单词和诸如此类的东西,但不是这行或任何逗号或双引号。
真的很感激一些帮助。
Set UTFStream = CreateObject("adodb.stream")
UTFStream.Type = adTypeText
UTFStream.Mode = adModeReadWrite
UTFStream.Charset = "UTF-8"
UTFStream.LineSeparator = adLF
UTFStream.Open
'set field separator
ListSep = ","
'set source range with data for csv file
If Selection.Cells.Count > 1 Then
Set SrcRange = Selection
Else
Set SrcRange = ActiveSheet.UsedRange
End If
For Each CurrRow In SrcRange.Rows
'enclose each value with quotation marks and escape quotation marks in values
CurrTextStr = ""
For Each CurrCell In CurrRow.Cells
CurrTextStr = CurrTextStr & """" & CurrCell.Value & """" & ListSep
Next
'remove ListSep after the last value in line
While Right(CurrTextStr, 1) = ListSep
CurrTextStr = Left(CurrTextStr, Len(CurrTextStr) - 1)
'CurrRow.Replace What:=Chr(44) & "UTF-8" & Chr(44) & Chr(34) & Chr(44) & Chr(44) & Chr(34) & Chr(44) & Chr(44) & Chr(34) & Chr(44) & Chr(44) & Chr(34) & Chr(44) & Chr(44) & Chr(34) & Chr(44) & Chr(44) & Chr(34) & Chr(44) & Chr(44) & Chr(34) & Chr(44) & Chr(44) & Chr(34) & Chr(44) & Chr(44) & Chr(34) & Chr(44) & Chr(44) & Chr(34) & Chr(44) & Chr(44) & Chr(34) & Chr(44) & Chr(44) & Chr(34) & Chr(44) & Chr(44) & Chr(34) & Chr(44) & Chr(44) & Chr(34) & Chr(44) & Chr(44) & Chr(34) & Chr(44) & Chr(44) & Chr(34) & Chr(44) & Chr(44) & Chr(34) & Chr(44) & Chr(44) & Chr(34) & Chr(44) & Chr(44) & Chr(34) & Chr(44) & Chr(44) & Chr(34) & Chr(44) & Chr(44) & Chr(34) & Chr(44) & Chr(44) & Chr(34) & Chr(44) & Chr(44) & Chr(34) & Chr(44) & Chr(44) & Chr(34) & Chr(44) & Chr(44) & Chr(34) & Chr(44) & Chr(44) & Chr(34) & Chr(44) & Chr(44), Replacement:="X"
Wend
'add line to UTFStream
UTFStream.WriteText CurrTextStr, adWriteLine
Next
答案 0 :(得分:0)
所以这解决了我的问题。但是,没有version check
,它可以正常工作。我无法使用它,尝试调试它,但似乎路径是正确的?
Sub RemoveCommasDoubleQ()
' Enable a reference to 'Microsft Scripting Runtime'
' under VBA menu option Tools > References
Dim week, UserName As String
Dim MyFile, MyFilePath As String
Dim version As Integer
' Current week, XX
week = Format(Date, "ww")
' Username, e.g. niclas.madsen
UserName = Environ$("UserName")
' Initials, first letter of last and surname to caps
' e.g. niclas.madsen would be NM
UserName = UCase(Left(UserName, 1) & Mid(UserName, InStr(UserName, ".") + 1, 1))
' fix filename for saving purpose
MyFile = Replace(Replace("SupplierOrganization_W", "", ""), ".", "_") _
& "" _
& week _
& " " _
& UserName _
& ".csv"
'SupplierOrganization_WXX NM
MyFilePath = getDirSubParentPath & MyFile
' Version check
Do While Len(Dir(MyFilePath)) <> 0
version = version + 1
MyFilePath = getDirSubParentPath & "SupplierOrganization_W" & week & "-" & version & " " & UserName & ".csv"
Loop
'Const tmpFile As String = "C:\Users\niclas.madsen\Desktop\AP\tmp_file.txt"
Dim tmpFile, tmpFilePath As String
tmpFile = getDirSubParentPath & "tmp_file.txt"
Dim tmpString As String
'Dim fso As New FileSystemObject
Dim fso As Object 'scripting.filesystemobject
Set fso = CreateObject("scripting.filesystemobject")
If fso.FileExists(MyFilePath) = True Then
Application.ScreenUpdating = False
Open MyFilePath For Input As #1
Open tmpFile For Output As #2
tmpString = Input(LOF(1), 1) 'read the entire file
tmpString = Replace(tmpString, (Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) _
& Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) _
& Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) _
& Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) _
& Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) _
& Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) _
& Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) _
& Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) _
& Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) _
& Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) _
& Chr(34)), "") 'eliminate double quotation and commas in the first line with UTF-8
Print #2, tmpString 'output result
Close #1
Close #2
fso.DeleteFile (MyFilePath) 'delete original file
fso.CopyFile tmpFile, MyFilePath, True 'rename temp file
fso.DeleteFile (tmpFile) 'delete temp file
Application.ScreenUpdating = True
MsgBox "Finished processing file", vbInformation, "Done!"
Else
MsgBox "Cannot locate the file : " & MyFile, vbCritical, "Error"
End If
Set fso = Nothing
End Sub
' Get Parent Sub Directory Path
Function getDirSubParentPath()
getDirSubParentPath = ThisWorkbook.Path & Application.PathSeparator & "CSV" & Application.PathSeparator & "Parent" & Application.PathSeparator
End Function