gpt4 book ai didi

vba - Excel:如何仅在 CSV 文件中为字符串添加双引号

转载 作者:行者123 更新时间:2023-12-04 21:09:39 24 4
gpt4 key购买 nike

我想从 Excel 创建一个 CSV 文件,其中字符串值应该用双引号括起来,日期值应该是 MM/dd/yyyy 格式。所有数字和 bool 值都应该不带引号。

我该怎么办?

最佳答案

Excel 不允许您指定格式,这有点可怕。这是一个可能对您也有用的 MrExcel 链接。

http://www.mrexcel.com/forum/showthread.php?t=320531

这是该站点的代码:

Sub CSVFile()

Dim SrcRg As Range
Dim CurrRow As Range
Dim CurrCell As Range
Dim CurrTextStr As String
Dim ListSep As String
Dim FName As Variant
FName = Application.GetSaveAsFilename("", "CSV File (*.csv), *.csv")

If FName <> False Then
ListSep = Application.International(xlListSeparator)
If Selection.Cells.Count > 1 Then
Set SrcRg = Selection
Else
Set SrcRg = ActiveSheet.UsedRange
End If
Open FName For Output As #1
For Each CurrRow In SrcRg.Rows
CurrTextStr = ""
For Each CurrCell In CurrRow.Cells
CurrTextStr = CurrTextStr & """" & CurrCell.Value & """" & ListSep
Next
While Right(CurrTextStr, 1) = ListSep
CurrTextStr = Left(CurrTextStr, Len(CurrTextStr) - 1)
Wend
Print #1, CurrTextStr
Next
Close #1
End If
End Sub

关于vba - Excel:如何仅在 CSV 文件中为字符串添加双引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/846839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com