将Excel转换为vb

时间:2018-05-20 17:34:06

标签: excel vba reference

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表我只是需要将其重命名为文本文件。就像我说的那不是问题,我不能弄清楚我正在查找的示例代码中看到的“WorkBook”或“Active Workbook”,并查找如何引用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

1 个答案:

答案 0 :(得分:0)

您必须将字符串变量path设置为代码中的值。 您必须DimSet变量ResultWB

可能还有其他问题。