我是这个论坛的新手,我想分享我的智慧,并将你传达给你。
我对Excel宏有足够的经验但是这个问题我找不到解决方案。
我打开一个带有Workbooks.OpenText的文本文件,可以在Excel中使用。
Workbooks.OpenText Filename:=myfile, DataType:=xlDelimited, Origin:=xlWindows, Other:=True, OtherChar:=","
但是当我关闭它时,我不能。我尝试下面的说明:
myfile = "path\file.txt"
当我使用此说明时,Excel表示我无法使用它。我只需要获取这些文本文件的一些信息并关闭它而不保存。
有人可以帮我这些吗?
答案 0 :(得分:4)
这对我也有用
Sub Sample()
Dim myfile As String
myfile = "C:\Delete Me.txt"
Workbooks.OpenText fileName:=myfile, _
DataType:=xlDelimited, _
Origin:=xlWindows, _
Other:=True, _
OtherChar:=","
myfile = GetFilenameFromPath(myfile)
Workbooks(myfile).Close
End Sub
Public Function GetFilenameFromPath(ByVal strPath As String) As String
If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then
GetFilenameFromPath = _
GetFilenameFromPath(Left$(strPath, Len(strPath) - 1)) + Right$(strPath, 1)
End If
End Function