Excel导入中的dynamicpath查找

我有一段VB代码可以在Excel中dynamic地find我当前的文件path。 现在我希望在从文本文件中导入文本时使用它,但是无法弄清楚如何对其进行编码。

这里是我的代码获取当前path:

Function GetCurDir() Dim str As String Dim pos As Integer str = ActiveWorkbook.FullName pos = InStrRev(str, "\") str = Mid(str, 1, pos) GetCurDir = str End Function 

这是我写入文件时的使用方法:

 MyFile = GetCurDir & "\Data\MaterialBalance\Ngasdata.dat" 

这也是我想用它的地方:

 With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;C:\NGasSim\Data\MaterialBalance\NGASPROD.DAT", Destination:=Range( _ "$G$4")) 

正如你所看到的,这是一个静态path..任何人都可以帮助我正确的语法?

只需创build另一个variables来构build连接string:

 MyFile = GetCurDir & "\Data\MaterialBalance\Ngasdata.dat" MyConnection = "TEXT;" & MyFile With ActiveSheet.QueryTables.Add(Connection:= _ MyConnection, Destination:=Range( _ "$G$4"))