在VB上处理双引号

时间:2014-10-08 14:42:24

标签: excel-vba quotes vba excel

我是论坛的新手,也是VB的新手。

我正在尝试找到一种非常简单的方法将excel内容复制到文件(.txt)中,我会将其命名为。

我找到了许多有用的代码,但结果总是低至我不需要的额外引号或根本没有引号。

在文件中,我希望我在excel中拥有正确数量的双引号(")的信息,不多也不少。

excel中的文件与以下内容非常相似:

"*ABC (123)"
"345","Name",145.4
"325","Name 2", 435.2

我可以将其转到没有"或不想要的额外"的文件中。

如何将其保存在文件中的方式与出现的完全相同?

Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" _
    (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

Private Const MAX_PATH As Long = 260

'~~> Change this where and how you want to save the file
Const FlName = "C:\- Test\MyWorkbook.txt"

Sub Sample()
    Dim tmpFile As String
    Dim MyData As String, strData() As String
    Dim entireline As String
    Dim filesize As Integer

    '~~> Create a Temp File
    tmpFile = TempPath & Format(Now, "ddmmyyyyhhmmss") & ".PAT"

    ActiveWorkbook.SaveAs Filename:=tmpFile _
        , FileFormat:=xlText, CreateBackup:=False

    '~~> Read the entire file in 1 Go!
    Open tmpFile For Binary As #1
    MyData = Space$(LOF(1))
    Get #1, , MyData
    Close #1
    strData() = Split(MyData, vbCrLf)

    '~~> Get a free file handle
    filesize = FreeFile()

    '~~> Open your file
    Open FlName For Output As #filesize

    For i = LBound(strData) To UBound(strData)
        entireline = Replace(strData(i), """", "")
        '~~> Export Text
        Print #filesize, entireline
    Next i

    Close #filesize

    MsgBox "Saved"
End Sub

Function TempPath() As String
    TempPath = String$(MAX_PATH, Chr$(0))
    GetTempPath MAX_PATH, TempPath
    TempPath = Replace(TempPath, Chr$(0), "")
End Function

0 个答案:

没有答案