Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 当我已经使用Workbooks.OpenText打开文本文件时,请关闭该文件_Excel_Vba - Fatal编程技术网

Excel 当我已经使用Workbooks.OpenText打开文本文件时,请关闭该文件

Excel 当我已经使用Workbooks.OpenText打开文本文件时,请关闭该文件,excel,vba,Excel,Vba,我是新加入这个论坛的,我想和大家分享我的智慧,你们也向我传达你们的智慧 我对Excel宏有足够的经验,但这个问题我找不到解决方案 我使用Workbooks.OpenText打开一个文本文件以在Excel中工作 Workbooks.OpenText文件名:=myfile,数据类型:=xlDelimited,来源:=xlWindows,其他:=True,其他字符:=“,” 但是当我要关闭它的时候,我不能。我尝试按照以下说明进行操作: 工作簿(myfile)。关闭 活动工作簿。关闭 myfile=

我是新加入这个论坛的,我想和大家分享我的智慧,你们也向我传达你们的智慧

我对Excel宏有足够的经验,但这个问题我找不到解决方案

我使用Workbooks.OpenText打开一个文本文件以在Excel中工作

Workbooks.OpenText文件名:=myfile,数据类型:=xlDelimited,来源:=xlWindows,其他:=True,其他字符:=“,”

但是当我要关闭它的时候,我不能。我尝试按照以下说明进行操作:

  • 工作簿(myfile)。关闭
  • 活动工作簿。关闭
myfile=“path\file.txt”

当我使用此说明时,Excel表示我无法使用它。我只需要采取一些信息,这个文本文件和关闭它没有保存

有人能帮我做这些吗?

这对我也有用

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
子样本()
将myfile设置为字符串
myfile=“C:\Delete Me.txt”
Workbooks.OpenText文件名:=myfile_
数据类型:=xlDelimited_
原点:=xlWindows_
其他:=正确_
其他字符:=“,”
myfile=GetFilenameFromPath(myfile)
工作簿(myfile)。关闭
端接头
公共函数GetFilenameFromPath(ByVal strPath作为字符串)作为字符串
如果右$(strPath,1)“\”和Len(strPath)>0,则
GetFilenameFromPath=_
GetFilenameFromPath(左$(strPath,Len(strPath)-1))+右$(strPath,1)
如果结束
端函数
这对我也适用

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
子样本()
将myfile设置为字符串
myfile=“C:\Delete Me.txt”
Workbooks.OpenText文件名:=myfile_
数据类型:=xlDelimited_
原点:=xlWindows_
其他:=正确_
其他字符:=“,”
myfile=GetFilenameFromPath(myfile)
工作簿(myfile)。关闭
端接头
公共函数GetFilenameFromPath(ByVal strPath作为字符串)作为字符串
如果右$(strPath,1)“\”和Len(strPath)>0,则
GetFilenameFromPath=_
GetFilenameFromPath(左$(strPath,Len(strPath)-1))+右$(strPath,1)
如果结束
端函数

ActiveWorkbook.Close
对我有效。顺便说一句,打开工作簿后,您确定焦点没有从工作簿中改变吗?如果此文件处于焦点,则此功能有效,但如果将信息粘贴到另一个工作簿中,则无法使用“Windows(myfile).Activate”再次返回到文本文件。无论如何,谢谢你的评论。你有一个
myfile=GetFilenameFromPath(myfile)
和我的代码一样,你可以使用
Windows(myfile)。同时激活
:)耶,我使用Windows(myfile)。激活并正常工作。
ActiveWorkbook.Close
对我有效。顺便说一句,您确定打开工作簿后焦点没有从工作簿中改变吗?如果您有此文件处于焦点,则此功能有效,但如果您将信息粘贴到另一个工作簿中,则无法使用“Windows(myfile).Activate”再次返回到文本文件。无论如何,谢谢你的评论。你有一个
myfile=GetFilenameFromPath(myfile)
就像在我的代码中一样,你可以使用
Windows(myfile)。同时激活
:)耶,我使用Windows(myfile)。激活并工作正常。好伙伴!此功能工作正常。非常感谢!好朋友!此功能工作正常。非常感谢!