循环遍历 excel 文件中的工作表并另存为 csv 文件的问题

鸭子

我正在编写一个遍历文件夹中文件的函数。在每个文件中,遍历工作表并将它们保存为 CSV 文件。我在没有翻阅床单的情况下对它们进行了测试,效果很好。但是,当我循环浏览工作表时,它会不断循环浏览文件。我跑了调试,发现到了最后一个文件的末尾时,又回到了第一个文件。我找不到哪里出了问题。这是我的代码:

Sub morningstar_VBA()
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
'SOURCE: www.TheSpreadsheetGuru.com

Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim filename As String
Dim path_to_save As String
Dim FldrPicker As FileDialog
Dim w As Long

'Retrieve Target Folder Path From User
  Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

    With FldrPicker
      .Title = "Select A Target Folder"
      .AllowMultiSelect = False
        If .Show <> -1 Then GoTo NextCode
        myPath = .SelectedItems(1) & "\"
    End With

'In Case of Cancel
NextCode:
  myPath = myPath
  If myPath = "" Then GoTo ResetSettings

'Target File Extension (must include wildcard "*")
  myExtension = "*.xlsx*"

'Target Path with Ending Extention
  myFile = Dir(myPath & myExtension)

'Loop through each Excel file in folder
  Do While myFile <> ""
    'Set variable equal to opened workbook
    Set wb = Workbooks.Open(filename:=myPath & myFile)

    'Ensure Workbook has opened before moving on to next line of code
    For w = 1 To Worksheets.Count
        With Worksheets(w).Copy
            'the ActiveWorkbook is now the new workbook populated with a copy of the current worksheet
            With ActiveWorkbook
                filename = .Worksheets(1).Name
                path_to_save = "E:\Morningstar_download\test\" & filename
                .SaveAs filename:=path_to_save, FileFormat:=xlCSV
                DoEvents
                .Close savechanges:=False
            End With
        End With
    Next w

    wb.Close savechanges:=True

    'Ensure Workbook has closed before moving on to next line of code
    DoEvents

    'Get next file name
    myFile = Dir
  Loop

'Message Box when tasks are completed
  MsgBox "Task Complete!"

ResetSettings:
  'Reset Macro Optimization Settings
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

End Sub
多里安

也许试试这个:

Sub morningstar_VBA()
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
'SOURCE: www.TheSpreadsheetGuru.com

Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim filename As String
Dim path_to_save As String
Dim FldrPicker As FileDialog
Dim w As Long

'Retrieve Target Folder Path From User
  Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

    With FldrPicker
      .Title = "Select A Target Folder"
      .AllowMultiSelect = False
        If .Show <> -1 Then GoTo NextCode
        myPath = .SelectedItems(1) & "\"
    End With

'In Case of Cancel
NextCode:
  myPath = myPath
  If myPath = "" Then GoTo ResetSettings

'Target File Extension (must include wildcard "*")
  myExtension = "*.xlsx*"

'Target Path with Ending Extention
  myFile = Dir(myPath & myExtension)

'Loop through each Excel file in folder
  Do While myFile <> ""
    'Set variable equal to opened workbook
    Set wb = Workbooks.Open(filename:=myPath & myFile)
    Windows(wb.Name).Visible = False

    'Ensure Workbook has opened before moving on to next line of code
    For w = 1 To wb.Worksheets.Count
        With wb.Worksheets(w).Copy
            'the ActiveWorkbook is now the new workbook populated with a copy of the current worksheet
                filename = ActiveWorkbook.Worksheets(1).Name
                path_to_save = "E:\Morningstar_download\test\" & filename

     wb.SaveAs Filename:="E:\Morningstar_download\test\" & filename & ".csv", FileFormat:=xlCSVWindows
Workbooks( Worksheets(w).Name & ".XLS").Close

        End With
    Next w

    wb.Close savechanges:=True

    'Ensure Workbook has closed before moving on to next line of code
    DoEvents

    'Get next file name
    myFile = Dir
  Loop

'Message Box when tasks are completed
  MsgBox "Task Complete!"

ResetSettings:
  'Reset Macro Optimization Settings
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

End Sub

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Excel中打开.csv文件并另存为Excel工作表时,它具有与原始Excel工作表相同的属性吗?

将Excel工作表另存为JSON文件

Excel VBA - 使用特定工作表中的内容另存为文件

将大矩阵另存为CSV文件-Excel中多行的标题

在熊猫中读取另存为CSV文件的Excel数据集

使用新输入将单个excel工作表另存为新的excel文件

在Excel中让问题循环遍历

将mysql表另存为csv修改文件,php中的某些单元格数据

循环多个 excel 文件以创建不同的数据帧,执行分组并在 R 中另存为单个 df

如何将Excel工作簿中的所有工作表另存为一个文本文件?

另存为==> Csv时在Excel中出现怪异问题

如何在不丢失长文本的情况下将Excel文件另存为CSV?

如何使用OpenOffice打开Excel文件并将其另存为CSV

无法另存为CSV文件

SQL Server管理器:循环并将文件另存为csv

在powershell中循环遍历csv文件

Excel VBA 宏循环“另存为”会增加文件大小?

我可以通过ClosedXML将EXCEL工作表另存为CSV吗?

打开Word模板并将.doc另存为当前工作簿文件夹-Excel中的VBA

Python循环遍历CSV文件及其列

Matlab 循环遍历未结合 csv 的文件

Python - 循环遍历 csv 文件行值

创建基于文本框的文件夹,并在此foder C#中另存为excel表

如何在python中“写入新的.CSV文件”或“另存为新的.CSV文件”

将.xlsx文件另存为.csv文件的SSIS过程

CSV文件另存为Creo Pro版本文件

在for循环中设置表并将其另存为csv

将电子表格另存为给定文件夹中的csv文件

Oracle - 如何使用命令从终端将表另存为 csv 文件