繁体   English   中英

在文件夹中的所有工作簿中循环VBA函数

[英]Cycle a vba function through all workbooks in a folder

我想循环浏览文件夹中的所有工作簿,提取名为“工作表名称”的工作表,并将其保存为.csv文件,并带有其起源的文件的名称。 什么是快速的方法?

有问题的vba函数示例:

Sub Sheet_SaveAs()
  Dim wb As Workbook
  Sheets("Sheet Name").Copy  
  Set wb = ActiveWorkbook       
  With wb
  .SaveAs ThisWorkbook.Path & "\" & ThisWorkbook.name, FileFormat:=xlCSV
  '.Close False
  End With
End Sub

非常感谢您的光临

编辑:不是重复的,因为我正在从多个工作簿中提取工作表,而不是从一个工作簿中提取多个工作表。

EDIT2:谢谢大家。

这样的事情。

更改此路径以适合您的文件夹

strFolder = "c:\temp"

Sub LoopThroughFiles()
    Dim Wb As Workbook
    Dim ws As Worksheet
    Dim strFolder As String
    Dim strFile As String

    strFolder = "c:\temp"
    strFile = Dir(strFolder & "\*.xls*")

    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
    End With

    Do While Len(strFile) > 0
     Set Wb = Workbooks.Open(strFolder & "\" & strFile)
     Set ws = Nothing
     On Error Resume Next
     Set ws = Wb.Sheets("Sheet Name")
     On Error GoTo 0
     If Not ws Is Nothing Then ws.SaveAs Left$(Wb.FullName, InStrRev(Wb.FullName, ".")) & "csv", FileFormat:=xlCSV
     Wb.Close False
        strFile = Dir
    Loop

     With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM