【问题标题】:Converting an Excel to text in vb在vb中将Excel转换为文本
【发布时间】: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转文本?

标签: excel vba reference


【解决方案1】:

您必须在代码中将字符串变量path 设置为一个值。 您必须同时使用 DimSet 变量 ResultWB

可能还有其他问题。

【讨论】:

  • 这是我用来展示“WorkBooks”或我看到的“Active WorkBooks”的一些示例代码。我理解路径“c:\....”我只是不明白为什么我在“Excel 工作簿”上出现错误是参考还是我需要声明一个变量?
  • @LuannSlate 你在哪里设置ResultWB的实例??
  • 学生的 WB 暗淡 ThisWorkbook 暗淡 WorkBooks 暗淡 xlDoubleQuote '
  • Workbooks.OpenText(FileName:= _ TxtFileName _ , Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _ xlDoubleQuote, ConsecutiveDelimiter: =False, Tab:=False, 分号:=False, _ Comma:=False, Space:=False, Other:=True, OtherChar:="|", FieldInfo:=Array(Array(1, 1), _ Array (2, 1)), DecimalSeparator:=".", ThousandsSeparator:=",", _ TrailingMinusNumbers:=True)
  • “代码中的错误” - 你看到了什么错误,在哪一行?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多