2013-11-25 43 views
1

我是這個論壇的新成員,我想分享我的智慧,並將您的意見傳達給我。當我已經使用Workbooks.OpenText打開它時,關閉文本文件

我有足夠的Excel宏的經驗,但這個問題我找不到解決方案。

我用Workbooks.OpenText打開一個文本文件在Excel中工作。

Workbooks.OpenText Filename:=myfile, DataType:=xlDelimited, Origin:=xlWindows, Other:=True, OtherChar:=","

但是,當我將關閉它,我不能。我嘗試用下一個指令:

  • 工作簿(MYFILE).Close
  • ActiveWorkbook.Close

myfile = "path\file.txt"

當我使用這個指令時,Excel說我不能使用它。我只需要獲取這些文本文件的一些信息並關閉它而不保存。

任何人都可以幫助我嗎?

+0

'ActiveWorkbook.Close'爲我工作。 –

+0

順便說一句你確定打開它後工作簿的焦點不會改變嗎? –

+0

如果你把這個文件放在焦點上,但是如果你把這些信息粘貼到另一個工作簿中,你就不能再用「Windows(myfile).Activate」回到文本文件。 無論如何感謝您的評論。 – Vallejote

回答

3

這也爲我的作品

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 
+0

偉大的伴侶! 這個功能很完美。 非常感謝! – Vallejote

相關問題