我已经使用CSV文件,用Excel多次多年来,通常发现Excel的本地CSV处理非常善变。我倾向于写我自己的csv处理程序。
下面是一个例子。您需要对其进行修改以满足您的需求。
Sub SaveAsCsv(ws As Worksheet, Name As String)
Dim fso As FileSystemObject
Dim fl As TextStream
Dim rw As Long
Dim ln As String
Dim Name as str
On Error GoTo EH
Name = "Your Path and File name"
Set fso = New FileSystemObject
Set fl = fso.CreateTextFile(Name, True)
For each rw in ws.UsedRange.Rows
' Modify this to match your Items colunmn number
If rw.Cells(1,ItemColumnNumber) <> "" then
ln = ""
' Modify this to match your colunmn numbers
For col = Col1 To LastCol
ln = ln & rw.Cells(1,col).Interior.Color & ","
' or process the colour into whatever code you want
Next
fl.WriteLine Left(ln, Len(ln) - 1)
End If
Next rw
EH:
On Error Resume Next
If Not fl Is Nothing Then fl.Close
Set fl = Nothing
Set fso = Nothing
End Sub
它包含对Microsoft脚本运行时的早期绑定引用,因此如果需要,请将引用或更改设置为延迟绑定。添加任何您需要的错误处理
注意:像这样迭代单元格范围可能会很慢,尤其是对于大范围。但鉴于您必须访问单元格格式,我没有看到任何其他选择。