Vba 将xls保存为csv时保留格式

Vba 将xls保存为csv时保留格式,vba,excel,Vba,Excel,.SaveAs文件名:=“name”&“.CSV”,文件格式:=xlCSV 以上是我用来将excel保存为csv的内容。然而,在这个过程中,我丢失了所有的格式。例如,小数变成整数等。。在excel vba中编写上述代码时,如何保留格式。或编写.csv文件而不是保存到.csv文件的示例 下面是从第一个示例中提取的代码 Sub makeCSV(theSheet As Worksheet) Dim iFile As Long, myPath As String Dim myArr() As Varia

.SaveAs文件名:=“name”&“.CSV”,文件格式:=xlCSV

以上是我用来将excel保存为csv的内容。然而,在这个过程中,我丢失了所有的格式。例如,小数变成整数等。。在excel vba中编写上述代码时,如何保留格式。

或编写.csv文件而不是保存到.csv文件的示例

下面是从第一个示例中提取的代码

Sub makeCSV(theSheet As Worksheet)
Dim iFile As Long, myPath As String
Dim myArr() As Variant, outStr As String
Dim iLoop As Long, jLoop As Long

myPath = Application.ActiveWorkbook.Path
iFile = FreeFile
Open myPath & "\myCSV.csv" For Output Lock Write As #iFile

myArr = theSheet.UsedRange
For iLoop = LBound(myArr, 1) To UBound(myArr, 1)
    outStr = ""
    For jLoop = LBound(myArr, 2) To UBound(myArr, 2) - 1
        If InStr(1, myArr(iLoop, jLoop), ",") Then
            outStr = outStr & """" & myArr(iLoop, jLoop) & """" & ","
        Else
            outStr = outStr & myArr(iLoop, jLoop) & ","
        End If
    Next jLoop
    If InStr(1, myArr(iLoop, jLoop), ",") Then
        outStr = outStr & """" & myArr(iLoop, UBound(myArr, 2)) & """"
    Else
        outStr = outStr & myArr(iLoop, UBound(myArr, 2))
    End If
    Print #iFile, outStr
Next iLoop

Close iFile
Erase myArr

End Sub

修改以处理不寻常的形状范围

Sub makeCSV(theSheet As Worksheet)
Dim iFile As Long, myPath As String
Dim myArr() As Variant, outStr As String
Dim iLoop As Long, jLoop As Long
Dim lastCol As Long

' set up output file
myPath = Application.ActiveWorkbook.Path
iFile = FreeFile
Open myPath & "\myCSV.csv" For Output Lock Write As #iFile

myArr = theSheet.UsedRange
For iLoop = LBound(myArr, 1) To UBound(myArr, 1) ' loop through all rows
    outStr = ""
    lastCol = UBound(myArr, 2) ' find last column with data in it
    For jLoop = UBound(myArr, 2) To LBound(myArr, 2) Step -1
        If myArr(iLoop, jLoop) = "" Then
            lastCol = jLoop - 1
        Else
            Exit For
        End If
    Next jLoop
    For jLoop = LBound(myArr, 2) To lastCol - 1 ' loop until second last column
        If InStr(1, myArr(iLoop, jLoop), ",") Then
            outStr = outStr & """" & myArr(iLoop, jLoop) & """" & ","
        Else
            outStr = outStr & myArr(iLoop, jLoop) & ","
        End If
    Next jLoop
    If InStr(1, myArr(iLoop, jLoop), ",") Then ' last column has no "," after it
        outStr = outStr & """" & myArr(iLoop, lastCol) & """"
    Else
        outStr = outStr & myArr(iLoop, lastCol)
    End If
    Print #iFile, outStr
Next iLoop

'clean up
Close iFile
Erase myArr

End Sub

CSV是一种文本格式。您不能有格式化的CSV文件。您需要格式化文件,或者需要逗号分隔值文本文件。若要保留格式,必须将其另存为.xls类型文件(.xls、.xlsx、.xlsm等)。在本网站上有许多示例,都是在不使用.SaveAs的情况下将工作表的内容导出为.csv。我强烈怀疑您的区域设置可能使用逗号表示小数点,甚至可能使用句点表示千位分隔符。如果是这种情况,则CSV创建工作与设计完全相同,您必须使用正确的代码页进行调整。我在CSV文件末尾看到逗号(在检查txt格式时)。如何删除它们?在我的测试用例中,我没有。如果你得到了额外的逗号,那是因为“UsedRange”比你预期的要大。有一些技术可以清理这些内容,但这可能会忽略主表之外的单元格。您要保存的工作表上有哪些数据?