【发布时间】:2018-05-20 17:34:05
【问题描述】:
Sub SaveToCSVs()
Dim fDir As String
Dim wB As Workbook
Dim wS As Worksheet
Dim fPath As String
Dim sPath As String
fPath = "C:\Users\rshuell\Desktop\My_Excel_Files\"
sPath = "C:\Users\rshuell\Desktop\My_Excel_Files\"
fDir = Dir(fPath)
Do While (fDir <> "")
If Right(fDir, 4) = ".xls" Or Right(fDir, 5) = ".xlsx" Then
On Error Resume Next
Set wB = Workbooks.Open(fPath & fDir)
For Each wS In wB.Sheets
wS.SaveAs sPath & wS.Name, xlCSV
Next wS
wB.Close False
Set wB = Nothing
End If
fDir = Dir
On Error GoTo 0
Loop
End Sub
我在引用 Excel 的“工作簿”参考时遇到困难,我的意图是制作一个非常小的 Excel 工作表选项卡,分隔成一个已创建的文本文件,但文件的名称被命名为我只需要的 excel 表将其重命名为文本文件。就像我说的那样,这并不是我无法弄清楚“工作簿”或“活动工作簿”的问题,我一直在我正在查找和查找如何引用 Microsoft.Excel 的示例代码中看到,我试图添加我的项目无法消除代码中的错误。任何帮助将不胜感激。
编辑
Sub SaveToCSVs()
Dim fDir As String
Dim wB As Workbook
Dim wS As Worksheet
Dim fPath As String
Dim sPath As String
fPath = "C:\Users\rshuell\Desktop\My_Excel_Files\"
sPath = "C:\Users\rshuell\Desktop\My_Excel_Files\"
fDir = Dir(fPath)
Do While (fDir <> "")
If Right(fDir, 4) = ".xls" Or Right(fDir, 5) = ".xlsx" Then
On Error Resume Next
Set wB = Workbooks.Open(fPath & fDir)
For Each wS In wB.Sheets
wS.SaveAs sPath & wS.Name, xlCSV
Next wS
wB.Close False
Set wB = Nothing
End If
fDir = Dir
On Error GoTo 0
Loop
End Sub
【问题讨论】:
-
您发布的代码与您描述的您想做的相反。
-
excel转文本?