Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Excel工作表导出为.txt文件_Excel_Vba - Fatal编程技术网

将Excel工作表导出为.txt文件

将Excel工作表导出为.txt文件,excel,vba,Excel,Vba,我正在尝试将excel文档中的工作表导出为文本文件。基本上,我从用户那里获取输出文件路径,跳过第一个工作表,然后将所有剩余的excel工作表保存为.txt文件。我在运行时遇到以下错误:“运行时错误“1004”:应用程序定义或对象定义的错误”。此错误似乎是由ActiveSheet.SaveAs命令导致的 任何帮助都将不胜感激。提前谢谢 Sub exportSheets() Dim ws As Worksheet Dim sheetName, filePath As String

我正在尝试将excel文档中的工作表导出为文本文件。基本上,我从用户那里获取输出文件路径,跳过第一个工作表,然后将所有剩余的excel工作表保存为.txt文件。我在运行时遇到以下错误:“运行时错误“1004”:应用程序定义或对象定义的错误”。此错误似乎是由ActiveSheet.SaveAs命令导致的

任何帮助都将不胜感激。提前谢谢

Sub exportSheets()
    Dim ws As Worksheet
    Dim sheetName, filePath As String

    'get filepath from user input 
    filePath = Range("E6").Value

    'ask if the user is sure about exporting 
    answer = MsgBox("Export all Worksheets to text files?", vbYesNo, "Run Macro")

    If answer = vbYes Then
        'loop through every sheet in the work book
        For Each ws In ThisWorkbook.Worksheets

            'skip the first page in the excel workbook
            If ws.Index > 1 Then
                sheetName = ws.Name 'get the sheet name

                'save the active sheet
                ActiveSheet.SaveAs fileName:=filePath & "\" & sheetName & ".txt", FileFormat:=xlText, CreateBackup:=False
            End If
        Next
    End If

End Sub

修复上面的代码

Option Explicit

Sub exportSheets()
    Dim ws As Worksheet
    Dim filePath As String

    'get filepath from user input
    filePath = Range("E6").Value

    Dim answer As Variant

    'ask if the user is sure about exporting
    answer = MsgBox("Export all Worksheets to text files?", vbYesNo, "Run Macro")

    If answer = vbYes Then

        ' Turn off alerts. Be aware that will overwrite existing file without warning
        Application.DisplayAlerts = False

        'loop through every sheet in the work book
        For Each ws In ThisWorkbook.Worksheets

            With ws
                'skip the first page in the excel workbook
                If .Index > 1 Then
                    .SaveAs Filename:=filePath & "\" & .Name & ".txt", FileFormat:=xlTextMSDOS, CreateBackup:=False
                End If
            End With

        Next

        Application.DisplayAlerts = True

    End If

End Sub

我喜欢这个主意!不确定,但帮助提示,
FileFormat:=xlText
不是一个有效的选项。而且您总是保存
Activesheet
而不是
ws
。这会清理问题,似乎会让我离得更近。然而,它只保存了第一页上的信息(我猜是我点击的那张表)。如何更新活动工作表?它会将除第一张工作表外的所有工作表保存为文本文件!第一页是什么意思?为什么您认为需要更新活动工作表?运行粘贴的代码不起作用。保存工作表时,它会获取第一张工作表上的信息,并且只更新.txt文件的名称。我可以通过在.SaveAs命令之前添加“ws.Active”来解决这个问题。虽然这可能是一种迂回的方式,但它对我来说是可行的。