在文件中获取“”而不是“不期望的”

时间:2013-03-01 14:29:23

标签: excel excel-vba vbscript excel-2007 vba

按钮单击中有一个代码,工作簿将保存到本地文本文件中。

工作簿包含以下信息:

  

CRITICAL;插入ifparam   值(3498, 'TAT_UNALLOCTRADESREC', 'STRING',“IF(STRING(C5)= STRING(”TCE    - 外部对冲“),STRING(”E“),IF(STRING(C5)= STRING(”TCE - 内部对冲“),STRING(”I“),STRING(C5)))');

但输出是关键的;

insert into ifparam values(3498,'TAT_UNALLOCTRADESREC','STRING','IF(STRING(C5)=STRING(""TCE - External Hedge""),STRING(""E""),IF(STRING(C5)=STRING(""TCE - Internal Hedge""),STRING(""I""),STRING(C5)))'); 

问题是输出中“我们正在获得”的地方。 任何人都可以帮助我获得这个,因为它在工作簿中,即;单双引号“而不是”“

如果需要更改代码,请建议。 使用的代码:

Private Sub CommandButton1_Click()
    Dim xlBook As Workbook, xlSheet As Worksheet
    Dim strOutputFileName As String
    Dim n As Long, i As Long, j As Long
    Dim MyData As String, strData() As String, MyArray() As String
    Dim strPath As String

    strPath = ActiveWorkbook.Path '<~~ \\plyalnppd3sm\d$\Temp\Arun\TAT\

    ThisWorkbook.SaveCopyAs strPath & "\Temp.xls"

    Set xlBook = Workbooks.Open(strPath & "\Temp.xls")

    For Each xlSheet In xlBook.Worksheets
        If xlSheet.Name <> "User_provided_data" Then
            strOutputFileName = strPath & "\" & xlSheet.Name & ".zup"
            xlSheet.SaveAs Filename:=strOutputFileName, FileFormat:=xlTextMSDOS



    n = n + 1

            ReDim Preserve MyArray(n)
            MyArray(n) = strOutputFileName
            Debug.Print strOutputFileName
        End If
    Next

    xlBook.Close SaveChanges:=False

    Kill strPath & "\Temp.xls"

    For i = 1 To UBound(MyArray)
        '~~> open the files in One go and store them in an array
        Open MyArray(i) For Binary As #1
        MyData = Space$(LOF(1))
        Get #1, , MyData
        Close #1
        strData() = Split(MyData, vbCrLf)

        '~~> Write to the text file
        Open MyArray(i) For Output As #1

        '~~> Loop through the array and check if the start and end has "
        '~~> And if it does then ignore those and write to the text file
        For j = LBound(strData) To UBound(strData)
            If Left(strData(j), 1) = """" And Right(strData(j), 1) = """" Then
                strData(j) = Mid(strData(j), 2, Len(strData(j)) - 2)
            End If
            Print #1, strData(j)
        Next j
        Close #1
    Next i
End Sub

1 个答案:

答案 0 :(得分:0)

最简单的解决方案,无需过多查看代码 - 在将strData(j)输出到文本文件之前添加此行:

strData(j) = Replace(strData(j), """""", """")

我确信有更好的方法,但这是一个非常简单,快速和肮脏的解决方法!