我试图将excel文件转换为文本文件,但是每次运行代码时,都会出现“应用程序定义或对象定义的错误”
在此先感谢!!!!!
Sub ExceltoText()
`Declaration
Dim FileName As String, sLine As String, Deliminator As String
Dim LastCol As Integer, LastRow As Integer, FileNumber As Integer
`Location and File Name
FileName = "C:\Users\Admin\Documents\New folder\ExceltoText.txt"
Deliminator = "|"
LastCol = ActiveSheet.Cells.SpecialCells(xlcelltypelast).Column
LastRow = ActiveSheet.Cells.SpecialCells(xlcelltypelast).Row
FileNumber = FreeFile
`To create txtfile
Open FileName For Output As FileNumber
`To read data from excel
For i = 1 To LastRow
For j = 1 To LastCol
If j = LastCol Then
sLine = sLine & Cells(i, j).Value
Else
sLine = sLine & Cells(i, j).Value & Deliminator
End If
Next j
`Wrighting data
Print #FileNumber, sLine
sLine = ""
Next i
Close #FileNumber
MsgBox "The text file has been generated"
End Sub
答案 0 :(得分:1)
您由于以下原因而出错
LastCol = ActiveSheet.Cells.SpecialCells(xlcelltypelast).Column
LastRow = ActiveSheet.Cells.SpecialCells(xlcelltypelast).Row
没有像xlcelltypelast
这样的常数。您需要的是xlCellTypeLastCell
我建议您阅读Range.SpecialCells method (Excel)
如果您要查找行或列中的最后一个单元格,则可能要使用.Find
,如图Here