【问题标题】:File saving in excel在excel中保存文件
【发布时间】:2013-02-21 05:37:07
【问题描述】:

在按钮点击下有一段代码可以将工作簿内容保存为txt文件

代码

Private Sub CommandButton1_Click()
    Dim xlApp As Object
    Dim xlBook As Object
    Dim xlSheet As Object
    Dim strOutputFileName

    Set xlApp = CreateObject("Excel.Application")
    Set xlBook = xlApp.Workbooks.Open("C:\Documents and Settings\bera02a\Desktop\Arun_TAT_Testing_orig_14022013.xls")

    For Each xlSheet In xlBook.Worksheets
        strOutputFileName = "C:\Documents and Settings\bera02a\Desktop\" & xlSheet.Name & ".txt"
        xlSheet.SaveAs Filename:=strOutputFileName, FileFormat:=xlUnicodeText
    Next
    xlApp.Quit
End Sub

工作簿中的原始行

create table abc as select rpt2_id, rpt2_usgcount, rpt2_usgdate, rpt2_usgmins from rpt2 where rpt2_id = 'xyz;

输出为

"create table abc as select rpt2_id, rpt2_usgcount, rpt2_usgdate, rpt2_usgmins from rpt2 where rpt2_id = 'xyz;"

输出的开头和结尾都带有双引号,并且仅用于工作簿中的插入和创建语句。

谁能帮我不要在输出中得到那个引号?

【问题讨论】:

标签: excel vba


【解决方案1】:

虽然你已经专门向彼得寻求帮助,但看看这是否是你想要的?

xlSheet.SaveAs Filename:=strOutputFileName, FileFormat:=xlTextPrinter

跟进(来自评论)

试试这个

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

【讨论】:

  • @user2075017:那是因为它超过了 256 个字符。请参阅上面给出的链接中的限制。我可以给你一个替代代码,如果你愿意,可以从开头和结尾删除"
  • @user2075017 如果您点击链接,您将找到绕过该限制的建议解决方案。此外,这是另一个问题。
  • 你到家后把文件发给我,然后我们从那里拿走?我相信这些错误的发生是因为模块 modAddin 中的一些其他错误
  • 嗨,Siddharth..我一到家就会给你发送 excel..同时,如果你能用上面提到的错误对我进行排序,我在输出中出现“”输出生成为 """"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-31
  • 2013-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多